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 { 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 { useDeadlinePassed } from "../../lib/clock";
12import {
13 type AppUpdateCheckState,
14 isAppUpdateCheckAvailable,
15 registerHiddenUpdateTap,
16 runAppUpdateCheck,
17} from "../updates/app-updates";
18import { SettingsRow } from "./components/SettingsRow";
19import { SettingsSection } from "./components/SettingsSection";
20import { SettingsScreen } from "./components/SettingsScreen";
21
22export function SettingsAboutRouteScreen() {
23 const insets = useSafeAreaInsets();
24
25 return (
26 <SettingsScreen title="About">
27 <ScrollView
28 contentInsetAdjustmentBehavior="automatic"
29 showsVerticalScrollIndicator={false}
30 className="flex-1"
31 contentContainerClassName="gap-6 px-5 pt-4"
32 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
33 >
34 <AppSettingsSection />
35 </ScrollView>
36 </SettingsScreen>
37 );
38}
39
40function AppSettingsSection() {
41 const [checkState, setCheckState] = useState<{
42 readonly state: AppUpdateCheckState;
43 // "Up to date" is a confirmation that fades back to idle.
44 readonly settledUntil: number | null;
45 }>({ state: "idle", settledUntil: null });
46 const confirmationExpired = useDeadlinePassed(checkState.settledUntil);
47 const updateState =
48 checkState.state === "current" && confirmationExpired ? "idle" : checkState.state;
49 const updateInFlight = useRef(false);
50 const hiddenUpdateTapCount = useRef(0);
51
52 const version = Constants.expoConfig?.version ?? "0.0.0";
53 // Match app.config.ts's production fallback to avoid mislabeling builds as development.
54 const variant = (Constants.expoConfig?.extra?.appVariant as string | undefined) ?? "production";
55 const variantLabel = variant === "production" ? "" : capitalize(variant);
56 const versionLabel = variantLabel ? `${version} · ${variantLabel}` : version;
57 const updateCheckAvailable = isAppUpdateCheckAvailable();
58 const busy =
59 updateState === "checking" || updateState === "downloading" || updateState === "restarting";
60
61 async function checkForUpdate() {
62 // disabled updates next render; the ref blocks same-frame double taps.
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: (state) =>
71 setCheckState({
72 state,
73 settledUntil: state === "current" ? Date.now() + 3000 : null,
74 }),
75 }),
76 );
77 updateInFlight.current = false;
78 }
79
80 function handleVersionPress() {
81 if (!updateCheckAvailable || updateInFlight.current) return;
82 const tap = registerHiddenUpdateTap(hiddenUpdateTapCount.current);
83 hiddenUpdateTapCount.current = tap.nextCount;
84 if (tap.shouldCheck) {
85 void checkForUpdate();
86 }
87 }
88
89 const statusLabel =
90 updateState === "checking"
91 ? "Checking…"
92 : updateState === "downloading"
93 ? "Downloading…"
94 : updateState === "ready"
95 ? "Update ready"
96 : updateState === "restarting"
97 ? "Restarting…"
98 : updateState === "current"
99 ? "Up to date"
100 : null;
101
102 const versionRow = (
103 <View className="flex-row items-center gap-4 p-4">
104 <SymbolView
105 name="info.circle"
106 size={22}
107 tintColorClassName="accent-icon"
108 type="monochrome"
109 weight="regular"
110 />
111 <Text className="flex-1 text-lg text-foreground">Version</Text>
112 <View className="items-end">
113 <Text className="text-lg text-foreground-muted">{versionLabel}</Text>
114 {statusLabel ? (
115 <Text className="text-xs text-foreground-muted/70">{statusLabel}</Text>
116 ) : null}
117 </View>
118 </View>
119 );
120
121 return (
122 <SettingsSection title="App">
123 <SettingsRow icon="internaldrive" label="Client Storage" target="SettingsClientStorage" />
124 <SettingsRow icon="stethoscope" label="Diagnostics" target="SettingsDiagnostics" />
125 <SettingsRow
126 icon="doc.on.doc"
127 label="Open source licenses"
128 target="SettingsOpenSourceLicenses"
129 />
130 <SettingsRow icon="doc.text" label="Legal" fullScreenTarget="SettingsLegal" />
131 {updateCheckAvailable ? (
132 <Pressable
133 accessibilityLabel={`Version ${versionLabel}`}
134 accessibilityRole="text"
135 disabled={busy}
136 onPress={handleVersionPress}
137 >
138 {versionRow}
139 </Pressable>
140 ) : (
141 versionRow
142 )}
143 </SettingsSection>
144 );
145}
146
147function capitalize(value: string): string {
148 return value.length > 0 ? value.charAt(0).toUpperCase() + value.slice(1) : value;
149}