( mcp: Pick<UseMCPReturn, 'send' | 'isRunning' | 'subscribeTurn'>, getToken: TokenProvider, )
| 69 | } |
| 70 | |
| 71 | export function useRemoteControl( |
| 72 | mcp: Pick<UseMCPReturn, 'send' | 'isRunning' | 'subscribeTurn'>, |
| 73 | getToken: TokenProvider, |
| 74 | ): RemoteControlState { |
| 75 | const getTokenRef = useRef(getToken); |
| 76 | getTokenRef.current = getToken; |
| 77 | |
| 78 | const queue = useRef<RemoteMessage[]>([]); |
| 79 | const draining = useRef(false); |
| 80 | // The inbox loop that owns the `listening` flag. |
| 81 | const listenerRef = useRef<AbortController | null>(null); |
| 82 | // Bumped to re-run the drain effect when a message arrives or a remote run finishes. |
| 83 | const [wake, setWake] = useState(0); |
| 84 | |
| 85 | const [enabled, setEnabledState] = useState(() => SensorSettings.isRemoteControlEnabled()); |
| 86 | const [deadman, setDeadmanState] = useState(() => SensorSettings.isDeadmanEnabled()); |
| 87 | const [status, setStatus] = useState<Record<RemoteChannel, RemoteStatus | null>>({ whatsapp: null, telegram: null }); |
| 88 | // The linked channels' codes, as JSON so the listener only restarts when they actually change. |
| 89 | const [sessionKey, setSessionKey] = useState('{}'); |
| 90 | const sessionKeyRef = useRef(sessionKey); |
| 91 | sessionKeyRef.current = sessionKey; |
| 92 | const deadmanRef = useRef(deadman); |
| 93 | deadmanRef.current = deadman; |
| 94 | // The last answer per channel, and the code it was for: a failed status fetch falls back to it. |
| 95 | const lastStatus = useRef<Partial<Record<RemoteChannel, { code: string; status: RemoteStatus }>>>({}); |
| 96 | const [listening, setListening] = useState(false); |
| 97 | const [lastMessageAt, setLastMessageAt] = useState<number | null>(null); |
| 98 | |
| 99 | const setDeadman = useCallback((next: boolean) => { |
| 100 | SensorSettings.setDeadmanEnabled(next); |
| 101 | setDeadmanState(next); |
| 102 | // Turning it off while listening needs nothing more: the listener restarts and its next |
| 103 | // poll carries deadman=0. |
| 104 | if (next) { |
| 105 | SensorSettings.setRemoteControlEnabled(true); |
| 106 | setEnabledState(true); |
| 107 | } |
| 108 | }, []); |
| 109 | |
| 110 | const setEnabled = useCallback((next: boolean) => { |
| 111 | if (!next && deadmanRef.current) { |
| 112 | // The listener is about to stop for good: disarm first, or the silence reads as an alarm. |
| 113 | disarmDeadman(JSON.parse(sessionKeyRef.current) as RemoteSessions, () => getTokenRef.current()); |
| 114 | SensorSettings.setDeadmanEnabled(false); |
| 115 | setDeadmanState(false); |
| 116 | } |
| 117 | SensorSettings.setRemoteControlEnabled(next); |
| 118 | setEnabledState(next); |
| 119 | }, []); |
| 120 | |
| 121 | // Only channels with a linked phone/chat are polled, all in one request: most tabs have nothing |
| 122 | // linked and so hold no connection open at all. |
| 123 | useEffect(() => { |
| 124 | const sessions = JSON.parse(sessionKey) as RemoteSessions; |
| 125 | if (!enabled || Object.keys(sessions).length === 0) { |
| 126 | setListening(false); |
| 127 | return; |
| 128 | } |
no test coverage detected