The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 72 lines 2.07 KB
Raw
1 import { useGT } from "gt-react";
2 import { useSyncExternalStore } from "react";
3
4 import { isMacPlatform } from "../lib/utils";
5
6 const HOLD_HINT_LINGER_MS = 1200;
7
8 type QuitHintMode = "hold" | "double-click";
9
10 let visibleMode: QuitHintMode | null = null;
11 let pressedMode: QuitHintMode = "hold";
12 let hideTimer: number | undefined;
13 const listeners = new Set<() => void>();
14 let stopBridge: (() => void) | null = null;
15
16 function setVisibleMode(next: QuitHintMode | null) {
17 if (next === visibleMode) return;
18 visibleMode = next;
19 for (const listener of listeners) listener();
20 }
21
22 function subscribeQuitHint(listener: () => void): () => void {
23 listeners.add(listener);
24 if (listeners.size === 1) {
25 stopBridge =
26 window.desktopBridge?.onQuitShortcut((hint) => {
27 window.clearTimeout(hideTimer);
28 if (hint.state === "down") {
29 pressedMode = hint.mode;
30 setVisibleMode(hint.mode);
31 return;
32 }
33 if (pressedMode === "double-click") {
34 setVisibleMode(null);
35 return;
36 }
37 hideTimer = window.setTimeout(() => setVisibleMode(null), HOLD_HINT_LINGER_MS);
38 }) ?? null;
39 }
40 return () => {
41 listeners.delete(listener);
42 if (listeners.size > 0) return;
43 window.clearTimeout(hideTimer);
44 stopBridge?.();
45 stopBridge = null;
46 visibleMode = null;
47 };
48 }
49
50 const readQuitHint = () => visibleMode;
51
52 export function QuitHoldOverlay() {
53 const visibleMode = useSyncExternalStore(subscribeQuitHint, readQuitHint, readQuitHint);
54 const gt = useGT();
55
56 if (!visibleMode) return null;
57 const shortcut = isMacPlatform(navigator.platform) ? "⌘Q" : "Ctrl+Q";
58 const message =
59 visibleMode === "hold"
60 ? gt("Hold {shortcut} or press twice to quit", { shortcut })
61 : gt("Press {shortcut} again to quit", { shortcut });
62 return (
63 <div
64 role="status"
65 className="pointer-events-none fixed inset-x-0 top-[22%] z-100 flex justify-center"
66 >
67 <div className="rounded-full bg-foreground/95 px-8 py-4 text-2xl font-bold text-background shadow-xl">
68 {message}
69 </div>
70 </div>
71 );
72 }