Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SettingsClientStorageRouteScreen.tsx

213 lines8.1 KB

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