From ccd328918abad03494dbd6f2964fb2523357db9c Mon Sep 17 00:00:00 2001
From: Robert Long <robert@robertlong.me>
Date: Mon, 8 Nov 2021 11:40:46 -0800
Subject: [PATCH] Don't poll webrtc call stats when inspector hidden

---
 src/GroupCallInspector.jsx | 38 +++++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 17 deletions(-)

diff --git a/src/GroupCallInspector.jsx b/src/GroupCallInspector.jsx
index 045d738..e964527 100644
--- a/src/GroupCallInspector.jsx
+++ b/src/GroupCallInspector.jsx
@@ -117,6 +117,23 @@ export function GroupCallInspector({ client, groupCall, show }) {
     client.on("hangup", onCallHangup);
     client.on("toDeviceEvent", onToDeviceEvent);
 
+    onUpdateRoomState();
+  }, [client, groupCall]);
+
+  const toDeviceEventsByCall = useMemo(() => {
+    const result = {};
+
+    for (const event of toDeviceEvents) {
+      const callId = event.content.call_id;
+      const key = `${callId} (${event.sender})`;
+      result[key] = result[key] || [];
+      result[key].push(event);
+    }
+
+    return result;
+  }, [toDeviceEvents]);
+
+  useEffect(() => {
     let timeout;
 
     async function updateCallStats() {
@@ -151,27 +168,14 @@ export function GroupCallInspector({ client, groupCall, show }) {
       timeout = setTimeout(updateCallStats, 1000);
     }
 
-    updateCallStats();
-
-    onUpdateRoomState();
+    if (show) {
+      updateCallStats();
+    }
 
     return () => {
       clearTimeout(timeout);
     };
-  }, [client, groupCall]);
-
-  const toDeviceEventsByCall = useMemo(() => {
-    const result = {};
-
-    for (const event of toDeviceEvents) {
-      const callId = event.content.call_id;
-      const key = `${callId} (${event.sender})`;
-      result[key] = result[key] || [];
-      result[key].push(event);
-    }
-
-    return result;
-  }, [toDeviceEvents]);
+  }, [show]);
 
   if (!show) {
     return null;