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 role="status" className="w-full text-sm text-muted-foreground">
79 {speech.preview.committed}
80 <span className="opacity-70">{speech.preview.tentative}</span>
81 </p>
82 ) : null}
83 <div className="flex min-w-0 items-center gap-1">
84 <ComposerSpeechRecordingPill
85 state={speech.state}
86 progress={null}
87 level={speech.level}
88 finishShortcutLabel={speech.shortcutLabel}
89 cancelShortcutLabel="Esc"
90 disabled={disabled}
91 onStart={() => void speech.start()}
92 onStop={() => void speech.stop()}
93 onCancel={speech.cancel}
94 onSkipPostProcessing={speech.skipPostProcessing}
95 />
96 {speech.available ? (
97 <ComposerSpeechButton
98 state={speech.state}
99 progress={null}
100 shortcutLabel={speech.shortcutLabel}
101 disabled={disabled}
102 onStart={() => void speech.start()}
103 onCancel={speech.cancel}
104 />
105 ) : null}
106 </div>
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. */
127export function SpeechTextField({
128 children,
129 ...input
130}: Parameters<typeof useTextFieldSpeech>[0] & {
131 children: (speech: TextFieldSpeech) => ReactNode;
132}) {
133 return children(useTextFieldSpeech(input));
134}