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