The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 117 lines 4.14 KB
Raw
1 import { msg, useGT, useMessages } from "gt-react";
2 import type { VoiceInputErrorCode } from "@t3tools/client-runtime/voice-input";
3 import { useNavigate } from "@tanstack/react-router";
4 import { CircleAlertIcon, XIcon } from "lucide-react";
5
6 import { Button } from "../ui/button";
7 import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
8 import { VoiceInputPill } from "./VoiceInputPill";
9
10 const 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
27 export 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 <Tooltip>
55 <TooltipTrigger
56 render={
57 <Button
58 type="button"
59 size={compact ? "icon-xs" : "icon-sm-pill"}
60 variant="ghost"
61 aria-label={gt("Dismiss voice input error")}
62 onPointerDown={(event) => event.preventDefault()}
63 onClick={props.onDismiss}
64 className="shrink-0"
65 >
66 <XIcon />
67 </Button>
68 }
69 />
70 <TooltipPopup side="top">
71 {props.cancelShortcutLabel
72 ? gt("Dismiss ({shortcut})", { shortcut: props.cancelShortcutLabel })
73 : gt("Dismiss")}
74 </TooltipPopup>
75 </Tooltip>
76 <Tooltip>
77 <TooltipTrigger
78 render={
79 <span
80 tabIndex={0}
81 role="status"
82 aria-label={message}
83 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"
84 />
85 }
86 >
87 <CircleAlertIcon aria-hidden className="size-3.5 shrink-0" />
88 <span className="truncate">{errorLabel ? m(errorLabel) : gt("Voice input failed")}</span>
89 </TooltipTrigger>
90 <TooltipPopup side="top">{message}</TooltipPopup>
91 </Tooltip>
92 {props.errorAction ? (
93 <Button
94 type="button"
95 size={compact ? "xs" : "sm-pill"}
96 variant="outline"
97 aria-label={action.label}
98 disabled={props.disabled}
99 onPointerDown={(event) => event.preventDefault()}
100 onClick={() => {
101 if (openSettings) {
102 props.onDismiss();
103 void navigate({ to: "/settings/voice" });
104 } else {
105 props.onRetry();
106 }
107 }}
108 className="ms-auto shrink-0"
109 >
110 {action.text}
111 </Button>
112 ) : (
113 <span aria-hidden className="w-1.5 shrink-0" />
114 )}
115 </VoiceInputPill>
116 );
117 }