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

SettingsKeyboardRouteScreen.tsx

82 lines3.1 KB

1import { msg, T, useGT, useMessages } from "gt-react-native";
2import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
3import { useAtomSet, useAtomValue } from "@effect/atom-react";
4import { AsyncResult } from "effect/reactivity";
5import { Platform } from "react-native";
6import { useSafeAreaInsets } from "react-native-safe-area-context";
7​
8import { AppText as Text } from "../../components/ui/AppText";
9import {
10 DEFAULT_COMPOSER_ENTER_BEHAVIOR,
11 type ComposerEnterBehavior,
12} from "../../lib/composerEnterBehavior";
13import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
14import { ListChoiceRow as SettingsChoiceRow } from "../../components/ui/ListChoiceRow";
15import { SettingsScreen } from "./components/SettingsScreen";
16import { SettingsSection } from "./components/SettingsSection";
17​
18const SEND_MODIFIER = Platform.OS === "android" ? "Ctrl" : "Command";
19​
20const ENTER_BEHAVIOR_OPTIONS: ReadonlyArray<{
21 readonly behavior: ComposerEnterBehavior;
22 readonly label: string;
23 readonly description: string;
24}> = [
25 {
26 behavior: "send",
27 label: msg("Send message"),
28 description: msg("{returnKey} sends the message. {shiftReturn} inserts a new line.", {
29 returnKey: "Return",
30 shiftReturn: "Shift-Return",
31 }),
32 },
33 {
34 behavior: "newline",
35 label: msg("Insert new line"),
36 description: msg("{returnKey} inserts a new line. {sendKeys} sends the message.", {
37 returnKey: "Return",
38 sendKeys: `${SEND_MODIFIER}-Return`,
39 }),
40 },
41];
42​
43export function SettingsKeyboardRouteScreen() {
44 const gt = useGT();
45 const m = useMessages();
46 const insets = useSafeAreaInsets();
47 const preferencesResult = useAtomValue(mobilePreferencesAtom);
48 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
49 const preferencesReady = AsyncResult.isSuccess(preferencesResult) && !preferencesResult.waiting;
50 const selectedBehavior = AsyncResult.isSuccess(preferencesResult)
51 ? (preferencesResult.value.composerEnterBehavior ?? DEFAULT_COMPOSER_ENTER_BEHAVIOR)
52 : null;
53​
54 return (
55 <SettingsScreen title={gt("Keyboard")}>
56 <ScrollView
57 contentInsetAdjustmentBehavior="automatic"
58 showsVerticalScrollIndicator={false}
59 className="flex-1"
60 contentContainerClassName="gap-3 px-5 pt-4"
61 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
62 >
63 <SettingsSection title={gt("Return key")}>
64 {ENTER_BEHAVIOR_OPTIONS.map((option, index) => (
65 <SettingsChoiceRow
66 key={option.behavior}
67 label={m(option.label)}
68 description={m(option.description)}
69 selected={selectedBehavior === option.behavior}
70 separated={index > 0}
71 disabled={!preferencesReady}
72 onPress={() => savePreferences({ composerEnterBehavior: option.behavior })}
73 />
74 ))}
75 </SettingsSection>
76 <Text className="px-2 text-sm text-foreground-muted">
77 <T>Applies to the composer when a hardware keyboard is connected.</T>
78 </Text>
79 </ScrollView>
80 </SettingsScreen>
81 );