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

main
ts 33 lines 1.43 KB
Raw
1 import { useEffect, useRef, type RefObject } from "react";
2
3 import { isCommandPaletteOpen } from "../commandPaletteBus";
4
5 /** Keep submission shortcuts reachable when the frozen composer loses focus to the page. */
6 export function useDictationSubmissionShortcut(input: {
7 enabled: boolean;
8 formRef: RefObject<HTMLFormElement | null>;
9 onKeyDown(event: KeyboardEvent): void;
10 }) {
11 const latest = useRef(input);
12 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- publishes committed inputs to the window keydown listener
13 useEffect(() => {
14 latest.current = input;
15 });
16 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to window keydown while dictation owns submission
17 useEffect(() => {
18 if (!input.enabled) return;
19 const onKeyDown = (event: KeyboardEvent) => {
20 if (event.defaultPrevented || isCommandPaletteOpen()) return;
21 const target = event.target;
22 if (!(target instanceof Element)) return;
23 if (target !== document.body && target !== document.documentElement) {
24 if (!latest.current.formRef.current?.contains(target)) return;
25 if (!target.closest('[data-testid="composer-editor"]')) return;
26 if (target.closest("textarea, input, button")) return;
27 }
28 latest.current.onKeyDown(event);
29 };
30 window.addEventListener("keydown", onKeyDown, true);
31 return () => window.removeEventListener("keydown", onKeyDown, true);
32 }, [input.enabled]);
33 }