Open History to see the last commit
1import { msg, useGT, useMessages } from "gt-react";
2import type { VoiceInputErrorCode } from "@t3tools/client-runtime/voice-input";
3import { useNavigate } from "@tanstack/react-router";
4import { CircleAlertIcon, XIcon } from "lucide-react";
5
6import { Button } from "../ui/button";
7import { VoiceInputPill } from "./VoiceInputPill";
8import { SharedTooltipTrigger } from "../ui/shared-tooltip";
9
10const VOICE_ERROR_LABELS: Readonly<Record<VoiceInputErrorCode, string>> = {
11 "microphone-blocked": msg("Mic blocked"),
12 "draft-unavailable": msg("Draft unavailable"),
13 "already-recording": msg("Already recording"),
14 unavailable: msg("Voice input unavailable"),
15 "still-transcribing": msg("Still transcribing"),
16 "language-unsupported": msg("Language unsupported"),
17 "start-failed": msg("Couldn’t start"),
18 interrupted: msg("Recording interrupted"),
19 "finish-failed": msg("Couldn’t finish"),
20 "transcription-failed": msg("Transcription failed"),
21 "draft-changed": msg("Draft changed"),
22 "no-speech": msg("No speech heard"),
23 "microphone-failed": msg("Mic failed"),
24 "microphone-disconnected": msg("Mic disconnected"),
25};
26
27export function VoiceInputErrorPill(props: {
28 error: string | null;
29 errorAction: "retry" | "settings" | null;
30 errorCode?: VoiceInputErrorCode | undefined;
31 disabled?: boolean;
32 cancelShortcutLabel?: string | null;
33 size?: "default" | "compact" | undefined;
34 onRetry(): void;
35 onDismiss(): void;
36}) {
37 const gt = useGT();
38 const m = useMessages();
39 const navigate = useNavigate();
40 const message = props.error ?? gt("Voice input failed");
41 const openSettings = props.errorAction === "settings";
42 const compact = props.size === "compact";
43 const errorLabel = props.errorCode ? VOICE_ERROR_LABELS[props.errorCode] : undefined;
44 const action = openSettings
45 ? { label: gt("Open voice settings"), text: gt("Settings") }
46 : props.errorCode === "transcription-failed"
47 ? {
48 label: gt("Retry transcription"),
49 text: gt("Retry", { $context: "verb, retries transcription" }),
50 }
51 : { label: gt("Record again"), text: gt("Record again") };
52 return (
53 <VoiceInputPill tone="error" size={props.size}>
54 <SharedTooltipTrigger
55 render={
56 <Button
57 type="button"
58 size={compact ? "icon-xs" : "icon-sm-pill"}
59 variant="ghost"
60 aria-label={gt("Dismiss voice input error")}
61 onPointerDown={(event) => event.preventDefault()}
62 onClick={props.onDismiss}
63 className="shrink-0"
64 >
65 <XIcon />
66 </Button>
67 }
68 label={
69 props.cancelShortcutLabel
70 ? gt("Dismiss ({shortcut})", { shortcut: props.cancelShortcutLabel })
71 : gt("Dismiss")
72 }
73 />
74 <SharedTooltipTrigger
75 render={
76 <span
77 tabIndex={0}
78 role="status"
79 aria-label={message}
80 className="flex min-w-0 items-center gap-1.5 rounded-sm text-sm font-medium outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
81 />
82 }
83 label={message}
84 >
85 <CircleAlertIcon aria-hidden className="size-3.5 shrink-0" />
86 <span className="truncate">{errorLabel ? m(errorLabel) : gt("Voice input failed")}</span>
87 </SharedTooltipTrigger>
88 {props.errorAction ? (
89 <Button
90 type="button"
91 size={compact ? "xs" : "sm-pill"}
92 variant="outline"
93 aria-label={action.label}
94 disabled={props.disabled}
95 onPointerDown={(event) => event.preventDefault()}
96 onClick={() => {
97 if (openSettings) {
98 props.onDismiss();
99 void navigate({ to: "/settings/voice" });
100 } else {
101 props.onRetry();
102 }
103 }}
104 className="ms-auto shrink-0"
105 >
106 {action.text}
107 </Button>
108 ) : (
109 <span aria-hidden className="w-1.5 shrink-0" />
110 )}
111 </VoiceInputPill>
112 );
113}