@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 199 lines 7.92 KB
Raw
1 import { T, useGT } from "gt-react-native";
2 import * as Dialog from "../../components/ui/Dialog";
3 import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
4 import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
5 import { useAtomSet, useAtomValue } from "@effect/atom-react";
6 import { type EnvironmentMachineKind, resolveEnvironmentMachineKind } from "@t3tools/contracts";
7 import { AsyncResult } from "effect/reactivity";
8 import { useMemo } from "react";
9 import { View } from "react-native";
10 import { useSafeAreaInsets } from "react-native-safe-area-context";
11
12 import { AppText as Text } from "../../components/ui/AppText";
13 import { SymbolView } from "../../components/ui/AppSymbol";
14 import { ENVIRONMENT_MACHINE_SYMBOLS } from "../../components/EnvironmentMachineSymbol";
15 import {
16 clearClientCacheAtom,
17 clientCacheSummaryAtom,
18 type EnvironmentClientCacheSummary,
19 } from "../../state/client-cache-state";
20 import { useServerConfigs } from "../../state/entities";
21 import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
22 import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow";
23 import { SettingsSection } from "./components/SettingsSection";
24 import { SettingsScreen } from "./components/SettingsScreen";
25 import { formatBytes } from "../../lib/formatBytes";
26
27 export 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}?", { environment: label }),
53 message: gt(
54 "This removes offline threads, server metadata, and cached branches for this environment. The saved connection and credentials stay intact.",
55 ),
56 confirmText: gt("Clear Cache"),
57 destructive: true,
58 onConfirm: () =>
59 clearCache({ type: "environment", environmentId: environment.environmentId }),
60 });
61 };
62
63 const confirmClearAll = () => {
64 Dialog.confirm({
65 title: gt("Clear all client caches?"),
66 message: gt(
67 "This removes offline data for every environment. Connections, credentials, account data, and app preferences stay intact.",
68 ),
69 confirmText: gt("Clear All Caches"),
70 destructive: true,
71 onConfirm: () => clearCache({ type: "all" }),
72 });
73 };
74
75 return (
76 <SettingsScreen title={gt("Client Storage")}>
77 <ScrollView
78 contentInsetAdjustmentBehavior="automatic"
79 showsVerticalScrollIndicator={false}
80 className="flex-1"
81 contentContainerClassName="gap-6 px-5 pt-4"
82 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
83 >
84 <SettingsSection title={gt("Environment caches")}>
85 {AsyncResult.isFailure(summaryResult) ? (
86 <View className="items-center gap-2 px-6 py-8">
87 <SymbolView
88 name="exclamationmark.triangle"
89 size={28}
90 tintColorClassName="accent-danger-foreground"
91 type="monochrome"
92 weight="regular"
93 />
94 <Text className="text-center text-base text-foreground">
95 <T>Storage unavailable</T>
96 </Text>
97 <Text className="text-center text-sm text-foreground-muted">
98 <T>Restart the app and try again.</T>
99 </Text>
100 </View>
101 ) : !summary ? (
102 <View className="items-center gap-3 px-6 py-8">
103 <AppActivityIndicator />
104 <Text className="text-center text-sm text-foreground-muted">
105 <T>Inspecting cached data…</T>
106 </Text>
107 </View>
108 ) : environmentSummaries.length > 0 ? (
109 environmentSummaries.map((environment, index) => (
110 <CacheEnvironmentRow
111 key={environment.environmentId}
112 environment={environment}
113 environmentLabel={
114 savedConnectionsById[environment.environmentId]?.environmentLabel ??
115 environment.environmentId
116 }
117 machine={resolveEnvironmentMachineKind(
118 serverConfigs.get(environment.environmentId) ?? null,
119 )}
120 disabled={isClearing}
121 first={index === 0}
122 onClear={() => confirmClearEnvironment(environment)}
123 />
124 ))
125 ) : (
126 <View className="items-center gap-2 px-6 py-8">
127 <SymbolView
128 name="checkmark.circle"
129 size={28}
130 tintColorClassName="accent-icon"
131 type="monochrome"
132 weight="regular"
133 />
134 <Text className="text-center text-base text-foreground">
135 <T>No cached data</T>
136 </Text>
137 <Text className="text-center text-sm text-foreground-muted">
138 <T>Offline cache records will appear here after environments are used.</T>
139 </Text>
140 </View>
141 )}
142 </SettingsSection>
143
144 <View className="gap-3">
145 <SettingsSection title={gt("Actions")}>
146 <SettingsActionRow
147 icon="trash"
148 label={
149 summary
150 ? gt("Clear {size}", { size: formatBytes(summary.payloadBytes) })
151 : gt("Clear caches")
152 }
153 tone="danger"
154 disabled={isClearing || !summary || summary.recordCount === 0}
155 loading={isClearing}
156 onPress={confirmClearAll}
157 />
158 </SettingsSection>
159 <Text className="px-2 text-sm leading-normal text-foreground-muted">
160 <T>
161 Clearing caches never removes environment connections, credentials, account data, or
162 appearance preferences.
163 </T>
164 </Text>
165 {AsyncResult.isFailure(summaryResult) || AsyncResult.isFailure(clearResult) ? (
166 <Text selectable className="px-2 text-sm text-danger-foreground">
167 <T>Client storage is temporarily unavailable. Try again after restarting the app.</T>
168 </Text>
169 ) : null}
170 </View>
171 </ScrollView>
172 </SettingsScreen>
173 );
174 }
175
176 function 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 const gt = useGT();
185 return (
186 <View className={props.first ? undefined : "border-t border-border"}>
187 <SettingsActionRow
188 icon={ENVIRONMENT_MACHINE_SYMBOLS[props.machine]}
189 label={props.environmentLabel}
190 description={gt("Clear {size} of cached data", {
191 size: formatBytes(props.environment.payloadBytes),
192 })}
193 tone="danger"
194 disabled={props.disabled}
195 onPress={props.onClear}
196 />
197 </View>
198 );
199 }