@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
tsx 468 lines 14.8 KB
Raw
1 import type { VoiceInputPhase, VoiceInputState } from "@t3tools/client-runtime/voice-input";
2 import { useGT } from "gt-react-native";
3 import { memo, useState, type ReactNode } from "react";
4 import { Linking, Pressable, Text as SystemText, View, type LayoutChangeEvent } from "react-native";
5 import Animated, {
6 Easing,
7 ReduceMotion,
8 useAnimatedStyle,
9 useDerivedValue,
10 useReducedMotion,
11 withTiming,
12 type EntryExitAnimationFunction,
13 type SharedValue,
14 } from "react-native-reanimated";
15
16 import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
17 import { AppText as Text } from "../../components/ui/AppText";
18 import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol";
19 import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing";
20 import { cn } from "../../lib/cn";
21 import type { VoiceComposerPresentation } from "./voiceInputPresentation";
22 import { VOICE_WAVEFORM_SAMPLE_COUNT } from "./voiceInputMetering";
23
24 const DICTATION_TIMING = {
25 duration: 220,
26 easing: Easing.out(Easing.cubic),
27 reduceMotion: ReduceMotion.System,
28 } as const;
29 const TOOLBAR_FLIP_TIMING = {
30 duration: 260,
31 easing: Easing.inOut(Easing.cubic),
32 reduceMotion: ReduceMotion.System,
33 } as const;
34 const TOOLBAR_HALF_HEIGHT = 22;
35 const TOOLBAR_PERSPECTIVE = 600;
36
37 function toolbarFlip(fromDegrees: number, toDegrees: number): EntryExitAnimationFunction {
38 return () => {
39 "worklet";
40 const fromRadians = (fromDegrees * Math.PI) / 180;
41 const toRadians = (toDegrees * Math.PI) / 180;
42 const fromSine = Math.sin(fromRadians);
43 const toSine = Math.sin(toRadians);
44 return {
45 initialValues: {
46 opacity: fromDegrees === 0 ? 1 : 0,
47 transform: [
48 { perspective: TOOLBAR_PERSPECTIVE },
49 { translateY: -TOOLBAR_HALF_HEIGHT * fromSine },
50 { rotateX: `${fromDegrees}deg` },
51 ],
52 },
53 animations: {
54 opacity: withTiming(toDegrees === 0 ? 1 : 0, TOOLBAR_FLIP_TIMING),
55 transform: [
56 { perspective: TOOLBAR_PERSPECTIVE },
57 {
58 translateY: withTiming(-TOOLBAR_HALF_HEIGHT * toSine, {
59 ...TOOLBAR_FLIP_TIMING,
60 easing: (time) => {
61 const angle =
62 fromRadians + (toRadians - fromRadians) * TOOLBAR_FLIP_TIMING.easing(time);
63 return (Math.sin(angle) - fromSine) / (toSine - fromSine);
64 },
65 }),
66 },
67 { rotateX: withTiming(`${toDegrees}deg`, TOOLBAR_FLIP_TIMING) },
68 ],
69 },
70 };
71 };
72 }
73
74 const DRAFT_TOOLBAR_ENTERING = toolbarFlip(90, 0);
75 const DRAFT_TOOLBAR_EXITING = toolbarFlip(0, 90);
76 const DICTATION_TOOLBAR_ENTERING = toolbarFlip(-90, 0);
77 const DICTATION_TOOLBAR_EXITING = toolbarFlip(0, -90);
78 const WAVEFORM_BAR_HEIGHT = 32;
79 const WAVEFORM_MIN_BAR_HEIGHT = 2;
80 const WAVEFORM_BAR_SPACING = 5;
81 const WAVEFORM_TIMING = {
82 duration: 100,
83 easing: Easing.out(Easing.quad),
84 reduceMotion: ReduceMotion.System,
85 } as const;
86
87 export function ComposerDictationDraftContent(props: {
88 readonly children: ReactNode;
89 readonly className?: string;
90 readonly compact: boolean;
91 readonly hidden: boolean;
92 }) {
93 const hidden = props.hidden;
94 const rotation = useDerivedValue(() => withTiming(hidden ? 1 : 0, TOOLBAR_FLIP_TIMING));
95 const compact = props.compact;
96 const animatedStyle = useAnimatedStyle(() => ({
97 opacity: compact ? 1 - rotation.value : 1,
98 transform: compact
99 ? [
100 { perspective: TOOLBAR_PERSPECTIVE },
101 { translateY: -TOOLBAR_HALF_HEIGHT * Math.sin((rotation.value * Math.PI) / 2) },
102 { rotateX: `${rotation.value * 90}deg` },
103 ]
104 : [],
105 }));
106
107 return (
108 <Animated.View
109 accessibilityElementsHidden={props.hidden}
110 importantForAccessibility={props.hidden ? "no-hide-descendants" : "auto"}
111 collapsable={false}
112 className={props.className}
113 pointerEvents={props.hidden ? "none" : "auto"}
114 style={[animatedStyle, { backfaceVisibility: "hidden" }]}
115 >
116 {props.children}
117 </Animated.View>
118 );
119 }
120
121 export function ComposerDictationToolbar(props: {
122 readonly children: ReactNode;
123 readonly showsDictation: boolean;
124 readonly visible?: boolean;
125 }) {
126 // Android can drop the reduced-motion flip's final frame, leaving the toolbar invisible.
127 const reducedMotion = useReducedMotion();
128 const entering = props.showsDictation ? DICTATION_TOOLBAR_ENTERING : DRAFT_TOOLBAR_ENTERING;
129 const exiting = props.showsDictation ? DICTATION_TOOLBAR_EXITING : DRAFT_TOOLBAR_EXITING;
130
131 return (
132 <View className="relative h-[44px] overflow-hidden">
133 {props.visible !== false ? (
134 <Animated.View
135 key={props.showsDictation ? "dictation" : "draft"}
136 className="absolute inset-0"
137 entering={reducedMotion ? undefined : entering}
138 exiting={reducedMotion ? undefined : exiting}
139 style={{ backfaceVisibility: "hidden" }}
140 >
141 {props.children}
142 </Animated.View>
143 ) : null}
144 </View>
145 );
146 }
147
148 const WaveformBar = memo(function WaveformBar(props: {
149 readonly audioLevels: SharedValue<ReadonlyArray<number>>;
150 readonly sampleIndex: number;
151 readonly height?: number;
152 }) {
153 const { audioLevels, sampleIndex } = props;
154 const height = props.height ?? WAVEFORM_BAR_HEIGHT;
155 const animatedStyle = useAnimatedStyle(() => {
156 const level = audioLevels.value[sampleIndex] ?? 0;
157 return {
158 opacity: withTiming(0.22 + level * 0.78, WAVEFORM_TIMING),
159 transform: [
160 {
161 scaleY: withTiming(
162 (WAVEFORM_MIN_BAR_HEIGHT + level * (height - WAVEFORM_MIN_BAR_HEIGHT)) / height,
163 WAVEFORM_TIMING,
164 ),
165 },
166 ],
167 };
168 });
169
170 return (
171 <Animated.View
172 className="w-0.5 rounded-full bg-foreground"
173 style={[{ height }, animatedStyle]}
174 />
175 );
176 });
177
178 const COMPACT_WAVEFORM_BAR_COUNT = 8;
179 const COMPACT_WAVEFORM_BAR_HEIGHT = 14;
180
181 export const CompactVoiceWaveform = memo(function CompactVoiceWaveform(props: {
182 readonly audioLevels: SharedValue<ReadonlyArray<number>>;
183 }) {
184 return (
185 <View
186 accessibilityElementsHidden
187 importantForAccessibility="no-hide-descendants"
188 className="flex-row items-center gap-0.5"
189 style={{ height: COMPACT_WAVEFORM_BAR_HEIGHT }}
190 >
191 {Array.from({ length: COMPACT_WAVEFORM_BAR_COUNT }, (_, index) => (
192 <WaveformBar
193 key={index}
194 audioLevels={props.audioLevels}
195 height={COMPACT_WAVEFORM_BAR_HEIGHT}
196 sampleIndex={VOICE_WAVEFORM_SAMPLE_COUNT - COMPACT_WAVEFORM_BAR_COUNT + index}
197 />
198 ))}
199 </View>
200 );
201 });
202
203 const VoiceWaveform = memo(function VoiceWaveform(props: {
204 readonly audioLevels: SharedValue<ReadonlyArray<number>>;
205 }) {
206 const [barCount, setBarCount] = useState(0);
207 const handleLayout = (event: LayoutChangeEvent) => {
208 setBarCount(
209 Math.max(
210 1,
211 Math.min(
212 VOICE_WAVEFORM_SAMPLE_COUNT,
213 Math.floor(event.nativeEvent.layout.width / WAVEFORM_BAR_SPACING),
214 ),
215 ),
216 );
217 };
218
219 return (
220 <View
221 accessibilityElementsHidden
222 importantForAccessibility="no-hide-descendants"
223 className="min-w-0 flex-1 flex-row items-center justify-between overflow-hidden"
224 style={{ height: WAVEFORM_BAR_HEIGHT }}
225 onLayout={handleLayout}
226 >
227 {Array.from({ length: barCount }, (_, index) => (
228 <WaveformBar
229 key={index}
230 audioLevels={props.audioLevels}
231 sampleIndex={VOICE_WAVEFORM_SAMPLE_COUNT - barCount + index}
232 />
233 ))}
234 </View>
235 );
236 });
237
238 /** Use system tabular figures; DM Sans lacks them and would jitter. */
239 export function DictationElapsedTime(props: {
240 readonly seconds: number;
241 readonly className?: string;
242 }) {
243 return (
244 <SystemText
245 className={cn("text-xs", props.className)}
246 numberOfLines={1}
247 style={{ fontVariant: ["tabular-nums"] }}
248 >
249 {Math.floor(props.seconds / 60)}:{String(props.seconds % 60).padStart(2, "0")}
250 </SystemText>
251 );
252 }
253
254 function VoiceActionButton(props: {
255 readonly size?: "regular" | "large";
256 readonly accessibilityLabel: string;
257 readonly disabled?: boolean;
258 readonly icon: AppSymbolName;
259 readonly loading?: boolean;
260 readonly onPress: () => void;
261 readonly variant?: "plain" | "primary";
262 }) {
263 const { scale } = useAndroidControlSizing();
264 const variant = props.variant ?? "plain";
265 const loading = props.loading === true;
266 const large = props.size === "large";
267 const circleSize = Math.round((large ? 44 : 30) * scale);
268 const iconSize = Math.round(
269 (variant === "primary" ? (large ? 20 : 16) : large ? 24 : 20) * scale,
270 );
271 const primaryStyle = useAnimatedStyle(() => ({
272 opacity: withTiming(loading ? 0 : 1, DICTATION_TIMING),
273 }));
274
275 return (
276 <Pressable
277 accessibilityLabel={props.accessibilityLabel}
278 accessibilityRole="button"
279 accessibilityState={{ busy: props.loading, disabled: props.disabled }}
280 className="size-[44px] shrink-0 items-center justify-center active:opacity-70"
281 disabled={props.disabled}
282 onPress={props.onPress}
283 style={{
284 opacity: props.disabled && !props.loading ? 0.4 : 1,
285 // Matches ComposerActionButton so the circle stays concentric with the large composer corner.
286 // An undefined width would still override the class's 44px width.
287 ...(variant === "primary" && large ? { width: 62 } : null),
288 }}
289 >
290 <View
291 className={cn(
292 "items-center justify-center",
293 variant === "primary" ? "rounded-full bg-subtle" : "size-[44px]",
294 )}
295 style={variant === "primary" ? { width: circleSize, height: circleSize } : undefined}
296 >
297 {variant === "primary" ? (
298 <Animated.View
299 className="absolute inset-0 rounded-full bg-primary"
300 style={primaryStyle}
301 />
302 ) : null}
303 <View className="absolute inset-0 items-center justify-center">
304 {props.loading ? (
305 <AppActivityIndicator size="small" colorClassName="accent-icon-muted" />
306 ) : (
307 <SymbolView
308 name={props.icon}
309 size={iconSize}
310 weight={variant === "primary" ? "semibold" : "regular"}
311 tintColorClassName={
312 variant === "primary" ? "accent-primary-foreground" : "accent-icon"
313 }
314 type="monochrome"
315 />
316 )}
317 </View>
318 </View>
319 </Pressable>
320 );
321 }
322
323 export function ComposerDictationError(props: {
324 readonly error: string | null;
325 readonly onDismiss: () => void;
326 }) {
327 const gt = useGT();
328 if (!props.error) return null;
329 return (
330 <View className="min-w-0 flex-row items-center gap-2 px-4 py-3">
331 <Text accessibilityRole="alert" className="min-w-0 flex-1 text-sm text-danger-foreground">
332 {props.error}
333 </Text>
334 <Pressable
335 accessibilityLabel={gt("Dismiss voice input error")}
336 accessibilityRole="button"
337 className="size-7 items-center justify-center active:opacity-70"
338 hitSlop={8}
339 onPress={props.onDismiss}
340 >
341 <SymbolView
342 name="xmark"
343 size={12}
344 tintColorClassName="accent-icon-muted"
345 type="monochrome"
346 />
347 </Pressable>
348 </View>
349 );
350 }
351
352 export function ComposerDictationStatus(props: {
353 readonly audioLevels: SharedValue<ReadonlyArray<number>>;
354 readonly elapsedSeconds: number;
355 readonly phase: VoiceInputPhase;
356 readonly presentation: VoiceComposerPresentation;
357 }) {
358 const recording = props.phase === "recording";
359 const waveformStyle = useAnimatedStyle(() => ({
360 opacity: withTiming(recording ? 1 : 0, DICTATION_TIMING),
361 }));
362 const labelStyle = useAnimatedStyle(() => ({
363 opacity: withTiming(recording ? 0 : 1, DICTATION_TIMING),
364 }));
365
366 if (!props.presentation.statusLabel || props.presentation.statusKind === "error") return null;
367 return (
368 <View className="relative h-11 min-w-0 flex-1 justify-center">
369 <View
370 accessible
371 accessibilityLabel={props.presentation.statusLabel}
372 accessibilityLiveRegion={props.phase === "recording" ? "none" : "polite"}
373 className="h-11"
374 >
375 <Animated.View
376 className="absolute inset-0 min-w-0 flex-row items-center gap-2 px-1"
377 style={waveformStyle}
378 >
379 <VoiceWaveform audioLevels={props.audioLevels} />
380 <DictationElapsedTime className="text-foreground-muted" seconds={props.elapsedSeconds} />
381 </Animated.View>
382 <Animated.View className="absolute inset-0 justify-center px-2" style={labelStyle}>
383 <Text className="text-center text-sm text-foreground-muted" numberOfLines={1}>
384 {props.presentation.statusLabel}
385 </Text>
386 </Animated.View>
387 </View>
388 </View>
389 );
390 }
391
392 export function ComposerDictationCancelAction(props: {
393 readonly size?: "regular" | "large";
394 readonly presentation: VoiceComposerPresentation;
395 readonly onCancel: () => void;
396 }) {
397 const gt = useGT();
398 if (props.presentation.leadingAction !== "cancel") return null;
399 return (
400 <VoiceActionButton
401 size={props.size}
402 accessibilityLabel={gt("Cancel dictation")}
403 icon="xmark"
404 onPress={props.onCancel}
405 />
406 );
407 }
408
409 export function ComposerDictationPrimaryAction(props: {
410 readonly size?: "regular" | "large";
411 readonly state: VoiceInputState;
412 readonly presentation: VoiceComposerPresentation;
413 readonly isAvailable: boolean;
414 readonly disabled?: boolean;
415 readonly onStart: () => void;
416 readonly onConfirm: () => void;
417 readonly onCancel: () => void;
418 }) {
419 const gt = useGT();
420 if (props.presentation.trailingAction === "confirm") {
421 return (
422 <VoiceActionButton
423 size={props.size}
424 accessibilityLabel={
425 props.presentation.confirmationEnabled
426 ? gt("Finish dictation")
427 : (props.presentation.statusLabel ?? gt("Preparing voice input"))
428 }
429 disabled={!props.presentation.confirmationEnabled}
430 icon="checkmark"
431 loading={!props.presentation.confirmationEnabled}
432 onPress={props.onConfirm}
433 variant="primary"
434 />
435 );
436 }
437
438 return <ComposerDictationStartAction {...props} />;
439 }
440
441 export function ComposerDictationStartAction(props: {
442 readonly size?: "regular" | "large";
443 readonly state: VoiceInputState;
444 readonly isAvailable: boolean;
445 readonly disabled?: boolean;
446 readonly onStart: () => void;
447 readonly onCancel: () => void;
448 }) {
449 const gt = useGT();
450 if (!props.isAvailable) return null;
451 const openSettings = props.state.phase === "error" && props.state.errorAction === "settings";
452 return (
453 <VoiceActionButton
454 size={props.size}
455 accessibilityLabel={openSettings ? gt("Open microphone settings") : gt("Start dictation")}
456 disabled={props.disabled}
457 icon="mic"
458 onPress={
459 openSettings
460 ? () => {
461 props.onCancel();
462 void Linking.openSettings();
463 }
464 : props.onStart
465 }
466 />
467 );
468 }