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

SettingsClientStorageRouteScreen.tsx

215 lines8.5 KB

1import { useAtomSet, useAtomValue } from "@effect/atom-react";
2import { type EnvironmentMachineKind, resolveEnvironmentMachineKind } from "@t3tools/contracts";
3import { AsyncResult } from "effect/unstable/reactivity";
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 { SymbolView } from "../../components/AppSymbol";
10import { EnvironmentMachineSymbol } from "../../components/EnvironmentMachineSymbol";
11import {
12 clearClientCacheAtom,
13 clientCacheSummaryAtom,
14 type EnvironmentClientCacheSummary,
15} from "../../state/client-cache-state";
16import { useServerConfigs } from "../../state/entities";
17import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
18import { SettingsSection } from "./components/SettingsSection";
19​
20export function SettingsClientStorageRouteScreen() {
21 const insets = useSafeAreaInsets();
22 const summaryResult = useAtomValue(clientCacheSummaryAtom);
23 const clearResult = useAtomValue(clearClientCacheAtom);
24 const clearCache = useAtomSet(clearClientCacheAtom);
25 const { savedConnectionsById } = useSavedRemoteConnections();
26 const serverConfigs = useServerConfigs();
27 const isClearing = clearResult.waiting;
28 const summary = AsyncResult.isSuccess(summaryResult) ? summaryResult.value : null;
29 const environmentSummaries = useMemo(
30 () =>
31 [...(summary?.environments ?? [])].sort((left, right) => {
32 const leftLabel = savedConnectionsById[left.environmentId]?.environmentLabel ?? "";
33 const rightLabel = savedConnectionsById[right.environmentId]?.environmentLabel ?? "";
34 return leftLabel.localeCompare(rightLabel);
35 }),
36 [savedConnectionsById, summary?.environments],
37 );
38​
39 const confirmClearEnvironment = (environment: EnvironmentClientCacheSummary) => {
40 const label =
41 savedConnectionsById[environment.environmentId]?.environmentLabel ??
42 environment.environmentId;
43 Alert.alert(
44 `Clear cache for ${label}?`,
45 "This removes offline threads, server metadata, and cached branches for this environment. The saved connection and credentials stay intact.",
46 [
47 { text: "Cancel", style: "cancel" },
48 {
49 text: "Clear Cache",
50 style: "destructive",
51 onPress: () =>
52 clearCache({ type: "environment", environmentId: environment.environmentId }),
53 },
54 ],
55 );
56 };
57​
58 const confirmClearAll = () => {
59 Alert.alert(
60 "Clear all client caches?",
61 "This removes offline data for every environment. Connections, credentials, account data, and app preferences stay intact.",
62 [
63 { text: "Cancel", style: "cancel" },
64 {
65 text: "Clear All Caches",
66 style: "destructive",
67 onPress: () => clearCache({ type: "all" }),
68 },
69 ],
70 );
71 };
72​
73 return (
74 <View collapsable={false} className="flex-1 bg-sheet">
75 <ScrollView
76 contentInsetAdjustmentBehavior="automatic"
77 contentInset={{ bottom: Math.max(insets.bottom, 18) }}
78 showsVerticalScrollIndicator={false}
79 className="flex-1"
80 contentContainerClassName="gap-6 px-5 pt-4 pb-[18px]"
81 >
82 <SettingsSection title="Environment caches">
83 {AsyncResult.isFailure(summaryResult) ? (
84 <View className="items-center gap-2 px-6 py-8">
85 <SymbolView
86 name="exclamationmark.triangle"
87 size={28}
88 tintColorClassName={"accent-danger-foreground"}
89 type="monochrome"
90 weight="regular"
91 />
92 <Text className="text-center text-base text-foreground">Storage unavailable</Text>
93 <Text className="text-center text-sm text-foreground-muted">
94 Restart the app and try again.
95 </Text>
96 </View>
97 ) : !summary ? (
98 <View className="items-center gap-3 px-6 py-8">
99 <ActivityIndicator />
100 <Text className="text-center text-sm text-foreground-muted">
101 Inspecting cached data…
102 </Text>
103 </View>
104 ) : environmentSummaries.length > 0 ? (
105 environmentSummaries.map((environment, index) => (
106 <CacheEnvironmentRow
107 key={environment.environmentId}
108 environment={environment}
109 environmentLabel={
110 savedConnectionsById[environment.environmentId]?.environmentLabel ??
111 environment.environmentId
112 }
113 machine={resolveEnvironmentMachineKind(
114 serverConfigs.get(environment.environmentId) ?? null,
115 )}
116 disabled={isClearing}
117 first={index === 0}
118 onClear={() => confirmClearEnvironment(environment)}
119 />
120 ))
121 ) : (
122 <View className="items-center gap-2 px-6 py-8">
123 <SymbolView
124 name="checkmark.circle"
125 size={28}
126 tintColorClassName={"accent-icon"}
127 type="monochrome"
128 weight="regular"
129 />
130 <Text className="text-center text-base text-foreground">No cached data</Text>
131 <Text className="text-center text-sm text-foreground-muted">
132 Offline cache records will appear here after environments are used.
133 </Text>
134 </View>
135 )}
136 </SettingsSection>
137​
138 <View className="gap-3">
139 <SettingsSection title="Actions">
140 <Pressable
141 accessibilityRole="button"
142 disabled={isClearing || !summary || summary.recordCount === 0}
143 onPress={confirmClearAll}
144 className="flex-row items-center gap-4 p-4 disabled:opacity-40"
145 >
146 <SymbolView
147 name="trash"
148 size={22}
149 tintColorClassName={"accent-danger-foreground"}
150 type="monochrome"
151 weight="regular"
152 />
153 <Text className="flex-1 text-lg tabular-nums text-danger-foreground">
154 {summary ? `Clear ${formatBytes(summary.payloadBytes)}` : "Clear caches"}
155 </Text>
156 {isClearing ? (
157 <ActivityIndicator colorClassName={"accent-danger-foreground"} />
158 ) : 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 </View>
173 );
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 );
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`;