From f9e190f8b9cbe8adbf0734c43121c5a1fcccabbc Mon Sep 17 00:00:00 2001 From: Alex P Date: Fri, 10 Oct 2025 22:23:25 +0300 Subject: [PATCH] fix: prevent multiple getPermissions RPC calls on page load The getPermissions useEffect had send and pollPermissions in its dependency array. Since send gets recreated when rpcDataChannel changes, this caused multiple getPermissions RPC calls (5 observed) on page load. Fix: - Add rpcDataChannel readiness check to prevent calls before channel is open - Remove send and pollPermissions from dependency array - Keep only currentMode and rpcDataChannel.readyState as dependencies This ensures getPermissions is called only when: 1. The RPC channel becomes ready (readyState changes to "open") 2. The session mode changes (observer <-> primary) Eliminates duplicate RPC calls while maintaining correct behavior for mode changes and initial connection. --- ui/src/hooks/usePermissions.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ui/src/hooks/usePermissions.ts b/ui/src/hooks/usePermissions.ts index 3e3ed1e7..8c679537 100644 --- a/ui/src/hooks/usePermissions.ts +++ b/ui/src/hooks/usePermissions.ts @@ -62,7 +62,7 @@ interface PermissionsResponse { export function usePermissions() { const { currentMode } = useSessionStore(); - const { setRpcHidProtocolVersion, rpcHidChannel } = useRTCStore(); + const { setRpcHidProtocolVersion, rpcHidChannel, rpcDataChannel } = useRTCStore(); const [permissions, setPermissions] = useState>({}); const [isLoading, setIsLoading] = useState(true); const previousCanControl = useRef(false); @@ -102,8 +102,10 @@ export function usePermissions() { const { send } = useJsonRpc(handleRpcRequest); useEffect(() => { + if (rpcDataChannel?.readyState !== "open") return; pollPermissions(send); - }, [send, currentMode, pollPermissions]); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [currentMode, rpcDataChannel?.readyState]); // Monitor permission changes and re-initialize HID when gaining control useEffect(() => {