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

SettingsKeyboardRouteScreen.tsx

79 lines3.1 KB

1import { useAtomSet, useAtomValue } from "@effect/atom-react";
2import { useNavigation } from "@react-navigation/native";
3import { AsyncResult } from "effect/reactivity";
4import { Platform, 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 { NativeStackScreenOptions } from "../../native/StackHeader";
10import {
11 DEFAULT_COMPOSER_ENTER_BEHAVIOR,
12 type ComposerEnterBehavior,
13} from "../../lib/composerEnterBehavior";
14import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
15import { SettingsChoiceRow } from "./components/SettingsChoiceRow";
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 <SettingsChoiceRow
63 key={option.behavior}
64 label={option.label}
65 description={option.description}
66 selected={selectedBehavior === option.behavior}
67 separated={index > 0}
68 disabled={!preferencesReady}
69 onPress={() => savePreferences({ composerEnterBehavior: option.behavior })}
70 />
71 ))}
72 </SettingsSection>
73 <Text className="px-2 text-sm text-foreground-muted">
74 Applies to the composer when a hardware keyboard is connected.
75 </Text>
76 </ScrollView>
77 </View>
78 );