Open History to see the last commit
1import { MICROPHONE_PERMISSION_ERROR } from "@t3tools/client-runtime/voice-input";
2import { useNavigate } from "@tanstack/react-router";
3import { RotateCcwIcon, SettingsIcon, XIcon } from "lucide-react";
4
5import { Button } from "../ui/button";
6import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
7import { VoiceInputPill } from "./VoiceInputPill";
8
9const VOICE_ERROR_LABELS: Record<string, string> = {
10 [MICROPHONE_PERMISSION_ERROR]: "Microphone access needed",
11 "Microphone access is required for voice input.": "Microphone access needed",
12 "This draft is no longer available.": "Draft unavailable",
13 "Another voice recording is already active.": "Another recording active",
14 "Voice transcription is not available.": "Voice input unavailable",
15 "Voice transcription is still finishing. Try again shortly.": "Transcription still busy",
16 "Voice transcription is not available for this language.": "Language unsupported",
17 "Could not prepare voice transcription.": "Couldn’t prepare dictation",
18 "Could not start voice recording.": "Couldn’t start recording",
19 "Voice recording was interrupted.": "Recording interrupted",
20 "Could not finish voice recording.": "Couldn’t finish recording",
21 "Could not transcribe this recording.": "Transcription failed",
22 "Voice transcription on this environment failed.": "Transcription failed",
23 "The draft changed while voice input was running. The transcript was not added.": "Draft changed",
24 "No speech was detected.": "No speech detected",
25 "Microphone processing failed.": "Microphone processing failed",
26 "The microphone disconnected.": "Microphone disconnected",
27};
28
29function voiceErrorLabel(error: string | null): string {
30 return (error && VOICE_ERROR_LABELS[error]) || "Voice input failed";
31}
32
33export function VoiceInputErrorPill(props: {
34 error: string | null;
35 errorAction: "retry" | "settings" | null;
36 disabled?: boolean;
37 cancelShortcutLabel?: string | null;
38 onRetry(): void;
39 onDismiss(): void;
40}) {
41 const navigate = useNavigate();
42 const openSettings = props.errorAction === "settings";
43 const actionLabel = openSettings
44 ? "Open voice settings"
45 : props.error === "Could not transcribe this recording." ||
46 props.error === "Voice transcription on this environment failed."
47 ? "Retry transcription"
48 : "Record again";
49 return (
50 <VoiceInputPill>
51 <Tooltip>
52 <TooltipTrigger
53 render={
54 <Button
55 type="button"
56 size="icon-sm-pill"
57 variant="ghost"
58 aria-label="Dismiss voice input error"
59 onPointerDown={(event) => event.preventDefault()}
60 onClick={props.onDismiss}
61 >
62 <XIcon />
63 </Button>
64 }
65 />
66 <TooltipPopup side="top">
67 Dismiss voice input error
68 {props.cancelShortcutLabel ? ` (${props.cancelShortcutLabel})` : ""}
69 </TooltipPopup>
70 </Tooltip>
71 <Tooltip>
72 <TooltipTrigger
73 render={
74 <span
75 tabIndex={0}
76 role="status"
77 aria-label={props.error ?? "Voice input failed"}
78 className="min-w-0 flex-1 truncate text-sm text-destructive"
79 />
80 }
81 >
82 {voiceErrorLabel(props.error)}
83 </TooltipTrigger>
84 <TooltipPopup side="top">{props.error ?? "Voice input failed"}</TooltipPopup>
85 </Tooltip>
86 {props.errorAction ? (
87 <Tooltip>
88 <TooltipTrigger
89 render={
90 <Button
91 type="button"
92 size="icon-sm-pill"
93 variant="ghost"
94 aria-label={actionLabel}
95 disabled={props.disabled}
96 onPointerDown={(event) => event.preventDefault()}
97 onClick={() => {
98 if (openSettings) {
99 props.onDismiss();
100 void navigate({ to: "/settings/voice" });
101 } else {
102 props.onRetry();
103 }
104 }}
105 >
106 {openSettings ? <SettingsIcon /> : <RotateCcwIcon />}
107 </Button>
108 }
109 />
110 <TooltipPopup side="top">{actionLabel}</TooltipPopup>
111 </Tooltip>
112 ) : null}
113 </VoiceInputPill>
114 );
115}