( 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 | // The one gate: linked and mirrored. As JSON so the listener only restarts on a real change. |
| 91 | const sessionKey = JSON.stringify(Object.fromEntries( |
| 92 | CHANNELS.filter(c => linked[c] && mirror[c]).map(c => [c, linked[c]]), |
| 93 | )); |
| 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 setMirror = useCallback((channel: RemoteChannel, on: boolean) => { |
| 100 | // Off: its polls are about to stop for good, so turnOffMirror disarms first, or the |
| 101 | // silence reads as an alarm. It fires MIRROR_CHANGED_EVENT, picked up below. |
| 102 | if (!on) { |
| 103 | turnOffMirror(channel, () => getTokenRef.current()); |
| 104 | return; |
| 105 | } |
| 106 | SensorSettings.setMirror(channel, on); |
| 107 | setMirrorState(prev => ({ ...prev, [channel]: on })); |
| 108 | }, []); |
| 109 | |
| 110 | // A mirror turned off elsewhere (the MCP's ask_user_info mirror=false), already disarmed. |
| 111 | useEffect(() => { |
| 112 | const sync = () => setMirrorState({ |
| 113 | whatsapp: SensorSettings.getMirror('whatsapp') === 'on', |
| 114 | telegram: SensorSettings.getMirror('telegram') === 'on', |
| 115 | }); |
| 116 | window.addEventListener(MIRROR_CHANGED_EVENT, sync); |
| 117 | return () => window.removeEventListener(MIRROR_CHANGED_EVENT, sync); |
| 118 | }, []); |
| 119 | |
| 120 | // Only mirrored phones/chats are polled, all in one request: most tabs have none and so hold |
| 121 | // no connection open at all. |
| 122 | useEffect(() => { |
| 123 | const sessions = JSON.parse(sessionKey) as RemoteSessions; |
| 124 | if (Object.keys(sessions).length === 0) { |
| 125 | setListening(false); |
| 126 | return; |
| 127 | } |
no test coverage detected