The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { VoiceInputState } from "@t3tools/client-runtime/voice-input"; |
| 2 | import { msg, useGT, useMessages } from "gt-react"; |
| 3 | import { CheckIcon, MicIcon, XIcon } from "lucide-react"; |
| 4 | import { useEffect, useState, type PointerEvent, type ReactElement } from "react"; |
| 5 | |
| 6 | import { VoiceInputErrorPill } from "./VoiceInputErrorPill"; |
| 7 | import { VoiceInputPill, VoiceWaveform } from "./VoiceInputPill"; |
| 8 | import { m, type MessageResolver } from "~/i18n/messages"; |
| 9 | import { cn } from "~/lib/utils"; |
| 10 | import { Button } from "../ui/button"; |
| 11 | import { Spinner } from "../ui/spinner"; |
| 12 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 13 | |
| 14 | type DownloadProgress = { downloaded: number; total: number } | null; |
| 15 | |
| 16 | const keepFocus = (event: PointerEvent) => event.preventDefault(); |
| 17 | |
| 18 | /** One short status line per phase; also mirrored into the preview annotation editor. */ |
| 19 | export function resolveSpeechPresentation( |
| 20 | state: VoiceInputState, |
| 21 | progress: DownloadProgress, |
| 22 | resolve: MessageResolver = m, |
| 23 | ): { status: string | null; active: boolean } { |
| 24 | switch (state.phase) { |
| 25 | case "idle": |
| 26 | return { status: null, active: false }; |
| 27 | case "error": |
| 28 | return { status: state.error ?? resolve(msg("Voice input failed")), active: false }; |
| 29 | case "preparing": |
| 30 | return { |
| 31 | status: progress |
| 32 | ? resolve( |
| 33 | msg("Downloading model {percent}%", { |
| 34 | percent: Math.round((progress.downloaded / Math.max(1, progress.total)) * 100), |
| 35 | }), |
| 36 | ) |
| 37 | : resolve(msg("Starting…")), |
| 38 | active: true, |
| 39 | }; |
| 40 | case "recording": |
| 41 | return { |
| 42 | status: resolve(msg("Recording", { $context: "voice input status" })), |
| 43 | active: true, |
| 44 | }; |
| 45 | case "transcribing": |
| 46 | return { status: resolve(msg("Transcribing…")), active: true }; |
| 47 | case "post-processing": |
| 48 | return { status: resolve(msg("Cleaning up…")), active: true }; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | function formatElapsed(seconds: number) { |
| 53 | return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`; |
| 54 | } |
| 55 | |
| 56 | function RecordingStatus(props: { |
| 57 | level: number; |
| 58 | preparing: boolean; |
| 59 | recordingStartedAt?: number | null | undefined; |
| 60 | recordingLimitSeconds?: number | undefined; |
| 61 | }) { |
| 62 | const { preparing, recordingStartedAt } = props; |
| 63 | const [elapsedSeconds, setElapsedSeconds] = useState(() => |
| 64 | recordingStartedAt ? Math.max(0, Math.floor((Date.now() - recordingStartedAt) / 1_000)) : 0, |
| 65 | ); |
| 66 | |
| 67 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- owns the timer displaying the native recording duration |
| 68 | useEffect(() => { |
| 69 | if (preparing) return; |
| 70 | const startedAt = recordingStartedAt ?? Date.now(); |
| 71 | const timer = window.setInterval( |
| 72 | () => setElapsedSeconds(Math.floor((Date.now() - startedAt) / 1_000)), |
| 73 | 250, |
| 74 | ); |
| 75 | return () => window.clearInterval(timer); |
| 76 | }, [preparing, recordingStartedAt]); |
| 77 | |
| 78 | return ( |
| 79 | <> |
| 80 | <span |
| 81 | aria-hidden |
| 82 | className={cn( |
| 83 | "size-2 shrink-0 rounded-full", |
| 84 | preparing ? "bg-muted-foreground/40" : "bg-destructive", |
| 85 | )} |
| 86 | /> |
| 87 | <span className="shrink-0 text-xs text-muted-foreground tabular-nums"> |
| 88 | {formatElapsed(preparing ? 0 : elapsedSeconds)} |
| 89 | {props.recordingLimitSeconds !== undefined |
| 90 | ? ` / ${formatElapsed(props.recordingLimitSeconds)}` |
| 91 | : ""} |
| 92 | </span> |
| 93 | <div className="me-1.5 flex w-16 min-w-0 shrink grow"> |
| 94 | <VoiceWaveform level={props.level} /> |
| 95 | </div> |
| 96 | </> |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | function ShortcutTooltip(props: { |
| 101 | label: string; |
| 102 | shortcut?: string | null | undefined; |
| 103 | children: ReactElement; |
| 104 | }) { |
| 105 | return ( |
| 106 | <Tooltip> |
| 107 | <TooltipTrigger render={props.children} /> |
| 108 | <TooltipPopup side="top"> |
| 109 | {props.label} |
| 110 | {props.shortcut ? ` (${props.shortcut})` : ""} |
| 111 | </TooltipPopup> |
| 112 | </Tooltip> |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | export function ComposerSpeechButton(props: { |
| 117 | state: VoiceInputState; |
| 118 | shortcutLabel?: string | null; |
| 119 | disabled?: boolean; |
| 120 | /** Compact matches the `xs` buttons beside text-field editors. */ |
| 121 | size?: "default" | "compact"; |
| 122 | onStart(): void; |
| 123 | }) { |
| 124 | const gt = useGT(); |
| 125 | if (props.state.phase !== "idle") return null; |
| 126 | const label = gt("Start voice input"); |
| 127 | |
| 128 | return ( |
| 129 | <ShortcutTooltip label={label} shortcut={props.shortcutLabel}> |
| 130 | <Button |
| 131 | type="button" |
| 132 | size={props.size === "compact" ? "icon-xs" : "icon-sm"} |
| 133 | variant="ghost" |
| 134 | aria-label={label} |
| 135 | disabled={props.disabled} |
| 136 | onPointerDown={keepFocus} |
| 137 | onClick={() => { |
| 138 | if (!props.disabled) props.onStart(); |
| 139 | }} |
| 140 | className="shrink-0" |
| 141 | > |
| 142 | <MicIcon /> |
| 143 | </Button> |
| 144 | </ShortcutTooltip> |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | export function ComposerSpeechRecordingPill(props: { |
| 149 | state: VoiceInputState; |
| 150 | progress: DownloadProgress; |
| 151 | finishShortcutLabel?: string | null; |
| 152 | cancelShortcutLabel?: string | null; |
| 153 | level: number; |
| 154 | recordingStartedAt?: number | null | undefined; |
| 155 | recordingLimitSeconds?: number | undefined; |
| 156 | /** Compact matches the `xs` buttons beside text-field editors. */ |
| 157 | size?: "default" | "compact"; |
| 158 | onStart(): void; |
| 159 | disabled?: boolean; |
| 160 | onStop(): void; |
| 161 | onCancel(): void; |
| 162 | onSkipPostProcessing(): void; |
| 163 | }) { |
| 164 | const gt = useGT(); |
| 165 | const m = useMessages(); |
| 166 | const { phase } = props.state; |
| 167 | const compact = props.size === "compact"; |
| 168 | if (phase === "error") { |
| 169 | return ( |
| 170 | <VoiceInputErrorPill |
| 171 | size={props.size} |
| 172 | error={props.state.error} |
| 173 | errorAction={props.state.errorAction} |
| 174 | errorCode={props.state.errorCode} |
| 175 | disabled={props.disabled ?? false} |
| 176 | cancelShortcutLabel={props.cancelShortcutLabel ?? null} |
| 177 | onRetry={props.onStart} |
| 178 | onDismiss={props.onCancel} |
| 179 | /> |
| 180 | ); |
| 181 | } |
| 182 | const presentation = resolveSpeechPresentation(props.state, props.progress, m); |
| 183 | if (!presentation.active) return null; |
| 184 | const recording = phase === "recording"; |
| 185 | const starting = phase === "preparing" && props.progress === null; |
| 186 | const showRecordingLayout = starting || recording; |
| 187 | const trailing = showRecordingLayout || phase === "post-processing"; |
| 188 | |
| 189 | return ( |
| 190 | <VoiceInputPill size={props.size}> |
| 191 | <ShortcutTooltip label={gt("Cancel voice input")} shortcut={props.cancelShortcutLabel}> |
| 192 | <Button |
| 193 | type="button" |
| 194 | size={compact ? "icon-xs" : "icon-sm-pill"} |
| 195 | variant="ghost" |
| 196 | aria-label={gt("Cancel voice input")} |
| 197 | onPointerDown={keepFocus} |
| 198 | onClick={props.onCancel} |
| 199 | className="shrink-0" |
| 200 | > |
| 201 | <XIcon /> |
| 202 | </Button> |
| 203 | </ShortcutTooltip> |
| 204 | <div |
| 205 | className={cn( |
| 206 | "flex min-w-0 flex-1 items-center gap-2 overflow-hidden", |
| 207 | !trailing && "pe-2.5", |
| 208 | )} |
| 209 | role="status" |
| 210 | aria-live={recording ? "off" : "polite"} |
| 211 | aria-label={presentation.status ?? undefined} |
| 212 | > |
| 213 | {showRecordingLayout ? ( |
| 214 | <> |
| 215 | {starting && <span className="sr-only">{presentation.status}</span>} |
| 216 | <RecordingStatus |
| 217 | preparing={starting} |
| 218 | level={starting ? 0 : props.level} |
| 219 | recordingStartedAt={props.recordingStartedAt} |
| 220 | recordingLimitSeconds={props.recordingLimitSeconds} |
| 221 | /> |
| 222 | </> |
| 223 | ) : ( |
| 224 | <> |
| 225 | <Spinner aria-hidden size="sm" tone="muted" className="shrink-0" /> |
| 226 | <span className="min-w-0 truncate text-sm text-muted-foreground"> |
| 227 | {presentation.status} |
| 228 | </span> |
| 229 | </> |
| 230 | )} |
| 231 | </div> |
| 232 | {phase === "post-processing" ? ( |
| 233 | <Button |
| 234 | type="button" |
| 235 | size={compact ? "xs" : "sm-pill"} |
| 236 | variant="ghost" |
| 237 | onPointerDown={keepFocus} |
| 238 | onClick={props.onSkipPostProcessing} |
| 239 | className="shrink-0" |
| 240 | > |
| 241 | {gt("Skip", { $context: "verb, skips cleaning up the transcript" })} |
| 242 | </Button> |
| 243 | ) : showRecordingLayout ? ( |
| 244 | <ShortcutTooltip label={gt("Finish voice input")} shortcut={props.finishShortcutLabel}> |
| 245 | <Button |
| 246 | type="button" |
| 247 | size={compact ? "icon-xs" : "icon-sm-pill"} |
| 248 | aria-label={gt("Finish voice input")} |
| 249 | disabled={starting} |
| 250 | onPointerDown={keepFocus} |
| 251 | onClick={props.onStop} |
| 252 | className="shrink-0" |
| 253 | > |
| 254 | {starting ? <Spinner aria-hidden /> : <CheckIcon />} |
| 255 | </Button> |
| 256 | </ShortcutTooltip> |
| 257 | ) : null} |
| 258 | </VoiceInputPill> |
| 259 | ); |
| 260 | } |