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

main
tsx 134 lines 4.57 KB
Raw
1 import { useAtomValue } from "@effect/atom-react";
2 import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
3 import { useEffect, useRef, type ReactNode, type RefObject } from "react";
4
5 import {
6 ComposerSpeechButton,
7 ComposerSpeechRecordingPill,
8 } from "~/components/chat/ComposerSpeechButton";
9 import { VoiceInputSetup } from "~/components/chat/VoiceInputSetup";
10 import { primaryServerKeybindingsAtom } from "~/state/server";
11 import { useDictationShortcut } from "./useDictationShortcut";
12 import { useEnvironmentSpeechInput } from "./useEnvironmentSpeechInput";
13
14 /** Binds dictation to a plain textarea draft; the shortcut targets it only while it has focus. */
15 export function useTextFieldSpeech(input: {
16 readonly environmentId: EnvironmentId;
17 readonly projectId?: ProjectId | null | undefined;
18 readonly ownerKey: string;
19 readonly text: string;
20 readonly textareaRef: RefObject<HTMLTextAreaElement | null>;
21 readonly onTextChange: (text: string) => void;
22 readonly disabled?: boolean | undefined;
23 }) {
24 const latest = useRef(input);
25 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- publishes committed field callbacks to the asynchronous recording controller
26 useEffect(() => {
27 latest.current = input;
28 });
29 const keybindings = useAtomValue(primaryServerKeybindingsAtom);
30 const speech = useEnvironmentSpeechInput({
31 environmentId: input.environmentId,
32 projectId: input.projectId,
33 ownerKey: input.ownerKey,
34 disabled: input.disabled,
35 offerSetup: false,
36 readDraft: () => {
37 const field = latest.current.textareaRef.current;
38 const cursor = field?.selectionStart ?? latest.current.text.length;
39 return {
40 text: field?.value ?? latest.current.text,
41 selection: { start: cursor, end: field?.selectionEnd ?? cursor },
42 };
43 },
44 commitDraft: (text, selection) => {
45 latest.current.onTextChange(text);
46 window.requestAnimationFrame(() => {
47 const field = latest.current.textareaRef.current;
48 if (!field) return;
49 field.focus({ preventScroll: true });
50 field.setSelectionRange(selection.start, selection.end);
51 });
52 },
53 });
54 const shortcutLabel = useDictationShortcut({
55 keybindings,
56 speech,
57 disabled: input.disabled ?? false,
58 terminalOpen: false,
59 modelPickerOpen: false,
60 targetRef: input.textareaRef,
61 });
62 return { ...speech, shortcutLabel };
63 }
64
65 export type TextFieldSpeech = ReturnType<typeof useTextFieldSpeech>;
66
67 export function TextFieldSpeechControls({
68 speech,
69 disabled = false,
70 }: {
71 speech: TextFieldSpeech;
72 disabled?: boolean;
73 }) {
74 return (
75 <>
76 {speech.preview ? (
77 <p
78 role="status"
79 className="order-first max-h-24 w-full overflow-y-auto rounded-lg border border-border/60 bg-muted/30 px-2.5 py-1.5 text-sm break-words whitespace-pre-wrap"
80 >
81 {speech.preview.committed}
82 <span className="text-muted-foreground">{speech.preview.tentative}</span>
83 </p>
84 ) : null}
85 <ComposerSpeechRecordingPill
86 size="compact"
87 state={speech.state}
88 progress={null}
89 level={speech.level}
90 finishShortcutLabel={speech.shortcutLabel}
91 cancelShortcutLabel="Esc"
92 disabled={disabled}
93 onStart={() => void speech.start()}
94 onStop={() => void speech.stop()}
95 onCancel={speech.cancel}
96 onSkipPostProcessing={speech.skipPostProcessing}
97 />
98 {speech.available ? (
99 <ComposerSpeechButton
100 state={speech.state}
101 size="compact"
102 shortcutLabel={speech.shortcutLabel}
103 disabled={disabled}
104 onStart={() => void speech.start()}
105 />
106 ) : null}
107 <VoiceInputSetup
108 environmentId={speech.transcriptionEnvironmentId}
109 open={speech.setup.open}
110 step={speech.setup.step}
111 status={speech.status}
112 model={speech.setup.model}
113 language={speech.setup.language}
114 downloading={speech.setup.downloading}
115 error={speech.setup.error}
116 onOpenChange={speech.setup.setOpen}
117 onRetryCatalog={speech.setup.retryCatalog}
118 onDownload={() => void speech.setup.download()}
119 onCancelDownload={() => void speech.setup.cancelDownload()}
120 onStartRecording={() => void speech.setup.startRecording()}
121 />
122 </>
123 );
124 }
125
126 /** Render-prop form for editors that only sometimes have an environment to dictate on. */
127 export function SpeechTextField({
128 children,
129 ...input
130 }: Parameters<typeof useTextFieldSpeech>[0] & {
131 children: (speech: TextFieldSpeech) => ReactNode;
132 }) {
133 return children(useTextFieldSpeech(input));
134 }