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

SettingsClientStorageRouteScreen.tsx

193 lines7.8 KB

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