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

SettingsKeyboardRouteScreen.tsx

79 lines3.1 KB

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