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

ComposerSpeechButton.tsx

302 lines8.5 KB

1import type { VoiceInputState } from "@t3tools/client-runtime/voice-input";
2import { CheckIcon, MicIcon, XIcon } from "lucide-react";
3import { useEffect, useState } from "react";
4​
5import { VoiceInputErrorPill } from "./VoiceInputErrorPill";
6import { VoiceInputPill, VoiceWaveform } from "./VoiceInputPill";
7import { cn } from "~/lib/utils";
8import { Button } from "../ui/button";
9import { Spinner } from "../ui/spinner";
10import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
11​
12type SpeechPresentation = {
13 status: string | null;
14 showsCancel: boolean;
15 showsConfirm: boolean;
16 confirmEnabled: boolean;
17 showsSend: boolean;
18};
19​
20export function resolveSpeechPresentation(
21 state: VoiceInputState,
22 progress: { downloaded: number; total: number } | null,
23): SpeechPresentation {
24 switch (state.phase) {
25 case "idle":
26 return {
27 status: null,
28 showsCancel: false,
29 showsConfirm: false,
30 confirmEnabled: false,
31 showsSend: true,
32 };
33 case "error":
34 return {
35 status: state.error ?? "Voice input failed",
36 showsCancel: false,
37 showsConfirm: false,
38 confirmEnabled: false,
39 showsSend: true,
40 };
41 case "preparing":
42 return {
43 status: progress
44 ? `Downloading speech model ${Math.round((progress.downloaded / Math.max(1, progress.total)) * 100)}%`
45 : "Preparing",
46 showsCancel: true,
47 showsConfirm: true,
48 confirmEnabled: false,
49 showsSend: true,
50 };
51 case "recording":
52 return {
53 status: "Recording",
54 showsCancel: true,
55 showsConfirm: true,
56 confirmEnabled: true,
57 showsSend: true,
58 };
59 case "transcribing":
60 return {
61 status: "Transcribing",
62 showsCancel: true,
63 showsConfirm: true,
64 confirmEnabled: false,
65 showsSend: true,
66 };
67 case "post-processing":
68 return {
69 status: "Post-processing",
70 showsCancel: true,
71 showsConfirm: true,
72 confirmEnabled: false,
73 showsSend: true,
74 };
75 }
77​
78function formatElapsed(seconds: number) {
79 return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
81​
82function RecordingStatus(props: { level: number; recordingLimitSeconds?: number | undefined }) {
83 const [elapsedSeconds, setElapsedSeconds] = useState(0);
84​
85 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- owns the timer displaying the native recording duration
86 useEffect(() => {
87 const startedAt = Date.now();
88 const timer = window.setInterval(
89 () => setElapsedSeconds(Math.floor((Date.now() - startedAt) / 1_000)),
90 250,
91 );
92 return () => window.clearInterval(timer);
93 }, []);
94​
95 return (
96 <>
97 <VoiceWaveform level={props.level} />
98 <span aria-hidden className="size-1.5 shrink-0 rounded-full bg-primary" />
99 <span className="shrink-0 text-secondary-label text-xs tabular-nums">
100 {formatElapsed(elapsedSeconds)}
101 {props.recordingLimitSeconds !== undefined
102 ? ` / ${formatElapsed(props.recordingLimitSeconds)}`
103 : ""}
104 </span>
105 </>
106 );
108​
109function ComposerSpeechStatus(props: {
110 state: VoiceInputState;
111 progress: { downloaded: number; total: number } | null;
112 level: number;
113 recordingLimitSeconds?: number | undefined;
114}) {
115 const presentation = resolveSpeechPresentation(props.state, props.progress);
116​
117 if (!presentation.status) return null;
118 const isRecording = props.state.phase === "recording";
119 const isError = props.state.phase === "error";
120​
121 return (
122 <div
123 className="flex h-7 min-w-0 flex-1 items-center gap-2"
124 role="status"
125 aria-live={isRecording ? "off" : "polite"}
126 aria-label={presentation.status}
127 >
128 {isRecording ? (
129 <RecordingStatus level={props.level} recordingLimitSeconds={props.recordingLimitSeconds} />
130 ) : (
131 <span
132 className={cn(
133 "min-w-0 flex-1 truncate text-right text-sm text-secondary-label",
134 isError && "text-destructive",
135 )}
136 >
137 {presentation.status}
138 </span>
139 )}
140 </div>
141 );
143​
144function ComposerSpeechCancelButton(props: {
145 state: VoiceInputState;
146 shortcutLabel?: string | null;
147 onCancel(): void;
148}) {
149 if (props.state.phase === "idle") return null;
150 const label = props.state.phase === "error" ? "Dismiss voice input error" : "Cancel voice input";
151​
152 return (
153 <Tooltip>
154 <TooltipTrigger
155 render={
156 <Button
157 type="button"
158 size="icon-sm-pill"
159 variant="ghost"
160 aria-label={label}
161 onPointerDown={(event) => event.preventDefault()}
162 onClick={props.onCancel}
163 className="shrink-0"
164 >
165 <XIcon />
166 </Button>
167 }
168 />
169 <TooltipPopup side="top">
170 {label}
171 {props.shortcutLabel ? ` (${props.shortcutLabel})` : ""}
172 </TooltipPopup>
173 </Tooltip>
174 );
176​
177export function ComposerSpeechButton(props: {
178 state: VoiceInputState;
179 progress: { downloaded: number; total: number } | null;
180 shortcutLabel?: string | null;
181 disabled?: boolean;
182 onStart(): void;
183 onCancel(): void;
184}) {
185 const presentation = resolveSpeechPresentation(props.state, props.progress);
186 const label = "Start voice input";
187​
188 if (presentation.showsConfirm || props.state.phase === "error") return null;
189​
190 return (
191 <Tooltip>
192 <TooltipTrigger
193 render={
194 <Button
195 type="button"
196 size="icon-sm"
197 variant="ghost"
198 aria-label={label}
199 disabled={props.disabled}
200 onPointerDown={(event) => event.preventDefault()}
201 onClick={() => {
202 if (props.disabled) return;
203 props.onStart();
204 }}
205 className="relative shrink-0"
206 >
207 <MicIcon />
208 </Button>
209 }
210 />
211 <TooltipPopup side="top">
212 {label}
213 {props.shortcutLabel ? ` (${props.shortcutLabel})` : ""}
214 </TooltipPopup>
215 </Tooltip>
216 );
218​
219export function ComposerSpeechRecordingPill(props: {
220 state: VoiceInputState;
221 progress: { downloaded: number; total: number } | null;
222 finishShortcutLabel?: string | null;
223 cancelShortcutLabel?: string | null;
224 level: number;
225 recordingLimitSeconds?: number | undefined;
226 onStart(): void;
227 disabled?: boolean;
228 onStop(): void;
229 onCancel(): void;
230 onSkipPostProcessing(): void;
231}) {
232 const presentation = resolveSpeechPresentation(props.state, props.progress);
233 if (props.state.phase === "error") {
234 return (
235 <VoiceInputErrorPill
236 error={props.state.error}
237 errorAction={props.state.errorAction}
238 disabled={props.disabled ?? false}
239 cancelShortcutLabel={props.cancelShortcutLabel ?? null}
240 onRetry={props.onStart}
241 onDismiss={props.onCancel}
242 />
243 );
244 }
245 if (!presentation.showsConfirm) return null;
246​
247 const label = presentation.confirmEnabled
248 ? "Finish voice input"
249 : (presentation.status ?? "Voice input is busy");
250​
251 return (
252 <VoiceInputPill>
253 <ComposerSpeechCancelButton
254 state={props.state}
255 shortcutLabel={props.cancelShortcutLabel ?? null}
256 onCancel={props.onCancel}
257 />
258 <ComposerSpeechStatus
259 state={props.state}
260 progress={props.progress}
261 level={props.level}
262 recordingLimitSeconds={props.recordingLimitSeconds}
263 />
264 {props.state.phase === "post-processing" ? (
265 <Button
266 type="button"
267 size="sm-pill"
268 variant="ghost"
269 onPointerDown={(event) => event.preventDefault()}
270 onClick={props.onSkipPostProcessing}
271 className="shrink-0"
272 >
273 Skip
274 </Button>
275 ) : (
276 <Tooltip>
277 <TooltipTrigger
278 render={
279 <Button
280 type="button"
281 size="icon-sm-pill"
282 aria-label={label}
283 disabled={!presentation.confirmEnabled}
284 onPointerDown={(event) => event.preventDefault()}
285 onClick={props.onStop}
286 className="shrink-0"
287 >
288 {presentation.confirmEnabled ? <CheckIcon /> : <Spinner aria-hidden />}
289 </Button>
290 }
291 />
292 <TooltipPopup side="top">
293 {label}
294 {presentation.confirmEnabled && props.finishShortcutLabel
295 ? ` (${props.finishShortcutLabel})`
296 : ""}
297 </TooltipPopup>
298 </Tooltip>
299 )}
300 </VoiceInputPill>
301 );