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

SettingsClientStorageRouteScreen.tsx

182 lines7.5 KB

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 { 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 { ENVIRONMENT_MACHINE_SYMBOLS } 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 { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow";
22import { SettingsSection } from "./components/SettingsSection";
23import { SettingsScreen } from "./components/SettingsScreen";
24import { formatBytes } from "../../lib/formatBytes";
25​
26export function SettingsClientStorageRouteScreen() {
27 const insets = useSafeAreaInsets();
28 const summaryResult = useAtomValue(clientCacheSummaryAtom);
29 const clearResult = useAtomValue(clearClientCacheAtom);
30 const clearCache = useAtomSet(clearClientCacheAtom);
31 const { savedConnectionsById } = useSavedRemoteConnections();
32 const serverConfigs = useServerConfigs();
33 const isClearing = clearResult.waiting;
34 const summary = AsyncResult.isSuccess(summaryResult) ? summaryResult.value : null;
35 const environmentSummaries = useMemo(
36 () =>
37 [...(summary?.environments ?? [])].sort((left, right) => {
38 const leftLabel = savedConnectionsById[left.environmentId]?.environmentLabel ?? "";
39 const rightLabel = savedConnectionsById[right.environmentId]?.environmentLabel ?? "";
40 return leftLabel.localeCompare(rightLabel);
41 }),
42 [savedConnectionsById, summary?.environments],
43 );
44​
45 const confirmClearEnvironment = (environment: EnvironmentClientCacheSummary) => {
46 const label =
47 savedConnectionsById[environment.environmentId]?.environmentLabel ??
48 environment.environmentId;
49 Dialog.confirm({
50 title: `Clear cache for ${label}?`,
51 message:
52 "This removes offline threads, server metadata, and cached branches for this environment. The saved connection and credentials stay intact.",
53 confirmText: "Clear Cache",
54 destructive: true,
55 onConfirm: () =>
56 clearCache({ type: "environment", environmentId: environment.environmentId }),
57 });
58 };
59​
60 const confirmClearAll = () => {
61 Dialog.confirm({
62 title: "Clear all client caches?",
63 message:
64 "This removes offline data for every environment. Connections, credentials, account data, and app preferences stay intact.",
65 confirmText: "Clear All Caches",
66 destructive: true,
67 onConfirm: () => clearCache({ type: "all" }),
68 });
69 };
70​
71 return (
72 <SettingsScreen title="Client Storage">
73 <ScrollView
74 contentInsetAdjustmentBehavior="automatic"
75 contentInset={{ bottom: Math.max(insets.bottom, 18) }}
76 showsVerticalScrollIndicator={false}
77 className="flex-1"
78 contentContainerClassName="gap-6 px-5 pt-4 pb-[18px]"
79 >
80 <SettingsSection title="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">Storage unavailable</Text>
91 <Text className="text-center text-sm text-foreground-muted">
92 Restart the app and try again.
93 </Text>
94 </View>
95 ) : !summary ? (
96 <View className="items-center gap-3 px-6 py-8">
97 <AppActivityIndicator />
98 <Text className="text-center text-sm text-foreground-muted">
99 Inspecting cached data…
100 </Text>
101 </View>
102 ) : environmentSummaries.length > 0 ? (
103 environmentSummaries.map((environment, index) => (
104 <CacheEnvironmentRow
105 key={environment.environmentId}
106 environment={environment}
107 environmentLabel={
108 savedConnectionsById[environment.environmentId]?.environmentLabel ??
109 environment.environmentId
110 }
111 machine={resolveEnvironmentMachineKind(
112 serverConfigs.get(environment.environmentId) ?? null,
113 )}
114 disabled={isClearing}
115 first={index === 0}
116 onClear={() => confirmClearEnvironment(environment)}
117 />
118 ))
119 ) : (
120 <View className="items-center gap-2 px-6 py-8">
121 <SymbolView
122 name="checkmark.circle"
123 size={28}
124 tintColorClassName="accent-icon"
125 type="monochrome"
126 weight="regular"
127 />
128 <Text className="text-center text-base text-foreground">No cached data</Text>
129 <Text className="text-center text-sm text-foreground-muted">
130 Offline cache records will appear here after environments are used.
131 </Text>
132 </View>
133 )}
134 </SettingsSection>
135​
136 <View className="gap-3">
137 <SettingsSection title="Actions">
138 <SettingsActionRow
139 icon="trash"
140 label={summary ? `Clear ${formatBytes(summary.payloadBytes)}` : "Clear caches"}
141 tone="danger"
142 disabled={isClearing || !summary || summary.recordCount === 0}
143 loading={isClearing}
144 onPress={confirmClearAll}
145 />
146 </SettingsSection>
147 <Text className="px-2 text-sm leading-normal text-foreground-muted">
148 Clearing caches never removes environment connections, credentials, account data, or
149 appearance preferences.
150 </Text>
151 {AsyncResult.isFailure(summaryResult) || AsyncResult.isFailure(clearResult) ? (
152 <Text selectable className="px-2 text-sm text-danger-foreground">
153 Client storage is temporarily unavailable. Try again after restarting the app.
154 </Text>
155 ) : null}
156 </View>
157 </ScrollView>
158 </SettingsScreen>
159 );
161​
162function CacheEnvironmentRow(props: {
163 readonly environment: EnvironmentClientCacheSummary;
164 readonly environmentLabel: string;
165 readonly machine: EnvironmentMachineKind;
166 readonly disabled: boolean;
167 readonly first: boolean;
168 readonly onClear: () => void;
169}) {
170 return (
171 <View className={props.first ? undefined : "border-t border-border"}>
172 <SettingsActionRow
173 icon={ENVIRONMENT_MACHINE_SYMBOLS[props.machine]}
174 label={props.environmentLabel}
175 description={`Clear ${formatBytes(props.environment.payloadBytes)} of cached data`}
176 tone="danger"
177 disabled={props.disabled}
178 onPress={props.onClear}
179 />
180 </View>
181 );