Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useTextFieldSpeech.tsx

131 lines4.5 KB

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 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 };
64​
65export type TextFieldSpeech = ReturnType<typeof useTextFieldSpeech>;
66​
67export function TextFieldSpeechControls({
68 speech,
69 disabled = false,
70}: {
71 speech: TextFieldSpeech;
72 disabled?: boolean;
73}) {
74 return (
75 <>
76 {speech.preview ? (
77 <p role="status" className="w-full text-sm text-muted-foreground">
78 {speech.preview.committed}
79 <span className="opacity-70">{speech.preview.tentative}</span>
80 </p>
81 ) : null}
82 <div className="flex min-w-0 items-center gap-1">
83 <ComposerSpeechRecordingPill
84 state={speech.state}
85 progress={null}
86 level={speech.level}
87 finishShortcutLabel={speech.shortcutLabel}
88 cancelShortcutLabel="Esc"
89 disabled={disabled}
90 onStart={() => void speech.start()}
91 onStop={() => void speech.stop()}
92 onCancel={speech.cancel}
93 onSkipPostProcessing={speech.skipPostProcessing}
94 />
95 {speech.available ? (
96 <ComposerSpeechButton
97 state={speech.state}
98 progress={null}
99 shortcutLabel={speech.shortcutLabel}
100 disabled={disabled}
101 onStart={() => void speech.start()}
102 onCancel={speech.cancel}
103 />
104 ) : null}
105 </div>
106 <VoiceInputSetup
107 environmentId={speech.transcriptionEnvironmentId}
108 open={speech.setup.open}
109 step={speech.setup.step}
110 status={speech.status}
111 model={speech.setup.model}
112 downloading={speech.setup.downloading}
113 error={speech.setup.error}
114 onOpenChange={speech.setup.setOpen}
115 onDownload={() => void speech.setup.download()}
116 onCancelDownload={() => void speech.setup.cancelDownload()}
117 onStartRecording={() => void speech.setup.startRecording()}
118 />
119 </>
120 );
122​
123/** Render-prop form for editors that only sometimes have an environment to dictate on. */
124export function SpeechTextField({
125 children,
126 ...input
127}: Parameters<typeof useTextFieldSpeech>[0] & {
128 children: (speech: TextFieldSpeech) => ReactNode;
129}) {
130 return children(useTextFieldSpeech(input));