Open History to see the last commit
1import * as Dialog from "../../components/ui/Dialog";
2import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
3import Constants from "expo-constants";
4import { settlePromise } from "@t3tools/client-runtime/state/runtime";
5import { useEffect, useRef, useState } from "react";
6import { Pressable, View } from "react-native";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8
9import { AppText as Text } from "../../components/ui/AppText";
10import { SymbolView } from "../../components/ui/AppSymbol";
11import {
12 type AppUpdateCheckState,
13 isAppUpdateCheckAvailable,
14 registerHiddenUpdateTap,
15 runAppUpdateCheck,
16} from "../updates/app-updates";
17import { SettingsRow } from "./components/SettingsRow";
18import { SettingsSection } from "./components/SettingsSection";
19import { SettingsScreen } from "./components/SettingsScreen";
20
21export function SettingsAboutRouteScreen() {
22 const insets = useSafeAreaInsets();
23
24 return (
25 <SettingsScreen title="About T3 Code">
26 <ScrollView
27 contentInsetAdjustmentBehavior="automatic"
28 showsVerticalScrollIndicator={false}
29 className="flex-1"
30 contentContainerClassName="gap-6 px-5 pt-4"
31 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
32 >
33 <AppSettingsSection />
34 </ScrollView>
35 </SettingsScreen>
36 );
37}
38
39function AppSettingsSection() {
40 const [updateState, setUpdateState] = useState<AppUpdateCheckState>("idle");
41 const updateInFlight = useRef(false);
42 const hiddenUpdateTapCount = useRef(0);
43
44 const version = Constants.expoConfig?.version ?? "0.0.0";
45 // Match app.config.ts's production fallback to avoid mislabeling builds 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 useEffect(() => {
54 if (updateState !== "current") return;
55 const timer = setTimeout(() => setUpdateState("idle"), 3000);
56 return () => clearTimeout(timer);
57 }, [updateState]);
58
59 async function checkForUpdate() {
60 // disabled updates next render; the ref blocks same-frame double taps.
61 if (updateInFlight.current) return;
62 updateInFlight.current = true;
63 // Failures arrive through onFailure; the settled result only matters for unlocking.
64 await settlePromise(() =>
65 runAppUpdateCheck({
66 applyMode: "immediate",
67 onFailure: (message) => Dialog.alert("Update failed", message),
68 onStateChange: setUpdateState,
69 }),
70 );
71 updateInFlight.current = false;
72 }
73
74 function handleVersionPress() {
75 if (!updateCheckAvailable || updateInFlight.current) return;
76 const tap = registerHiddenUpdateTap(hiddenUpdateTapCount.current);
77 hiddenUpdateTapCount.current = tap.nextCount;
78 if (tap.shouldCheck) {
79 void checkForUpdate();
80 }
81 }
82
83 const statusLabel =
84 updateState === "checking"
85 ? "Checking…"
86 : updateState === "downloading"
87 ? "Downloading…"
88 : updateState === "ready"
89 ? "Update ready"
90 : updateState === "restarting"
91 ? "Restarting…"
92 : updateState === "current"
93 ? "Up to date"
94 : null;
95
96 const versionRow = (
97 <View className="flex-row items-center gap-4 p-4">
98 <SymbolView
99 name="info.circle"
100 size={22}
101 tintColorClassName="accent-icon"
102 type="monochrome"
103 weight="regular"
104 />
105 <Text className="flex-1 text-lg text-foreground">Version</Text>
106 <View className="items-end">
107 <Text className="text-lg text-foreground-muted">{versionLabel}</Text>
108 {statusLabel ? (
109 <Text className="text-xs text-foreground-muted/70">{statusLabel}</Text>
110 ) : null}
111 </View>
112 </View>
113 );
114
115 return (
116 <SettingsSection title="App">
117 <SettingsRow icon="internaldrive" label="Client Storage" target="SettingsClientStorage" />
118 <SettingsRow icon="stethoscope" label="Diagnostics" target="SettingsDiagnostics" />
119 <SettingsRow
120 icon="doc.on.doc"
121 label="Open source licenses"
122 target="SettingsOpenSourceLicenses"
123 />
124 <SettingsRow icon="doc.text" label="Legal" fullScreenTarget="SettingsLegal" />
125 {updateCheckAvailable ? (
126 <Pressable
127 accessibilityLabel={`Version ${versionLabel}`}
128 accessibilityRole="text"
129 disabled={busy}
130 onPress={handleVersionPress}
131 >
132 {versionRow}
133 </Pressable>
134 ) : (
135 versionRow
136 )}
137 </SettingsSection>
138 );
139}
140
141function capitalize(value: string): string {
142 return value.length > 0 ? value.charAt(0).toUpperCase() + value.slice(1) : value;
143}