@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 517 lines 19.2 KB
Raw
1 import { msg, useGT, useMessages } from "gt-react-native";
2 import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
3 import { useAtomSet, useAtomValue } from "@effect/atom-react";
4 import { AsyncResult } from "effect/reactivity";
5 import { getRecordingPermissionsAsync, requestRecordingPermissionsAsync } from "expo-audio";
6 import { useState } from "react";
7 import { Linking, Platform, useWindowDimensions, View } from "react-native";
8 import Reanimated, { ReduceMotion, useAnimatedStyle, withTiming } from "react-native-reanimated";
9 import { useSafeAreaInsets } from "react-native-safe-area-context";
10
11 import { type AppSymbolName } from "../../components/ui/AppSymbol";
12 import { AppText as Text } from "../../components/ui/AppText";
13 import { cn } from "../../lib/cn";
14 import { m } from "../../i18n/messages";
15 import { makePermissionStatusAtom } from "../../lib/permissionStatusAtom";
16 import {
17 isMicrophoneDevice,
18 type MicrophoneEntry,
19 type MicrophoneKind,
20 microphoneEntryKey,
21 } from "../../lib/microphonePriority";
22 import { mobilePreferencesAtom, saveMobilePreferences } from "../../state/preferences";
23 import { useAtomCommand } from "../../state/use-atom-command";
24 import { ListRowLayout } from "../../components/ui/ListRowLayout";
25 import { ListChoiceRow } from "../../components/ui/ListChoiceRow";
26 import * as Dialog from "../../components/ui/Dialog";
27 import { useEnvironments, type EnvironmentPresentation } from "../../state/environments";
28 import {
29 chooseVoiceDestination,
30 cloudVoiceConnectionAtom,
31 voiceConnectionAtom,
32 voiceGrantAtom,
33 hasEnvironmentVoiceCapture,
34 } from "../voice-input/voiceDestination";
35 import { LocalModelChoice } from "./VoiceModelSection";
36 import { CloudVoiceChoice } from "./CloudVoiceSection";
37 import type { VoiceGrant } from "../voice-input/voiceGrantStorage";
38 import type { EnvironmentId } from "@t3tools/contracts";
39 import { ThreadSwipeable } from "../home/thread-swipe-actions";
40 import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow";
41 import { SettingsDragHandle } from "./components/SettingsDragHandle";
42 import { SettingsScreen } from "./components/SettingsScreen";
43 import { SettingsSection } from "./components/SettingsSection";
44 import {
45 deviceMicrophoneOrder,
46 localVoiceTranscriberAtom,
47 } from "../voice-input/VoiceInputProvider";
48
49 type MicrophonePermission = "checking" | "granted" | "ask" | "denied";
50
51 function toMicrophonePermission(response: {
52 readonly granted: boolean;
53 readonly canAskAgain: boolean;
54 }): MicrophonePermission {
55 return response.granted ? "granted" : response.canAskAgain ? "ask" : "denied";
56 }
57
58 const readMicrophonePermission = () =>
59 getRecordingPermissionsAsync().then(toMicrophonePermission, (): MicrophonePermission => "ask");
60
61 const microphonePermissionAtom = makePermissionStatusAtom<MicrophonePermission>({
62 label: "mobile:microphone-permission",
63 initial: "checking",
64 read: readMicrophonePermission,
65 });
66
67 function useMicrophonePermission() {
68 const permission = useAtomValue(microphonePermissionAtom);
69 const setPermission = useAtomSet(microphonePermissionAtom);
70 const request = () =>
71 void requestRecordingPermissionsAsync()
72 .then(toMicrophonePermission, readMicrophonePermission)
73 .then(setPermission);
74 return { permission, request };
75 }
76
77 const KINDS: Record<
78 MicrophoneKind,
79 {
80 readonly label: string;
81 readonly row: string;
82 readonly rowDescription: string;
83 readonly icon: AppSymbolName;
84 }
85 > = {
86 wired: {
87 label: msg("Wired", { $context: "microphone connection type" }),
88 row: msg("Other wired"),
89 rowDescription: msg("Headsets and USB microphones not listed here"),
90 icon: "cable.connector",
91 },
92 bluetooth: {
93 label: msg("Bluetooth"),
94 row: msg("Other Bluetooth"),
95 rowDescription:
96 Platform.OS === "android"
97 ? msg("Earbuds, headsets, and cars, including Android Auto, not listed here")
98 : msg("AirPods, headsets, and cars not listed here"),
99 icon: "headphones",
100 },
101 builtIn: {
102 label: msg("Built-in", { $context: "microphone connection type" }),
103 row: msg("Built-in microphone"),
104 rowDescription: msg("The microphone on this device"),
105 icon: "iphone",
106 },
107 carPlay: {
108 label: "CarPlay",
109 row: "CarPlay",
110 rowDescription: msg("The car’s microphone"),
111 icon: "car",
112 },
113 };
114
115 const UNAVAILABLE_EXPLANATION =
116 Platform.OS === "android"
117 ? msg(
118 "Dictation on this device needs Android 12 or later and Google speech services that support your language. Your keyboard’s voice typing is separate.",
119 )
120 : msg(
121 "Dictation on this device needs iOS 26 or later, a supported device, and a supported language. Your keyboard’s dictation is separate.",
122 );
123
124 function saveDestination(id: EnvironmentId | null) {
125 try {
126 chooseVoiceDestination(id);
127 } catch {
128 Dialog.alert(
129 m(msg("Could not save transcription choice")),
130 m(
131 msg(
132 "Dictation on an environment is off for this session. Save again before closing the app, or your previous choice may return when it restarts.",
133 ),
134 ),
135 );
136 }
137 }
138
139 function EnvironmentChoice({
140 environment,
141 selected,
142 onCloudOnly,
143 }: {
144 environment: EnvironmentPresentation;
145 selected: boolean;
146 /** Called instead of a local grant when the environment only offers cloud transcription. */
147 onCloudOnly: () => void;
148 }) {
149 const gt = useGT();
150 const local = useAtomValue(voiceConnectionAtom(environment.environmentId)) !== null;
151 const cloud = useAtomValue(cloudVoiceConnectionAtom(environment.environmentId)) !== null;
152 const usable = local || cloud;
153 return (
154 <ListChoiceRow
155 label={environment.label}
156 selected={selected}
157 disabled={!usable}
158 description={
159 environment.connection.phase !== "connected"
160 ? gt("Not connected")
161 : !usable
162 ? gt("Transcription unavailable")
163 : (environment.displayUrl ?? gt("Send finished recordings to this environment."))
164 }
165 separated
166 onPress={() => {
167 if (selected) return;
168 if (!local) return onCloudOnly();
169 Dialog.confirm({
170 title: gt("Send microphone audio to {environment}?", { environment: environment.label }),
171 message: gt(
172 "Finished recordings will be sent to {destination} for transcription. You can switch back to This device at any time.",
173 { destination: environment.displayUrl ?? environment.label },
174 ),
175 confirmText: gt("Use environment"),
176 onConfirm: () => saveDestination(environment.environmentId),
177 });
178 }}
179 />
180 );
181 }
182
183 /** How the selected environment transcribes: its own model or a cloud provider. */
184 function EnvironmentMethodSection({
185 environment,
186 grant,
187 cloudOpen,
188 onCloudOpenChange,
189 }: {
190 environment: EnvironmentPresentation;
191 grant: VoiceGrant | null;
192 cloudOpen: boolean;
193 onCloudOpenChange: (open: boolean) => void;
194 }) {
195 const gt = useGT();
196 const id = environment.environmentId;
197 const destination = grant?.destination.environmentId === id ? grant.destination : null;
198 const cloudCapable =
199 environment.serverConfig?.environment.capabilities.cloudVoiceTranscription === true ||
200 destination?.kind === "cloud";
201 return (
202 <SettingsSection title={gt("Method on {environment}", { environment: environment.label })}>
203 <LocalModelChoice
204 environmentId={id}
205 label={environment.label}
206 selected={destination?.kind === "environment"}
207 // The user already agreed to send audio to this environment.
208 onSelect={() => saveDestination(id)}
209 />
210 {cloudCapable ? (
211 <CloudVoiceChoice
212 environment={environment}
213 active={destination?.kind === "cloud" ? destination : null}
214 open={cloudOpen}
215 onOpenChange={onCloudOpenChange}
216 />
217 ) : null}
218 </SettingsSection>
219 );
220 }
221
222 export function SettingsVoiceRouteScreen() {
223 const gt = useGT();
224 const m = useMessages();
225 const transcriber = useAtomValue(localVoiceTranscriberAtom);
226 const grant = useAtomValue(voiceGrantAtom);
227 const { environments, isReady } = useEnvironments();
228 const grantEnvironment = grant
229 ? environments.find(
230 (environment) => environment.environmentId === grant.destination.environmentId,
231 )
232 : undefined;
233 // An environment whose cloud sheet is open; it may not hold the grant yet.
234 const [cloudSheet, setCloudSheet] = useState<EnvironmentId | null>(null);
235 // A sheet whose environment lost cloud transcription is gone, so its pending choice is too.
236 const sheetEnvironment = environments.find(
237 (environment) =>
238 environment.environmentId === cloudSheet &&
239 environment.serverConfig?.environment.capabilities.cloudVoiceTranscription === true,
240 );
241 const methodEnvironment = sheetEnvironment ?? grantEnvironment;
242 const insets = useSafeAreaInsets();
243 const preferencesResult = useAtomValue(mobilePreferencesAtom);
244 const savePreferences = useAtomCommand(saveMobilePreferences);
245 const ready = AsyncResult.isSuccess(preferencesResult) && !preferencesResult.waiting;
246 const entries = deviceMicrophoneOrder(
247 AsyncResult.isSuccess(preferencesResult) ? preferencesResult.value.microphones : undefined,
248 );
249 const { permission, request: requestPermission } = useMicrophonePermission();
250 const [drag, setDrag] = useState<{ readonly key: string; readonly translation: number } | null>(
251 null,
252 );
253 const [rowHeight, setRowHeight] = useState(0);
254 // Each drop remounts the rows so the new order and the cleared offsets land in one frame.
255 const [drops, setDrops] = useState(0);
256
257 // Transforms apply to the stored order, so a device recorded meanwhile is kept.
258 const move = (key: string, steps: number) => {
259 void savePreferences({
260 transform: (current) => {
261 const next = [...deviceMicrophoneOrder(current.microphones)];
262 const from = next.findIndex((entry) => microphoneEntryKey(entry) === key);
263 const to = Math.min(next.length - 1, Math.max(0, from + steps));
264 if (from === -1 || from === to) return {};
265 const [moved] = next.splice(from, 1);
266 next.splice(to, 0, moved!);
267 return { microphones: next };
268 },
269 });
270 };
271 const forget = (key: string) => {
272 void savePreferences({
273 transform: (current) => ({
274 microphones: deviceMicrophoneOrder(current.microphones).filter(
275 (entry) => microphoneEntryKey(entry) !== key,
276 ),
277 }),
278 });
279 };
280 const dropSteps = (from: number, translation: number) =>
281 rowHeight === 0
282 ? 0
283 : Math.min(entries.length - 1, Math.max(0, from + Math.round(translation / rowHeight))) -
284 from;
285
286 const dragFrom =
287 drag === null ? -1 : entries.findIndex((entry) => microphoneEntryKey(entry) === drag.key);
288 const dragTo = drag === null ? -1 : dragFrom + dropSteps(dragFrom, drag.translation);
289
290 return (
291 <SettingsScreen title={gt("Voice")}>
292 <ScrollView
293 contentInsetAdjustmentBehavior="automatic"
294 showsVerticalScrollIndicator={false}
295 className="flex-1"
296 contentContainerClassName="gap-3 px-5 pt-4"
297 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
298 >
299 <SettingsSection title={gt("Transcribe on")}>
300 <ListChoiceRow
301 label={gt("This device")}
302 description={gt("Audio stays on this device and is transcribed here.")}
303 selected={!grant && !sheetEnvironment}
304 onPress={() => {
305 setCloudSheet(null);
306 saveDestination(null);
307 }}
308 />
309 {hasEnvironmentVoiceCapture &&
310 environments.map((environment) => (
311 <EnvironmentChoice
312 key={environment.environmentId}
313 environment={environment}
314 selected={
315 sheetEnvironment
316 ? sheetEnvironment.environmentId === environment.environmentId
317 : grant?.destination.environmentId === environment.environmentId
318 }
319 onCloudOnly={() => setCloudSheet(environment.environmentId)}
320 />
321 ))}
322 {isReady && grant && !grantEnvironment ? (
323 <ListChoiceRow
324 label={gt("Previously selected environment")}
325 description={gt("No longer paired")}
326 separated
327 selected
328 disabled
329 onPress={() => {}}
330 />
331 ) : null}
332 </SettingsSection>
333 {hasEnvironmentVoiceCapture && methodEnvironment ? (
334 <EnvironmentMethodSection
335 environment={methodEnvironment}
336 grant={grant}
337 cloudOpen={cloudSheet === methodEnvironment.environmentId}
338 onCloudOpenChange={(open) =>
339 setCloudSheet(open ? methodEnvironment.environmentId : null)
340 }
341 />
342 ) : null}
343 {!grant && transcriber === null ? (
344 <Text className="px-2 text-sm text-foreground-muted">{m(UNAVAILABLE_EXPLANATION)}</Text>
345 ) : null}
346 {permission === "ask" || permission === "denied" ? (
347 <>
348 <SettingsSection title={gt("Microphone access")}>
349 <SettingsActionRow
350 icon="mic"
351 label={permission === "ask" ? gt("Allow microphone access") : gt("Open Settings")}
352 onPress={
353 permission === "ask" ? requestPermission : () => void Linking.openSettings()
354 }
355 />
356 </SettingsSection>
357 <Text className="px-2 text-sm text-foreground-muted">
358 {permission === "ask"
359 ? gt("Allow microphone access to record and to see your microphones.")
360 : gt("Turn on microphone access for {product} in Settings to use voice input.", {
361 product: "T3 Code",
362 })}
363 </Text>
364 </>
365 ) : null}
366 <SettingsSection title={gt("Preferred order")}>
367 {entries.map((entry, index) => {
368 const key = microphoneEntryKey(entry);
369 const shift =
370 dragFrom === -1 || index === dragFrom
371 ? 0
372 : dragFrom < dragTo && index > dragFrom && index <= dragTo
373 ? -rowHeight
374 : dragFrom > dragTo && index < dragFrom && index >= dragTo
375 ? rowHeight
376 : 0;
377 return (
378 <MicrophoneRow
379 key={`${key}:${drops}`}
380 entry={entry}
381 position={index + 1}
382 count={entries.length}
383 reorderable={ready}
384 offset={index === dragFrom ? (drag?.translation ?? 0) : shift}
385 lifted={index === dragFrom}
386 onHeight={setRowHeight}
387 onDragStart={() => setDrag({ key, translation: 0 })}
388 onDragMove={(translation) => setDrag({ key, translation })}
389 onDragEnd={(translation, cancelled) => {
390 setDrag(null);
391 setDrops((count) => count + 1);
392 if (!cancelled) move(key, dropSteps(index, translation));
393 }}
394 onStep={(direction) => move(key, direction === "up" ? -1 : 1)}
395 onForget={isMicrophoneDevice(entry) ? () => forget(key) : undefined}
396 />
397 );
398 })}
399 </SettingsSection>
400 <Text className="px-2 text-sm text-foreground-muted">
401 {Platform.OS === "android"
402 ? gt(
403 "Voice input uses the first connected microphone in this list. A wired or Bluetooth microphone is added by name the first time you dictate with it connected; until then it falls under Other. Swipe left on a named microphone to forget it. Android may still use a different microphone if the preferred one can’t be routed.",
404 )
405 : gt(
406 "Voice input uses the first connected microphone in this list. A wired or Bluetooth microphone is added by name the first time you dictate with it connected; until then it falls under Other. Swipe left on a named microphone to forget it.",
407 )}
408 </Text>
409 </ScrollView>
410 </SettingsScreen>
411 );
412 }
413
414 function MicrophoneRow(props: {
415 readonly entry: MicrophoneEntry;
416 readonly position: number;
417 readonly count: number;
418 readonly reorderable: boolean;
419 readonly offset: number;
420 readonly lifted: boolean;
421 readonly onHeight: (height: number) => void;
422 readonly onDragStart: () => void;
423 readonly onDragMove: (translation: number) => void;
424 readonly onDragEnd: (translation: number, cancelled: boolean) => void;
425 readonly onStep: (direction: "up" | "down") => void;
426 readonly onForget: (() => void) | undefined;
427 }) {
428 const gt = useGT();
429 const m = useMessages();
430 const { lifted, offset, entry, onForget } = props;
431 const kind = KINDS[entry.kind];
432 const title = isMicrophoneDevice(entry) ? entry.name : m(kind.row);
433 const { width: windowWidth } = useWindowDimensions();
434 const style = useAnimatedStyle(() => ({
435 transform: [
436 {
437 translateY: lifted
438 ? offset
439 : withTiming(offset, { duration: 160, reduceMotion: ReduceMotion.System }),
440 },
441 ],
442 zIndex: lifted ? 1 : 0,
443 }));
444 const row = (
445 // Opaque so the swipe actions stay hidden until the row slides over them.
446 <View className="flex-row items-center bg-grouped-card">
447 <View
448 accessible
449 accessibilityLabel={gt("{title}, {kind}, {position} of {count}", {
450 title,
451 kind: m(kind.label),
452 position: props.position,
453 count: props.count,
454 })}
455 accessibilityActions={
456 onForget
457 ? [{ name: "forget", label: gt("Forget", { $context: "verb, forget a microphone" }) }]
458 : undefined
459 }
460 onAccessibilityAction={({ nativeEvent }) => {
461 if (nativeEvent.actionName === "forget") onForget?.();
462 }}
463 className="min-w-0 flex-1"
464 >
465 <ListRowLayout
466 icon={kind.icon}
467 label={title}
468 description={isMicrophoneDevice(entry) ? m(kind.label) : m(kind.rowDescription)}
469 />
470 </View>
471 {props.reorderable ? (
472 <SettingsDragHandle
473 title={title}
474 canMoveUp={props.position > 1}
475 canMoveDown={props.position < props.count}
476 onStart={props.onDragStart}
477 onMove={props.onDragMove}
478 onEnd={props.onDragEnd}
479 onStep={props.onStep}
480 />
481 ) : null}
482 </View>
483 );
484 return (
485 <Reanimated.View
486 style={style}
487 onLayout={(event) => props.onHeight(event.nativeEvent.layout.height)}
488 className={cn(
489 props.position > 1 && !lifted && "border-t border-border-subtle",
490 lifted && "bg-grouped-card shadow-md",
491 )}
492 >
493 {onForget ? (
494 <ThreadSwipeable
495 backgroundColor="transparent"
496 fullSwipeAction="primary"
497 fullSwipeWidth={windowWidth - 40}
498 onDelete={onForget}
499 primaryAction={{
500 accessibilityLabel: gt("Forget {title}", { title }),
501 destructive: true,
502 icon: "trash",
503 label: gt("Forget", { $context: "verb, forget a microphone" }),
504 onPress: onForget,
505 }}
506 secondaryAction={null}
507 threadKey={`microphone:${microphoneEntryKey(entry)}`}
508 threadTitle={title}
509 >
510 {() => row}
511 </ThreadSwipeable>
512 ) : (
513 row
514 )}
515 </Reanimated.View>
516 );
517 }