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

VoiceInputSetup.tsx

277 lines11.3 KB

1import { useStableCallback } from "~/hooks/useStableCallback";
2import {
3 listBrowserMicrophones,
4 updateMicrophoneOrder,
5 type BrowserMicrophone,
6} from "../../speech/browserMicrophones";
7import type {
8 EnvironmentId,
9 EnvironmentSpeechModel,
10 EnvironmentSpeechStatus,
11} from "@t3tools/contracts";
12import { useNavigate } from "@tanstack/react-router";
13import { CircleAlertIcon, MicIcon, RefreshCwIcon } from "lucide-react";
14import { useEffect, useState } from "react";
15​
16import { useClientSettings } from "../../hooks/useSettings";
17import {
18 isSystemDefaultDeviceId,
19 microphoneOrder,
20 preferMicrophone,
21 preferredMicrophoneId,
22} from "../../speech/microphoneOrder";
23import { useEnvironment } from "../../state/environments";
24import { AuthSettingsWriteScope } from "@t3tools/contracts";
25import { useEnvironmentScope } from "../../state/session";
26import { languageLabel } from "../settings/SpeechLanguageSelect";
27import { Button } from "../ui/button";
28import { Dialog } from "../ui/dialog";
29import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
30import { WizardFooter, WizardHeader, WizardPanel, WizardPopup } from "../ui/wizard";
31​
32export function VoiceInputSetup(props: {
33 environmentId: EnvironmentId | null;
34 open: boolean;
35 step: number;
36 status: EnvironmentSpeechStatus | null;
37 model: EnvironmentSpeechModel | null;
38 /** The language the offered model was chosen for. */
39 language: string | null;
40 downloading: boolean;
41 error: string | null;
42 onOpenChange(open: boolean): void;
43 onRetryCatalog: (() => void) | null;
44 onDownload(): void;
45 onCancelDownload(): void;
46 onStartRecording(): void;
47}) {
48 const navigate = useNavigate();
49 const environment = useEnvironment(props.environmentId);
50 const canManage = useEnvironmentScope(props.environmentId, AuthSettingsWriteScope);
51 const order = useClientSettings(microphoneOrder);
52 const [microphones, setMicrophones] = useState<BrowserMicrophone[]>([]);
53 const selectedMicrophone = preferredMicrophoneId(order, microphones);
54 const [loadingMicrophones, setLoadingMicrophones] = useState(false);
55 const [microphoneError, setMicrophoneError] = useState<string | null>(null);
56 const refreshMicrophones = useStableCallback(async () => {
57 if (!navigator.mediaDevices?.enumerateDevices) return;
58 setLoadingMicrophones(true);
59 try {
60 setMicrophones(await listBrowserMicrophones());
61 setMicrophoneError(null);
62 } catch {
63 setMicrophoneError("Couldn’t list microphones. The system default still works.");
64 }
65 setLoadingMicrophones(false);
66 });
67 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to native microphone changes while the device setup step is visible
68 useEffect(() => {
69 if (!props.open || props.step !== 1) return;
70 queueMicrotask(() => void refreshMicrophones());
71 const mediaDevices = navigator.mediaDevices;
72 mediaDevices?.addEventListener?.("devicechange", refreshMicrophones);
73 return () => mediaDevices?.removeEventListener?.("devicechange", refreshMicrophones);
74 }, [props.open, props.step, refreshMicrophones]);
75 const selectedIsUnavailable = Boolean(
76 selectedMicrophone && !microphones.some((device) => device.deviceId === selectedMicrophone),
77 );
78 const selectedMicrophoneLabel = selectedMicrophone
79 ? (microphones.find((device) => device.deviceId === selectedMicrophone)?.label ??
80 "Selected microphone (Unavailable)")
81 : "System default";
82 const status = props.status?.supported ? props.status : null;
83 const model = props.model;
84 const progress =
85 model?.downloaded === undefined ? null : Math.round((model.downloaded / model.size) * 100);
86 const openSettings = (hash: string) => {
87 props.onOpenChange(false);
88 void navigate({ to: "/settings/voice", hash });
89 };
90​
91 const destination = environment?.label ?? "your transcription environment";
92​
93 return (
94 <Dialog open={props.open} onOpenChange={props.onOpenChange}>
95 <WizardPopup>
96 <WizardHeader
97 title={props.step === 0 ? "Set up voice input" : "Voice input is ready"}
98 description={
99 props.step === 0
100 ? `Download a speech model once. Your audio is transcribed on ${destination}.`
101 : "Pick a microphone, then start talking."
102 }
103 />
104 <WizardPanel>
105 {props.step === 0 ? (
106 <section className="space-y-3 text-sm">
107 {model ? (
108 <div className="space-y-3 rounded-lg border border-border/70 bg-card/60 p-3">
109 <div className="flex items-baseline justify-between gap-3">
110 <h3 className="min-w-0 truncate font-medium">{model.name}</h3>
111 <span className="shrink-0 text-xs text-muted-foreground tabular-nums">
112 {Math.round(model.size / 1024 / 1024)} MB
113 </span>
114 </div>
115 <p className="text-xs text-muted-foreground">
116 {model.recommended && props.language
117 ? `Recommended for ${languageLabel(props.language)}. `
118 : ""}
119 Includes a 1.2 MB silence detector.
120 </p>
121 {props.downloading ? (
122 <div role="status" className="space-y-1.5">
123 <div
124 role="progressbar"
125 aria-label="Speech model download progress"
126 aria-valuemin={0}
127 aria-valuemax={100}
128 aria-valuenow={progress ?? undefined}
129 className="h-1 overflow-hidden rounded-full bg-muted"
130 >
131 <div
132 className="h-full bg-primary transition-[width] duration-200"
133 style={{ width: `${progress ?? 0}%` }}
134 />
135 </div>
136 <p className="text-xs text-muted-foreground tabular-nums">
137 {model.state === "verifying"
138 ? "Verifying…"
139 : progress === null
140 ? "Starting download…"
141 : `${progress}% downloaded`}
142 </p>
143 </div>
144 ) : null}
145 </div>
146 ) : !props.error ? (
147 <p role="status" className="text-muted-foreground">
148 Loading transcription models…
149 </p>
150 ) : null}
151 {props.error ? (
152 <p role="alert" className="flex items-start gap-1.5 text-destructive">
153 <CircleAlertIcon aria-hidden className="mt-0.5 size-3.5 shrink-0" />
154 {props.error}
155 </p>
156 ) : null}
157 </section>
158 ) : (
159 <section className="space-y-2 text-sm">
160 <label className="sr-only" htmlFor="voice-setup-microphone">
161 Microphone
162 </label>
163 <div className="flex items-center gap-2">
164 <Select
165 value={selectedMicrophone ? `device:${selectedMicrophone}` : "system-default"}
166 disabled={loadingMicrophones}
167 onValueChange={(value) => {
168 if (!value) return;
169 const deviceId = value.slice("device:".length);
170 const label =
171 microphones.find((device) => device.deviceId === deviceId)?.label ?? "";
172 updateMicrophoneOrder((order) =>
173 preferMicrophone(
174 order,
175 value === "system-default" || isSystemDefaultDeviceId(deviceId)
176 ? { kind: "default" }
177 : { kind: "device", deviceId, label },
178 ),
179 );
180 }}
181 >
182 <SelectTrigger
183 id="voice-setup-microphone"
184 size="sm"
185 aria-label="Microphone"
186 className="min-w-0 flex-1"
187 >
188 <MicIcon aria-hidden className="size-3.5 shrink-0 text-muted-foreground" />
189 <SelectValue>{selectedMicrophoneLabel}</SelectValue>
190 </SelectTrigger>
191 <SelectPopup align="end" alignItemWithTrigger={false}>
192 <SelectItem value="system-default">System default</SelectItem>
193 {selectedIsUnavailable ? (
194 <SelectItem value={`device:${selectedMicrophone}`}>
195 Selected microphone (Unavailable)
196 </SelectItem>
197 ) : null}
198 {microphones.map((device) => (
199 <SelectItem key={device.deviceId} value={`device:${device.deviceId}`}>
200 {device.label}
201 </SelectItem>
202 ))}
203 </SelectPopup>
204 </Select>
205 <Button
206 type="button"
207 variant="ghost"
208 size="icon-sm"
209 disabled={loadingMicrophones}
210 aria-label="Refresh microphones"
211 onClick={() => void refreshMicrophones()}
212 >
213 <RefreshCwIcon className="size-3.5" />
214 </Button>
215 </div>
216 {selectedIsUnavailable ? (
217 <p className="text-xs text-destructive">
218 That microphone is unplugged. Pick another one.
219 </p>
220 ) : null}
221 {microphoneError ? (
222 <p role="alert" className="text-xs text-muted-foreground">
223 {microphoneError}
224 </p>
225 ) : null}
226 </section>
227 )}
228 </WizardPanel>
229 {props.step === 0 ? (
230 <WizardFooter
231 leading={
232 <Button variant="ghost" onClick={() => openSettings("speech-models")}>
233 {model ? "Other models" : "Choose a model"}
234 </Button>
235 }
236 >
237 {props.onRetryCatalog ? (
238 <Button onClick={props.onRetryCatalog}>Retry loading models</Button>
239 ) : null}
240 {props.downloading ? (
241 <Button variant="outline" disabled={!canManage} onClick={props.onCancelDownload}>
242 Cancel download
243 </Button>
244 ) : null}
245 {model ? (
246 <Button
247 disabled={!canManage || props.downloading || !status}
248 onClick={props.onDownload}
249 >
250 {props.downloading
251 ? "Downloading…"
252 : props.error
253 ? "Try again"
254 : canManage
255 ? "Download model"
256 : "Needs an admin to download"}
257 </Button>
258 ) : null}
259 </WizardFooter>
260 ) : (
261 <WizardFooter
262 leading={
263 <Button variant="ghost" onClick={() => openSettings("")}>
264 Voice settings
265 </Button>
266 }
267 >
268 <Button onClick={props.onStartRecording}>
269 <MicIcon />
270 Start dictating
271 </Button>
272 </WizardFooter>
273 )}
274 </WizardPopup>
275 </Dialog>
276 );