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

SettingsKeyboardRouteScreen.tsx

101 lines3.9 KB

1import { useAtomSet, useAtomValue } from "@effect/atom-react";
2import { useNavigation } from "@react-navigation/native";
3import { AsyncResult } from "effect/unstable/reactivity";
4import { Platform, Pressable, ScrollView, View } from "react-native";
5import { useSafeAreaInsets } from "react-native-safe-area-context";
6​
7import { AndroidScreenHeader } from "../../components/AndroidScreenHeader";
8import { AppText as Text } from "../../components/AppText";
9import { SymbolView } from "../../components/AppSymbol";
10import { NativeStackScreenOptions } from "../../native/StackHeader";
11import {
12 DEFAULT_COMPOSER_ENTER_BEHAVIOR,
13 type ComposerEnterBehavior,
14} from "../../lib/composerEnterBehavior";
15import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
16import { SettingsSection } from "./components/SettingsSection";
17​
18const ENTER_BEHAVIOR_OPTIONS: ReadonlyArray<{
19 readonly behavior: ComposerEnterBehavior;
20 readonly label: string;
21 readonly description: string;
22}> = [
23 {
24 behavior: "send",
25 label: "Send message",
26 description: "Return sends the message. Shift-Return inserts a new line.",
27 },
28 {
29 behavior: "newline",
30 label: "Insert new line",
31 description: "Return inserts a new line. Command-Return sends the message.",
32 },
33];
34​
35export function SettingsKeyboardRouteScreen() {
36 const navigation = useNavigation();
37 const insets = useSafeAreaInsets();
38 const preferencesResult = useAtomValue(mobilePreferencesAtom);
39 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
40 const preferencesReady = AsyncResult.isSuccess(preferencesResult) && !preferencesResult.waiting;
41 const selectedBehavior = AsyncResult.isSuccess(preferencesResult)
42 ? (preferencesResult.value.composerEnterBehavior ?? DEFAULT_COMPOSER_ENTER_BEHAVIOR)
43 : null;
44​
45 return (
46 <View collapsable={false} className="flex-1 bg-sheet">
47 {Platform.OS === "android" ? (
48 <>
49 <NativeStackScreenOptions options={{ headerShown: false }} />
50 <AndroidScreenHeader title="Keyboard" onBack={() => navigation.goBack()} />
51 </>
52 ) : null}
53 <ScrollView
54 contentInsetAdjustmentBehavior="automatic"
55 showsVerticalScrollIndicator={false}
56 className="flex-1"
57 contentContainerClassName="gap-3 px-5 pt-4"
58 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
59 >
60 <SettingsSection title="Return key">
61 {ENTER_BEHAVIOR_OPTIONS.map((option, index) => (
62 <Pressable
63 key={option.behavior}
64 accessibilityRole="radio"
65 accessibilityState={{
66 checked: selectedBehavior === option.behavior,
67 disabled: !preferencesReady,
68 }}
69 disabled={!preferencesReady}
70 onPress={() => savePreferences({ composerEnterBehavior: option.behavior })}
71 className={
72 index === 0
73 ? "flex-row items-center gap-4 p-4"
74 : "flex-row items-center gap-4 border-t border-border-subtle p-4"
75 }
76 >
77 <View className="min-w-0 flex-1 gap-1">
78 <Text className="text-lg text-foreground">{option.label}</Text>
79 <Text className="text-sm leading-normal text-foreground-muted">
80 {option.description}
81 </Text>
82 </View>
83 {selectedBehavior === option.behavior ? (
84 <SymbolView
85 name="checkmark"
86 size={18}
87 tintColorClassName={"accent-icon"}
88 type="monochrome"
89 weight="semibold"
90 />
91 ) : null}
92 </Pressable>
93 ))}
94 </SettingsSection>
95 <Text className="px-2 text-sm text-foreground-muted">
96 Applies to the composer when a hardware keyboard is connected.
97 </Text>
98 </ScrollView>
99 </View>
100 );