The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { DesktopAppActivationRequest } from "@t3tools/contracts"; |
| 2 | import { useEffect, useEffectEvent, useRef } from "react"; |
| 3 | |
| 4 | import { useOpenLocalWorkspace } from "../../hooks/useOpenLocalWorkspace"; |
| 5 | |
| 6 | export function DesktopAppActivationCoordinator() { |
| 7 | const openLocalWorkspace = useOpenLocalWorkspace(); |
| 8 | const queueRef = useRef(Promise.resolve()); |
| 9 | const activation = window.desktopBridge?.appActivation; |
| 10 | const ready = activation !== undefined && openLocalWorkspace.ready; |
| 11 | |
| 12 | const processRequest = useEffectEvent(async (request: DesktopAppActivationRequest) => |
| 13 | openLocalWorkspace.open(request), |
| 14 | ); |
| 15 | |
| 16 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to desktop activation requests over the bridge and marks the renderer ready |
| 17 | useEffect(() => { |
| 18 | if (!ready || activation === undefined) return; |
| 19 | |
| 20 | let subscribed = true; |
| 21 | const unsubscribe = activation.onRequest((request) => { |
| 22 | queueRef.current = queueRef.current.then(async () => { |
| 23 | const response = await processRequest(request); |
| 24 | await activation.complete(response); |
| 25 | }); |
| 26 | queueRef.current = queueRef.current.catch(() => undefined); |
| 27 | }); |
| 28 | // Skip readiness if React runs cleanup before this subscription can receive requests. |
| 29 | queueMicrotask(() => { |
| 30 | if (subscribed) void activation.setReady(true).catch(() => undefined); |
| 31 | }); |
| 32 | return () => { |
| 33 | subscribed = false; |
| 34 | void activation.setReady(false).catch(() => undefined); |
| 35 | unsubscribe(); |
| 36 | }; |
| 37 | }, [activation, ready]); |
| 38 | |
| 39 | return null; |
| 40 | } |