From 7173cc76cdc1ab38a5a76663a1e1b84f13697589 Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Mon, 15 Jul 2024 22:47:14 -0500 Subject: [PATCH] Use refs for proper js closures in the liveReady timeout --- web/src/components/player/LivePlayer.tsx | 11 ++++++++++- web/src/views/live/LiveDashboardView.tsx | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/web/src/components/player/LivePlayer.tsx b/web/src/components/player/LivePlayer.tsx index 180017eb9..88730b8fb 100644 --- a/web/src/components/player/LivePlayer.tsx +++ b/web/src/components/player/LivePlayer.tsx @@ -73,6 +73,15 @@ export default function LivePlayer({ const liveMode = useCameraLiveMode(cameraConfig, preferredLiveMode); const [liveReady, setLiveReady] = useState(false); + + const liveReadyRef = useRef(liveReady); + const cameraActiveRef = useRef(cameraActive); + + useEffect(() => { + liveReadyRef.current = liveReady; + cameraActiveRef.current = cameraActive; + }, [liveReady, cameraActive]); + useEffect(() => { if (!autoLive || !liveReady) { return; @@ -80,7 +89,7 @@ export default function LivePlayer({ if (!cameraActive) { const timer = setTimeout(() => { - if (!cameraActive) { + if (liveReadyRef.current && !cameraActiveRef.current) { setLiveReady(false); } }, 500); diff --git a/web/src/views/live/LiveDashboardView.tsx b/web/src/views/live/LiveDashboardView.tsx index e40634af0..cafd6647a 100644 --- a/web/src/views/live/LiveDashboardView.tsx +++ b/web/src/views/live/LiveDashboardView.tsx @@ -79,7 +79,7 @@ export default function LiveDashboardView({ return cameras .map((cam) => cam.name) - .filter((cam) => config.camera_groups[cameraGroup].cameras.includes(cam)) + .filter((cam) => config.camera_groups[cameraGroup]?.cameras.includes(cam)) .join(","); }, [cameras, cameraGroup, config, includeBirdseye]);