Open History to see the last commit
1import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView";
2import { useAtomSet, useAtomValue } from "@effect/atom-react";
3import { type EnvironmentMachineKind, resolveEnvironmentMachineKind } from "@t3tools/contracts";
4import { AsyncResult } from "effect/unstable/reactivity";
5import { useMemo } from "react";
6import { ActivityIndicator, Alert, Pressable, View } from "react-native";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8
9import { AppText as Text } from "../../components/AppText";
10import { SymbolView } from "../../components/AppSymbol";
11import { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSymbol";
12import {
13 clearClientCacheAtom,
14 clientCacheSummaryAtom,
15 type EnvironmentClientCacheSummary,
16} from "../../state/client-cache-state";
17import { useServerConfigs } from "../../state/entities";
18import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
19import { SettingsSection } from "./components/SettingsSection";
20import { SettingsScreen } from "./components/SettingsScreen";
21
22export function SettingsClientStorageRouteScreen() {
23 const insets = useSafeAreaInsets();
24 const summaryResult = useAtomValue(clientCacheSummaryAtom);
25 const clearResult = useAtomValue(clearClientCacheAtom);
26 const clearCache = useAtomSet(clearClientCacheAtom);
27 const { savedConnectionsById } = useSavedRemoteConnections();
28 const serverConfigs = useServerConfigs();
29 const isClearing = clearResult.waiting;
30 const summary = AsyncResult.isSuccess(summaryResult) ? summaryResult.value : null;
31 const environmentSummaries = useMemo(
32 () =>
33 [...(summary?.environments ?? [])].sort((left, right) => {
34 const leftLabel = savedConnectionsById[left.environmentId]?.environmentLabel ?? "";
35 const rightLabel = savedConnectionsById[right.environmentId]?.environmentLabel ?? "";
36 return leftLabel.localeCompare(rightLabel);
37 }),
38 [savedConnectionsById, summary?.environments],
39 );
40
41 const confirmClearEnvironment = (environment: EnvironmentClientCacheSummary) => {
42 const label =
43 savedConnectionsById[environment.environmentId]?.environmentLabel ??
44 environment.environmentId;
45 Alert.alert(
46 `Clear cache for ${label}?`,
47 "This removes offline threads, server metadata, and cached branches for this environment. The saved connection and credentials stay intact.",
48 [
49 { text: "Cancel", style: "cancel" },
50 {
51 text: "Clear Cache",
52 style: "destructive",
53 onPress: () =>
54 clearCache({ type: "environment", environmentId: environment.environmentId }),
55 },
56 ],
57 );
58 };
59
60 const confirmClearAll = () => {
61 Alert.alert(
62 "Clear all client caches?",
63 "This removes offline data for every environment. Connections, credentials, account data, and app preferences stay intact.",
64 [
65 { text: "Cancel", style: "cancel" },
66 {
67 text: "Clear All Caches",
68 style: "destructive",
69 onPress: () => clearCache({ type: "all" }),
70 },
71 ],
72 );
73 };
74
75 return (
76 <SettingsScreen title="Client Storage">
77 <ScrollView
78 contentInsetAdjustmentBehavior="automatic"
79 contentInset={{ bottom: Math.max(insets.bottom, 18) }}
80 showsVerticalScrollIndicator={false}
81 className="flex-1"
82 contentContainerClassName="gap-6 px-5 pt-4 pb-[18px]"
83 >
84 <SettingsSection title="Environment caches">
85 {AsyncResult.isFailure(summaryResult) ? (
86 <View className="items-center gap-2 px-6 py-8">
87 <SymbolView
88 name="exclamationmark.triangle"
89 size={28}
90 tintColorClassName="accent-danger-foreground"
91 type="monochrome"
92 weight="regular"
93 />
94 <Text className="text-center text-base text-foreground">Storage unavailable</Text>
95 <Text className="text-center text-sm text-foreground-muted">
96 Restart the app and try again.
97 </Text>
98 </View>
99 ) : !summary ? (
100 <View className="items-center gap-3 px-6 py-8">
101 <ActivityIndicator />
102 <Text className="text-center text-sm text-foreground-muted">
103 Inspecting cached data…
104 </Text>
105 </View>
106 ) : environmentSummaries.length > 0 ? (
107 environmentSummaries.map((environment, index) => (
108 <CacheEnvironmentRow
109 key={environment.environmentId}
110 environment={environment}
111 environmentLabel={
112 savedConnectionsById[environment.environmentId]?.environmentLabel ??
113 environment.environmentId
114 }
115 machine={resolveEnvironmentMachineKind(
116 serverConfigs.get(environment.environmentId) ?? null,
117 )}
118 disabled={isClearing}
119 first={index === 0}
120 onClear={() => confirmClearEnvironment(environment)}
121 />
122 ))
123 ) : (
124 <View className="items-center gap-2 px-6 py-8">
125 <SymbolView
126 name="checkmark.circle"
127 size={28}
128 tintColorClassName="accent-icon"
129 type="monochrome"
130 weight="regular"
131 />
132 <Text className="text-center text-base text-foreground">No cached data</Text>
133 <Text className="text-center text-sm text-foreground-muted">
134 Offline cache records will appear here after environments are used.
135 </Text>
136 </View>
137 )}
138 </SettingsSection>
139
140 <View className="gap-3">
141 <SettingsSection title="Actions">
142 <Pressable
143 accessibilityRole="button"
144 disabled={isClearing || !summary || summary.recordCount === 0}
145 onPress={confirmClearAll}
146 className="flex-row items-center gap-4 p-4 disabled:opacity-40"
147 >
148 <SymbolView
149 name="trash"
150 size={22}
151 tintColorClassName="accent-danger-foreground"
152 type="monochrome"
153 weight="regular"
154 />
155 <Text className="flex-1 text-lg tabular-nums text-danger-foreground">
156 {summary ? `Clear ${formatBytes(summary.payloadBytes)}` : "Clear caches"}
157 </Text>
158 {isClearing ? <ActivityIndicator colorClassName="accent-danger-foreground" /> : null}
159 </Pressable>
160 </SettingsSection>
161 <Text className="px-2 text-sm leading-normal text-foreground-muted">
162 Clearing caches never removes environment connections, credentials, account data, or
163 appearance preferences.
164 </Text>
165 {AsyncResult.isFailure(summaryResult) || AsyncResult.isFailure(clearResult) ? (
166 <Text selectable className="px-2 text-sm text-danger-foreground">
167 Client storage is temporarily unavailable. Try again after restarting the app.
168 </Text>
169 ) : null}
170 </View>
171 </ScrollView>
172 </SettingsScreen>
173 );
174}
175
176function CacheEnvironmentRow(props: {
177 readonly environment: EnvironmentClientCacheSummary;
178 readonly environmentLabel: string;
179 readonly machine: EnvironmentMachineKind;
180 readonly disabled: boolean;
181 readonly first: boolean;
182 readonly onClear: () => void;
183}) {
184 return (
185 <View
186 className={
187 props.first
188 ? "flex-row items-center gap-3 p-4"
189 : "border-t border-border flex-row items-center gap-3 p-4"
190 }
191 >
192 <EnvironmentMachineSymbol kind={props.machine} size={22} tintColorClassName="accent-icon" />
193 <Text className="min-w-0 flex-1 text-base text-foreground" numberOfLines={1}>
194 {props.environmentLabel}
195 </Text>
196 <Pressable
197 accessibilityLabel={`Clear cache for ${props.environmentLabel}`}
198 accessibilityRole="button"
199 disabled={props.disabled}
200 onPress={props.onClear}
201 className="rounded-full px-3 py-2 disabled:opacity-40"
202 >
203 <Text className="font-t3-medium tabular-nums text-danger-foreground" numberOfLines={1}>
204 Clear {formatBytes(props.environment.payloadBytes)}
205 </Text>
206 </Pressable>
207 </View>
208 );
209}
210
211function formatBytes(bytes: number): string {
212 if (bytes < 1024) return `${bytes} B`;
213 if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(bytes < 10 * 1024 ? 1 : 0)} KB`;
214 return `${(bytes / (1024 * 1024)).toFixed(bytes < 10 * 1024 * 1024 ? 1 : 0)} MB`;
215}