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

SettingsAboutRouteScreen.tsx

150 lines5.4 KB

1import * as Dialog from "../../components/ui/Dialog";
2import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
3import Constants from "expo-constants";
4import { useCallback, useEffect, useRef, useState } from "react";
5import { Pressable, View } from "react-native";
6import { useSafeAreaInsets } from "react-native-safe-area-context";
7​
8import { AppText as Text } from "../../components/ui/AppText";
9import { SymbolView } from "../../components/ui/AppSymbol";
10import {
11 type AppUpdateCheckState,
12 isAppUpdateCheckAvailable,
13 registerHiddenUpdateTap,
14 runAppUpdateCheck,
15} from "../updates/app-updates";
16import { SettingsRow } from "./components/SettingsRow";
17import { SettingsSection } from "./components/SettingsSection";
18import { SettingsScreen } from "./components/SettingsScreen";
19​
20export function SettingsAboutRouteScreen() {
21 const insets = useSafeAreaInsets();
22​
23 return (
24 <SettingsScreen title="About T3 Code">
25 <ScrollView
26 contentInsetAdjustmentBehavior="automatic"
27 showsVerticalScrollIndicator={false}
28 className="flex-1"
29 contentContainerClassName="gap-6 px-5 pt-4"
30 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
31 >
32 <AppSettingsSection />
33 </ScrollView>
34 </SettingsScreen>
35 );
37​
38function AppSettingsSection() {
39 const [updateState, setUpdateState] = useState<AppUpdateCheckState>("idle");
40 const updateInFlight = useRef(false);
41 const hiddenUpdateTapCount = useRef(0);
42​
43 const version = Constants.expoConfig?.version ?? "0.0.0";
44 // Fall back to "production" to match resolveAppVariant in app.config.ts, so a
45 // missing variant never mislabels a production build as development.
46 const variant = (Constants.expoConfig?.extra?.appVariant as string | undefined) ?? "production";
47 const variantLabel = variant === "production" ? "" : capitalize(variant);
48 const versionLabel = variantLabel ? `${version} · ${variantLabel}` : version;
49 const updateCheckAvailable = isAppUpdateCheckAvailable();
50 const busy =
51 updateState === "checking" || updateState === "downloading" || updateState === "restarting";
52​
53 // "Up to date" is a transient acknowledgement, not a state worth persisting —
54 // return the version row to its normal, deliberately quiet state.
55 useEffect(() => {
56 if (updateState !== "current") return;
57 const timer = setTimeout(() => setUpdateState("idle"), 3000);
58 return () => clearTimeout(timer);
59 }, [updateState]);
60​
61 const checkForUpdate = useCallback(async () => {
62 // `disabled={busy}` only takes effect on the next render, so two taps in the
63 // same frame would both get through. The ref closes that window.
64 if (updateInFlight.current) return;
65 updateInFlight.current = true;
66 try {
67 // The user asked for this restart by tapping the version row, so it may
68 // apply immediately instead of prompting.
69 await runAppUpdateCheck({
70 applyMode: "immediate",
71 onFailure: (message) => Dialog.alert("Update failed", message),
72 onStateChange: setUpdateState,
73 });
74 } finally {
75 updateInFlight.current = false;
76 }
77 }, []);
78​
79 const handleVersionPress = useCallback(() => {
80 if (!updateCheckAvailable || updateInFlight.current) return;
81 const tap = registerHiddenUpdateTap(hiddenUpdateTapCount.current);
82 hiddenUpdateTapCount.current = tap.nextCount;
83 if (tap.shouldCheck) {
84 void checkForUpdate();
85 }
86 }, [checkForUpdate, updateCheckAvailable]);
87​
88 const statusLabel =
89 updateState === "checking"
90 ? "Checking…"
91 : updateState === "downloading"
92 ? "Downloading…"
93 : // "ready" appears only when this check joined an in-flight background-mode
94 // check; that download installs at the next backgrounding.
95 updateState === "ready"
96 ? "Update ready"
97 : updateState === "restarting"
98 ? "Restarting…"
99 : updateState === "current"
100 ? "Up to date"
101 : null;
102​
103 const versionRow = (
104 <View className="flex-row items-center gap-4 p-4">
105 <SymbolView
106 name="info.circle"
107 size={22}
108 tintColorClassName="accent-icon"
109 type="monochrome"
110 weight="regular"
111 />
112 <Text className="flex-1 text-lg text-foreground">Version</Text>
113 <View className="items-end">
114 <Text className="text-lg text-foreground-muted">{versionLabel}</Text>
115 {statusLabel ? (
116 <Text className="text-xs text-foreground-muted/70">{statusLabel}</Text>
117 ) : null}
118 </View>
119 </View>
120 );
121​
122 return (
123 <SettingsSection title="App">
124 <SettingsRow icon="internaldrive" label="Client Storage" target="SettingsClientStorage" />
125 <SettingsRow icon="stethoscope" label="Diagnostics" target="SettingsDiagnostics" />
126 <SettingsRow
127 icon="doc.on.doc"
128 label="Open source licenses"
129 target="SettingsOpenSourceLicenses"
130 />
131 <SettingsRow icon="doc.text" label="Legal" fullScreenTarget="SettingsLegal" />
132 {updateCheckAvailable ? (
133 <Pressable
134 accessibilityLabel={`Version ${versionLabel}`}
135 accessibilityRole="text"
136 disabled={busy}
137 onPress={handleVersionPress}
138 >
139 {versionRow}
140 </Pressable>
141 ) : (
142 versionRow
143 )}
144 </SettingsSection>
145 );
147​
148function capitalize(value: string): string {
149 return value.length > 0 ? value.charAt(0).toUpperCase() + value.slice(1) : value;