Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
3import { useEffect, useRef, type ReactNode, type RefObject } from "react";
4
5import {
6 ComposerSpeechButton,
7 ComposerSpeechRecordingPill,
8} from "~/components/chat/ComposerSpeechButton";
9import { VoiceInputSetup } from "~/components/chat/VoiceInputSetup";
10import { primaryServerKeybindingsAtom } from "~/state/server";
11import { useDictationShortcut } from "./useDictationShortcut";
12import { useEnvironmentSpeechInput } from "./useEnvironmentSpeechInput";
13
14/** Binds dictation to a plain textarea draft; the shortcut targets it only while it has focus. */
15export 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 draftText: input.text,
35 disabled: input.disabled,
36 offerSetup: false,
37 readDraft: () => {
38 const field = latest.current.textareaRef.current;
39 const cursor = field?.selectionStart ?? latest.current.text.length;
40 return {
41 text: field?.value ?? latest.current.text,
42 selection: { start: cursor, end: field?.selectionEnd ?? cursor },
43 };
44 },
45 commitDraft: (text, selection) => {
46 latest.current.onTextChange(text);
47 window.requestAnimationFrame(() => {
48 const field = latest.current.textareaRef.current;
49 if (!field) return;
50 field.focus({ preventScroll: true });
51 field.setSelectionRange(selection.start, selection.end);
52 });
53 },
54 });
55 const shortcutLabel = useDictationShortcut({
56 keybindings,
57 speech,
58 disabled: input.disabled ?? false,
59 terminalOpen: false,
60 modelPickerOpen: false,
61 targetRef: input.textareaRef,
62 });
63 return { ...speech, shortcutLabel };
64}
65
66export type TextFieldSpeech = ReturnType<typeof useTextFieldSpeech>;
67
68export function TextFieldSpeechControls({
69 speech,
70 disabled = false,
71}: {
72 speech: TextFieldSpeech;
73 disabled?: boolean;
74}) {
75 return (
76 <>
77 {speech.preview ? (
78 <p
79 role="status"
80 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"
81 >
82 {speech.preview.committed}
83 <span className="text-muted-foreground">{speech.preview.tentative}</span>
84 </p>
85 ) : null}
86 <ComposerSpeechRecordingPill
87 size="compact"
88 state={speech.state}
89 progress={null}
90 level={speech.level}
91 finishShortcutLabel={speech.shortcutLabel}
92 cancelShortcutLabel="Esc"
93 disabled={disabled}
94 onStart={() => void speech.start()}
95 onStop={() => void speech.stop()}
96 onCancel={speech.cancel}
97 onSkipPostProcessing={speech.skipPostProcessing}
98 />
99 {speech.available ? (
100 <ComposerSpeechButton
101 state={speech.state}
102 size="compact"
103 shortcutLabel={speech.shortcutLabel}
104 disabled={disabled}
105 onStart={() => void speech.start()}
106 />
107 ) : null}
108 <VoiceInputSetup
109 environmentId={speech.transcriptionEnvironmentId}
110 open={speech.setup.open}
111 step={speech.setup.step}
112 status={speech.status}
113 model={speech.setup.model}
114 language={speech.setup.language}
115 downloading={speech.setup.downloading}
116 error={speech.setup.error}
117 onOpenChange={speech.setup.setOpen}
118 onRetryCatalog={speech.setup.retryCatalog}
119 onDownload={() => void speech.setup.download()}
120 onCancelDownload={() => void speech.setup.cancelDownload()}
121 onStartRecording={() => void speech.setup.startRecording()}
122 />
123 </>
124 );
125}
126
127/** Render-prop form for editors that only sometimes have an environment to dictate on. */
128export function SpeechTextField({
129 children,
130 ...input
131}: Parameters<typeof useTextFieldSpeech>[0] & {
132 children: (speech: TextFieldSpeech) => ReactNode;
133}) {
134 return children(useTextFieldSpeech(input));
135}