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

SettingsVoiceModelsRouteScreen.tsx

108 lines4.3 KB

1import { NativeScrollView } from "../../native/NativeScrollViewMarker";
2import { LegendList } from "@legendapp/list/react-native";
3import { useAtomValue } from "@effect/atom-react";
4import type { StaticScreenProps } from "@react-navigation/native";
5import type { EnvironmentId } from "@t3tools/contracts";
6import { AsyncResult } from "effect/reactivity";
7import { useState } from "react";
8import { Platform, View } from "react-native";
9​
10import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
11import { AppText as Text } from "../../components/ui/AppText";
12import { EmptyState } from "../../components/ui/EmptyState";
13import { cn } from "../../lib/cn";
14import { useEnvironments } from "../../state/environments";
15import { voiceSpeechBenchmarksAtom } from "../voice-input/voiceDestination";
16import { PullRequestSearchSheet } from "../pull-requests/PullRequestSearchSheet";
17import { useVoiceModels, VoiceModelRow } from "./VoiceModelSection";
18​
19export function SettingsVoiceModelsRouteScreen({
20 route,
21}: StaticScreenProps<{ readonly environmentId: EnvironmentId }>) {
22 const { environments } = useEnvironments();
23 const label =
24 environments.find((environment) => environment.environmentId === route.params.environmentId)
25 ?.label ?? "This environment";
26 const voice = useVoiceModels(route.params.environmentId, label);
27 const benchmarks = new Map(
28 (
29 AsyncResult.getOrElse(
30 useAtomValue(voiceSpeechBenchmarksAtom(route.params.environmentId)),
31 () => null,
32 )?.models ?? []
33 ).map((model) => [model.id, model.benchmark]),
34 );
35 const language = new Intl.DateTimeFormat().resolvedOptions().locale.split("-")[0]!.toLowerCase();
36 const [query, setQuery] = useState("");
37 const needle = query.trim().toLowerCase();
38 // Catalog order, not state order: re-sorting when a download finishes would move rows under a tap.
39 const models = voice.models
40 .map((model) => {
41 const benchmark = benchmarks.get(model.id);
42 return benchmark ? { ...model, benchmark } : model;
43 })
44 .filter((model) =>
45 `${model.name} ${model.description} ${model.languages.join(" ")}`
46 .toLowerCase()
47 .includes(needle),
48 );
49 return (
50 <PullRequestSearchSheet
51 title="Transcription models"
52 placeholder="Search models or languages"
53 closeAccessibilityLabel="Close transcription models"
54 clearAccessibilityLabel="Clear model search"
55 query={query}
56 onQueryChange={setQuery}
57 >
58 {({ paddingBottom }) => (
59 <LegendList
60 renderScrollComponent={NativeScrollView}
61 data={models}
62 keyExtractor={(model) => model.id}
63 extraData={{ voice, language, count: models.length }}
64 estimatedItemSize={72}
65 recycleItems
66 className="flex-1"
67 contentInsetAdjustmentBehavior="automatic"
68 automaticallyAdjustKeyboardInsets={Platform.OS === "ios"}
69 keyboardDismissMode={Platform.OS === "ios" ? "interactive" : "on-drag"}
70 keyboardShouldPersistTaps="handled"
71 showsVerticalScrollIndicator={false}
72 contentContainerStyle={{ paddingHorizontal: 16, paddingTop: 16, paddingBottom }}
73 ListHeaderComponent={
74 voice.canManage ? undefined : (
75 <Text className="pb-3 text-xs text-foreground-muted">
76 This connection can’t change settings on {label}.
77 </Text>
78 )
79 }
80 renderItem={({ item, index }) => (
81 <View
82 className={cn(
83 "overflow-hidden",
84 index === 0 && "rounded-t-2xl",
85 index === models.length - 1 && "rounded-b-2xl",
86 )}
87 >
88 <VoiceModelRow model={item} voice={voice} separated={index > 0} language={language} />
89 </View>
90 )}
91 ListEmptyComponent={
92 voice.loading ? (
93 <AppActivityIndicator className="my-8" />
94 ) : (
95 <EmptyState
96 title={needle ? "No models match" : "No transcription models"}
97 detail={
98 needle ? "Try another name or language." : `${label} has no models to list.`
99 }
100 variant="plain"
101 />
102 )
103 }
104 />
105 )}
106 </PullRequestSearchSheet>
107 );