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

GlobalVoiceInputControl.tsx

237 lines9.3 KB

1import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
2import { useEffect, useRef, useState, type ReactNode, type RefObject } from "react";
3import { Linking, Platform, Pressable, useWindowDimensions, View } from "react-native";
4import Animated, {
5 Easing,
6 Extrapolation,
7 FadeIn,
8 interpolate,
9 ReduceMotion,
10 useAnimatedStyle,
11 useSharedValue,
12 withTiming,
13} from "react-native-reanimated";
14import { useSafeAreaInsets } from "react-native-safe-area-context";
15import { FullWindowOverlay } from "react-native-screens";
16​
17import { AppText as Text } from "../../components/ui/AppText";
18import { SymbolView } from "../../components/ui/AppSymbol";
19import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing";
20import { cn } from "../../lib/cn";
21import { CompactVoiceWaveform, DictationElapsedTime } from "./ComposerDictationControl";
22import { useGlobalVoiceInput } from "./VoiceInputProvider";
23import { resolveVoiceComposerPresentation } from "./voiceInputPresentation";
24​
25const PILL_HEIGHT = 36;
26const COLLAPSED_WIDTH = 64;
27const EXPANDED_MAX_WIDTH = 280;
28const IOS_BOTTOM_CHROME_CLEARANCE = 64;
29const MORPH_TIMING = {
30 duration: 260,
31 easing: Easing.out(Easing.cubic),
32 reduceMotion: ReduceMotion.System,
33} as const;
34const ENTERING = FadeIn.duration(180).reduceMotion(ReduceMotion.System);
35​
36export function GlobalVoiceInputControl(props: { readonly children: ReactNode }) {
37 const voice = useGlobalVoiceInput();
38 const collapseRef = useRef<(() => void) | null>(null);
39 const presentation = resolveVoiceComposerPresentation(voice.state, voice.elapsedSeconds);
40 const visible =
41 presentation.statusLabel !== null &&
42 !(voice.ownerKey && voice.focusedOwners.has(voice.ownerKey));
43 const pill = visible ? <EdgeDictationPill collapseRef={collapseRef} /> : null;
44 return (
45 <>
46 <View
47 className="flex-1"
48 onStartShouldSetResponderCapture={() => {
49 collapseRef.current?.();
50 return false;
51 }}
52 >
53 {props.children}
54 </View>
55 {pill && Platform.OS === "ios" ? <FullWindowOverlay>{pill}</FullWindowOverlay> : pill}
56 </>
57 );
59​
60function EdgeDictationPill(props: { readonly collapseRef: RefObject<(() => void) | null> }) {
61 const voice = useGlobalVoiceInput();
62 const insets = useSafeAreaInsets();
63 const { width } = useWindowDimensions();
64 const { fabSize } = useAndroidControlSizing();
65 const [expanded, setExpanded] = useState(false);
66 const progress = useSharedValue(0);
67 useEffect(() => {
68 progress.value = withTiming(expanded ? 1 : 0, MORPH_TIMING);
69 }, [expanded, progress]);
70 const { collapseRef } = props;
71 useEffect(() => {
72 if (!expanded) return;
73 collapseRef.current = () => setExpanded(false);
74 return () => {
75 collapseRef.current = null;
76 };
77 }, [collapseRef, expanded]);
78​
79 const expandedWidth = Math.min(width - insets.left - insets.right - 32, EXPANDED_MAX_WIDTH);
80 const pillStyle = useAnimatedStyle(() => ({
81 width: interpolate(progress.value, [0, 1], [COLLAPSED_WIDTH, expandedWidth]),
82 }));
83 const collapsedStyle = useAnimatedStyle(() => ({
84 opacity: interpolate(progress.value, [0, 0.4], [1, 0], Extrapolation.CLAMP),
85 }));
86 const expandedStyle = useAnimatedStyle(() => ({
87 opacity: interpolate(progress.value, [0.3, 1], [0, 1], Extrapolation.CLAMP),
88 }));
89​
90 const bottom =
91 Platform.OS === "android"
92 ? Math.max(insets.bottom, 16) + 16 + fabSize + 12
93 : Math.max(insets.bottom, 12) + IOS_BOTTOM_CHROME_CLEARANCE;
94​
95 const presentation = resolveVoiceComposerPresentation(voice.state, voice.elapsedSeconds);
96 const phase = voice.state.phase;
97 const isError = phase === "error";
98 const openSettings = isError && voice.state.errorAction === "settings";
99 const label = voice.label ?? "Draft";
100​
101 return (
102 <View pointerEvents="box-none" className="absolute inset-0">
103 <Animated.View
104 entering={ENTERING}
105 className="absolute rounded-l-full border border-r-0 border-border bg-card shadow-md shadow-black/10"
106 style={[{ bottom, right: insets.right, height: PILL_HEIGHT }, pillStyle]}
107 >
108 <View className="flex-1 overflow-hidden rounded-l-full">
109 <Animated.View
110 accessibilityElementsHidden={expanded}
111 importantForAccessibility={expanded ? "no-hide-descendants" : "auto"}
112 pointerEvents={expanded ? "none" : "auto"}
113 className="absolute bottom-0 right-0 top-0"
114 style={[{ width: COLLAPSED_WIDTH }, collapsedStyle]}
115 >
116 <Pressable
117 accessibilityLabel={`${presentation.statusLabel}, dictating into ${label}`}
118 accessibilityHint="Shows dictation controls"
119 accessibilityRole="button"
120 className="flex-1 flex-row items-center justify-center gap-1.5 active:opacity-70"
121 hitSlop={{ top: 6, bottom: 6, left: 6 }}
122 onPress={() => setExpanded(true)}
123 >
124 {isError ? (
125 <SymbolView
126 name="exclamationmark.circle"
127 size={16}
128 tintColorClassName="accent-danger-foreground"
129 type="monochrome"
130 />
131 ) : phase === "recording" ? (
132 <>
133 <View className="size-2 rounded-full bg-danger-foreground" />
134 <DictationElapsedTime
135 className="text-foreground"
136 seconds={voice.elapsedSeconds}
137 />
138 </>
139 ) : (
140 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
141 )}
142 </Pressable>
143 </Animated.View>
144​
145 <Animated.View
146 accessibilityElementsHidden={!expanded}
147 importantForAccessibility={expanded ? "auto" : "no-hide-descendants"}
148 pointerEvents={expanded ? "auto" : "none"}
149 className="absolute bottom-0 right-0 top-0 flex-row items-center gap-1.5 pl-1 pr-1.5"
150 style={[{ width: expandedWidth }, expandedStyle]}
151 >
152 <Pressable
153 accessibilityLabel={isError ? "Dismiss voice input error" : "Cancel dictation"}
154 accessibilityRole="button"
155 className="size-[30px] items-center justify-center active:opacity-70"
156 onPress={voice.cancel}
157 >
158 <SymbolView
159 name="xmark"
160 size={13}
161 tintColorClassName="accent-icon-muted"
162 type="monochrome"
163 />
164 </Pressable>
165 <Pressable
166 accessibilityLabel={isError ? (presentation.statusLabel ?? label) : label}
167 accessibilityHint="Hides dictation controls"
168 accessibilityRole="button"
169 className="min-w-0 flex-1 justify-center self-stretch active:opacity-70"
170 onPress={() => setExpanded(false)}
171 >
172 <Text
173 className={cn(
174 "text-xs",
175 isError ? "text-danger-foreground" : "font-t3-medium text-foreground",
176 )}
177 numberOfLines={1}
178 >
179 {isError ? presentation.statusLabel : label}
180 </Text>
181 </Pressable>
182 {phase === "recording" ? (
183 <>
184 <CompactVoiceWaveform audioLevels={voice.audioLevels} />
185 <DictationElapsedTime
186 className="text-foreground-muted"
187 seconds={voice.elapsedSeconds}
188 />
189 <Pressable
190 accessibilityLabel="Finish dictation"
191 accessibilityRole="button"
192 className="size-[30px] items-center justify-center active:opacity-70"
193 onPress={voice.stop}
194 >
195 <View className="size-[24px] items-center justify-center rounded-full bg-primary">
196 <SymbolView
197 name="checkmark"
198 size={12}
199 weight="semibold"
200 tintColorClassName="accent-primary-foreground"
201 type="monochrome"
202 />
203 </View>
204 </Pressable>
205 </>
206 ) : isError ? (
207 voice.isAvailable ? (
208 <Pressable
209 accessibilityLabel={openSettings ? "Open microphone settings" : "Retry dictation"}
210 accessibilityRole="button"
211 className="size-[30px] items-center justify-center active:opacity-70"
212 onPress={() => {
213 if (!openSettings) {
214 void voice.session.retry();
215 return;
216 }
217 voice.cancel();
218 void Linking.openSettings();
219 }}
220 >
221 <SymbolView name="mic" size={16} tintColorClassName="accent-icon" />
222 </Pressable>
223 ) : null
224 ) : (
225 <View
226 accessibilityLabel={presentation.statusLabel ?? undefined}
227 className="size-[30px] items-center justify-center"
228 >
229 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
230 </View>
231 )}
232 </Animated.View>
233 </View>
234 </Animated.View>
235 </View>
236 );