( mcp: Pick<UseMCPReturn, 'send' | 'isRunning'>, getToken: TokenProvider, )
| 35 | channel === 'telegram' ? SensorSettings.getTelegramCode() : SensorSettings.getWhitelistCode(); |
| 36 | |
| 37 | export function useRemoteControl( |
| 38 | mcp: Pick<UseMCPReturn, 'send' | 'isRunning'>, |
| 39 | getToken: TokenProvider, |
| 40 | ): RemoteControlState { |
| 41 | const getTokenRef = useRef(getToken); |
| 42 | getTokenRef.current = getToken; |
| 43 | |
| 44 | const queue = useRef<RemoteMessage[]>([]); |
| 45 | const draining = useRef(false); |
| 46 | // The inbox loop that owns the `listening` flag. |
| 47 | const listenerRef = useRef<AbortController | null>(null); |
| 48 | // Bumped to re-run the drain effect when a message arrives or a remote run finishes. |
| 49 | const [wake, setWake] = useState(0); |
| 50 | |
| 51 | const [enabled, setEnabledState] = useState(() => SensorSettings.isRemoteControlEnabled()); |
| 52 | const [status, setStatus] = useState<Record<RemoteChannel, RemoteStatus | null>>({ whatsapp: null, telegram: null }); |
| 53 | // The linked channels' codes, as JSON so the listener only restarts when they actually change. |
| 54 | const [sessionKey, setSessionKey] = useState('{}'); |
| 55 | // The last answer per channel, and the code it was for: a failed status fetch falls back to it. |
| 56 | const lastStatus = useRef<Partial<Record<RemoteChannel, { code: string; status: RemoteStatus }>>>({}); |
| 57 | const [listening, setListening] = useState(false); |
| 58 | const [lastMessageAt, setLastMessageAt] = useState<number | null>(null); |
| 59 | |
| 60 | const setEnabled = useCallback((next: boolean) => { |
| 61 | SensorSettings.setRemoteControlEnabled(next); |
| 62 | setEnabledState(next); |
| 63 | }, []); |
| 64 | |
| 65 | // Only channels with a linked phone/chat are polled, all in one request: most tabs have nothing |
| 66 | // linked and so hold no connection open at all. |
| 67 | useEffect(() => { |
| 68 | const sessions = JSON.parse(sessionKey) as RemoteSessions; |
| 69 | if (!enabled || Object.keys(sessions).length === 0) { |
| 70 | setListening(false); |
| 71 | return; |
| 72 | } |
| 73 | const controller = new AbortController(); |
| 74 | listenerRef.current = controller; |
| 75 | setListening(true); |
| 76 | listenInbox({ |
| 77 | sessions, |
| 78 | getToken: () => getTokenRef.current(), |
| 79 | signal: controller.signal, |
| 80 | onMessage: message => { |
| 81 | Logger.info('MCP', `Remote message received via ${message.channel}`); |
| 82 | setLastMessageAt(Date.now()); |
| 83 | queue.current.push(message); |
| 84 | setWake(n => n + 1); |
| 85 | }, |
| 86 | // Only the current loop may clear the flag. A superseded loop (React re-running this |
| 87 | // effect, e.g. StrictMode's double mount) settles after its replacement started, and |
| 88 | // would otherwise leave the UI reading "connecting…" while the new loop polls happily. |
| 89 | }).finally(() => { if (listenerRef.current === controller) setListening(false); }); |
| 90 | return () => controller.abort(); |
| 91 | }, [enabled, sessionKey]); |
| 92 | |
| 93 | // The link itself lives on the server and changes when the user scans a QR on their phone, |
| 94 | // so it is polled rather than derived from anything in this tab, and re-checked as soon as a |
no test coverage detected