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 // Fall back to "production" to match resolveAppVariant in app.config.ts, so a
46 // missing variant never mislabels a production build as development.
47 const variant = (Constants.expoConfig?.extra?.appVariant as string | undefined) ?? "production";
48 const variantLabel = variant === "production" ? "" : capitalize(variant);
49 const versionLabel = variantLabel ? `${version} · ${variantLabel}` : version;
50 const updateCheckAvailable = isAppUpdateCheckAvailable();
51 const busy =
52 updateState === "checking" || updateState === "downloading" || updateState === "restarting";
53
54 useEffect(() => {
55 if (updateState !== "current") return;
56 const timer = setTimeout(() => setUpdateState("idle"), 3000);
57 return () => clearTimeout(timer);
58 }, [updateState]);
59
60 async function checkForUpdate() {
61 // `disabled={busy}` only takes effect on the next render, so two taps in the
62 // same frame would both get through. The ref closes that window.
63 if (updateInFlight.current) return;
64 updateInFlight.current = true;
65 // Failures arrive through onFailure; the settled result only matters for unlocking.
66 await settlePromise(() =>
67 runAppUpdateCheck({
68 applyMode: "immediate",
69 onFailure: (message) => Dialog.alert("Update failed", message),
70 onStateChange: setUpdateState,
71 }),
72 );
73 updateInFlight.current = false;
74 }
75
76 function handleVersionPress() {
77 if (!updateCheckAvailable || updateInFlight.current) return;
78 const tap = registerHiddenUpdateTap(hiddenUpdateTapCount.current);
79 hiddenUpdateTapCount.current = tap.nextCount;
80 if (tap.shouldCheck) {
81 void checkForUpdate();
82 }
83 }
84
85 const statusLabel =
86 updateState === "checking"
87 ? "Checking…"
88 : updateState === "downloading"
89 ? "Downloading…"
90 : updateState === "ready"
91 ? "Update ready"
92 : updateState === "restarting"
93 ? "Restarting…"
94 : updateState === "current"
95 ? "Up to date"
96 : null;
97
98 const versionRow = (
99 <View className="flex-row items-center gap-4 p-4">
100 <SymbolView
101 name="info.circle"
102 size={22}
103 tintColorClassName="accent-icon"
104 type="monochrome"
105 weight="regular"
106 />
107 <Text className="flex-1 text-lg text-foreground">Version</Text>
108 <View className="items-end">
109 <Text className="text-lg text-foreground-muted">{versionLabel}</Text>
110 {statusLabel ? (
111 <Text className="text-xs text-foreground-muted/70">{statusLabel}</Text>
112 ) : null}
113 </View>
114 </View>
115 );
116
117 return (
118 <SettingsSection title="App">
119 <SettingsRow icon="internaldrive" label="Client Storage" target="SettingsClientStorage" />
120 <SettingsRow icon="stethoscope" label="Diagnostics" target="SettingsDiagnostics" />
121 <SettingsRow
122 icon="doc.on.doc"
123 label="Open source licenses"
124 target="SettingsOpenSourceLicenses"
125 />
126 <SettingsRow icon="doc.text" label="Legal" fullScreenTarget="SettingsLegal" />
127 {updateCheckAvailable ? (
128 <Pressable
129 accessibilityLabel={`Version ${versionLabel}`}
130 accessibilityRole="text"
131 disabled={busy}
132 onPress={handleVersionPress}
133 >
134 {versionRow}
135 </Pressable>
136 ) : (
137 versionRow
138 )}
139 </SettingsSection>
140 );
141}
142
143function capitalize(value: string): string {
144 return value.length > 0 ? value.charAt(0).toUpperCase() + value.slice(1) : value;
145}