( mcp: Pick<UseMCPReturn, 'send' | 'isRunning' | 'subscribeTurn'>, getToken: TokenProvider, )
| 68 | } |
| 69 | |
| 70 | export function useRemoteControl( |
| 71 | mcp: Pick<UseMCPReturn, 'send' | 'isRunning' | 'subscribeTurn'>, |
| 72 | getToken: TokenProvider, |
| 73 | ): RemoteControlState { |
| 74 | const getTokenRef = useRef(getToken); |
| 75 | getTokenRef.current = getToken; |
| 76 | |
| 77 | const queue = useRef<RemoteMessage[]>([]); |
| 78 | const draining = useRef(false); |
| 79 | // The inbox loop that owns the `listening` flag. |
| 80 | const listenerRef = useRef<AbortController | null>(null); |
| 81 | // Bumped to re-run the drain effect when a message arrives or a remote run finishes. |
| 82 | const [wake, setWake] = useState(0); |
| 83 | |
| 84 | const [mirror, setMirrorState] = useState<Record<RemoteChannel, boolean>>(() => ({ |
| 85 | whatsapp: SensorSettings.getMirror('whatsapp') === 'on', |
| 86 | telegram: SensorSettings.getMirror('telegram') === 'on', |
| 87 | })); |
| 88 | const [status, setStatus] = useState<Record<RemoteChannel, RemoteStatus | null>>({ whatsapp: null, telegram: null }); |
| 89 | const [linked, setLinked] = useState<RemoteSessions>({}); |
| 90 | const linkedRef = useRef(linked); |
| 91 | linkedRef.current = linked; |
| 92 | // The one gate: linked and mirrored. As JSON so the listener only restarts on a real change. |
| 93 | const sessionKey = JSON.stringify(Object.fromEntries( |
| 94 | CHANNELS.filter(c => linked[c] && mirror[c]).map(c => [c, linked[c]]), |
| 95 | )); |
| 96 | // The last answer per channel, and the code it was for: a failed status fetch falls back to it. |
| 97 | const lastStatus = useRef<Partial<Record<RemoteChannel, { code: string; status: RemoteStatus }>>>({}); |
| 98 | const [listening, setListening] = useState(false); |
| 99 | const [lastMessageAt, setLastMessageAt] = useState<number | null>(null); |
| 100 | |
| 101 | const setMirror = useCallback((channel: RemoteChannel, on: boolean) => { |
| 102 | const code = linkedRef.current[channel]; |
| 103 | if (!on && code && SensorSettings.getMirror(channel) === 'on') { |
| 104 | // Its polls are about to stop for good: disarm first, or the silence reads as an alarm. |
| 105 | disarmDeadman({ [channel]: code }, () => getTokenRef.current()); |
| 106 | } |
| 107 | SensorSettings.setMirror(channel, on); |
| 108 | setMirrorState(prev => ({ ...prev, [channel]: on })); |
| 109 | }, []); |
| 110 | |
| 111 | // Only mirrored phones/chats are polled, all in one request: most tabs have none and so hold |
| 112 | // no connection open at all. |
| 113 | useEffect(() => { |
| 114 | const sessions = JSON.parse(sessionKey) as RemoteSessions; |
| 115 | if (Object.keys(sessions).length === 0) { |
| 116 | setListening(false); |
| 117 | return; |
| 118 | } |
| 119 | const controller = new AbortController(); |
| 120 | listenerRef.current = controller; |
| 121 | setListening(true); |
| 122 | listenInbox({ |
| 123 | sessions, |
| 124 | getToken: () => getTokenRef.current(), |
| 125 | signal: controller.signal, |
| 126 | onMessage: message => { |
| 127 | Logger.info('MCP', `Remote message received via ${message.channel}`); |
no test coverage detected