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

SettingsLanguageRouteScreen.tsx

85 lines2.9 KB

1import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
2import { useAtomSet, useAtomValue } from "@effect/atom-react";
3import { AsyncResult } from "effect/reactivity";
4import { useSafeAreaInsets } from "react-native-safe-area-context";
5​
6import { I18nProvider, useGT, useLanguagePreference } from "../../i18n/I18nProvider";
7import { SYSTEM_LANGUAGE, nativeLanguageName } from "@t3tools/client-runtime/locales";
8import { SUPPORTED_LOCALES } from "../../i18n/locales";
9import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
10import { ListChoiceRow as SettingsChoiceRow } from "../../components/ui/ListChoiceRow";
11import { SettingsRow } from "./components/SettingsRow";
12import { SettingsSection } from "./components/SettingsSection";
13import { SettingsScreen } from "./components/SettingsScreen";
14​
15/** General's row that opens the language picker, showing the current choice. */
16export function LanguageSettingsSection() {
17 return (
18 <I18nProvider>
19 <LanguageRow />
20 </I18nProvider>
21 );
23​
24function LanguageRow() {
25 const gt = useGT();
26 const language = useLanguagePreference();
27 return (
28 <SettingsSection>
29 <SettingsRow
30 icon="globe"
31 label={gt("Language")}
32 value={
33 SUPPORTED_LOCALES.includes(language) ? nativeLanguageName(language) : gt("System default")
34 }
35 target="SettingsLanguage"
36 />
37 </SettingsSection>
38 );
40​
41export function SettingsLanguageRouteScreen() {
42 return (
43 <I18nProvider>
44 <LanguageSettings />
45 </I18nProvider>
46 );
48​
49function LanguageSettings() {
50 const gt = useGT();
51 const insets = useSafeAreaInsets();
52 const preferencesResult = useAtomValue(mobilePreferencesAtom);
53 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
54 const preferencesReady = AsyncResult.isSuccess(preferencesResult) && !preferencesResult.waiting;
55 const language = useLanguagePreference();
56 const options = [
57 { language: SYSTEM_LANGUAGE, label: gt("System default") },
58 ...SUPPORTED_LOCALES.map((locale) => ({ language: locale, label: nativeLanguageName(locale) })),
59 ];
60​
61 return (
62 <SettingsScreen title={gt("Language")}>
63 <ScrollView
64 contentInsetAdjustmentBehavior="automatic"
65 showsVerticalScrollIndicator={false}
66 className="flex-1"
67 contentContainerClassName="gap-3 px-5 pt-4"
68 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
69 >
70 <SettingsSection>
71 {options.map((option, index) => (
72 <SettingsChoiceRow
73 key={option.language}
74 label={option.label}
75 selected={preferencesReady && language === option.language}
76 separated={index > 0}
77 disabled={!preferencesReady}
78 onPress={() => savePreferences({ language: option.language })}
79 />
80 ))}
81 </SettingsSection>
82 </ScrollView>
83 </SettingsScreen>
84 );