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