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

voiceInputPresentation.ts

72 lines2.1 KB

1import type { VoiceInputState } from "@t3tools/client-runtime/voice-input";
2import { msg } from "gt-react-native";
3​
4import { m, type MessageResolver } from "../../i18n/messages";
5​
6export type VoiceComposerPresentation = {
7 readonly leadingAction: "cancel" | null;
8 readonly trailingAction: "mic" | "confirm";
9 readonly showsSend: boolean;
10 readonly statusKind: "active" | "error" | null;
11 readonly statusLabel: string | null;
12 readonly confirmationEnabled: boolean;
13};
14​
15export function resolveVoiceComposerPresentation(
16 state: VoiceInputState,
17 elapsedSeconds: number,
18 resolve: MessageResolver = m,
19): VoiceComposerPresentation {
20 switch (state.phase) {
21 case "idle":
22 return {
23 leadingAction: null,
24 trailingAction: "mic",
25 showsSend: true,
26 statusKind: null,
27 statusLabel: null,
28 confirmationEnabled: false,
29 };
30 case "error":
31 return {
32 leadingAction: null,
33 trailingAction: "mic",
34 showsSend: true,
35 statusKind: "error",
36 statusLabel: state.error,
37 confirmationEnabled: false,
38 };
39 case "preparing":
40 return {
41 leadingAction: "cancel",
42 trailingAction: "confirm",
43 showsSend: false,
44 statusKind: "active",
45 statusLabel: resolve(msg({ context: "voice input status" })`Preparing`),
46 confirmationEnabled: false,
47 };
48 case "recording": {
49 const seconds = Math.max(0, Math.floor(elapsedSeconds));
50 return {
51 leadingAction: "cancel",
52 trailingAction: "confirm",
53 showsSend: false,
54 statusKind: "active",
55 statusLabel: resolve(
56 msg`Recording ${{ time: `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}` }}`,
57 ),
58 confirmationEnabled: true,
59 };
60 }
61 case "transcribing":
62 case "post-processing":
63 return {
64 leadingAction: "cancel",
65 trailingAction: "confirm",
66 showsSend: false,
67 statusKind: "active",
68 statusLabel: resolve(msg({ context: "voice input status" })`Transcribing`),
69 confirmationEnabled: false,
70 };
71 }