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