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

main
tsx 331 lines 12.6 KB
Raw
1 import { T, useGT } from "gt-react-native";
2 import { Button } from "../../components/ui/Button";
3 import {
4 AuthSettingsWriteScope,
5 isManagedSettingPath,
6 type ServerSettingsPatch,
7 } from "@t3tools/contracts";
8 import { useEnvironmentsWithScope } from "../../state/session";
9 import { AutoSettleDaysField } from "./components/AutoSettleDaysField";
10 import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
11 import { useAtomValue } from "@effect/atom-react";
12 import { AsyncResult } from "effect/reactivity";
13 import type { ComponentProps } from "react";
14 import { View } from "react-native";
15 import { useSafeAreaInsets } from "react-native-safe-area-context";
16
17 import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
18 import { supportsSharedSettingsSync } from "@t3tools/client-runtime/state/shared-settings";
19 import { AppText as Text } from "../../components/ui/AppText";
20 import { useEnvironments } from "../../state/environments";
21 import { mobilePreferencesAtom, saveMobilePreferences } from "../../state/preferences";
22 import { serverEnvironment } from "../../state/server";
23 import { useAtomCommand } from "../../state/use-atom-command";
24 import { SettingsSection } from "./components/SettingsSection";
25 import { SettingsSwitchRow } from "./components/SettingsSwitchRow";
26 import { SettingsScreen } from "./components/SettingsScreen";
27 import { planAutoSettleSettingsSync, type AutoSettleSettings } from "./autoSettleSettingsSync";
28 import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
29 import {
30 managedSettingReason,
31 resolveMobileSettingsTargets,
32 uniformMobileSetting,
33 } from "./settings-scoped-server";
34
35 export function SettingsLabsRouteScreen() {
36 const gt = useGT();
37 const insets = useSafeAreaInsets();
38
39 return (
40 <SettingsScreen title={gt("Labs")}>
41 <ScrollView
42 contentInsetAdjustmentBehavior="automatic"
43 showsVerticalScrollIndicator={false}
44 className="flex-1"
45 contentContainerClassName="gap-6 px-5 pt-4"
46 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
47 >
48 <MachineBetaSection
49 title={gt("Linked environments")}
50 icon="link"
51 capability="peerLinks"
52 setting="linkedEnvironmentsEnabled"
53 description={gt(
54 "Let agents on a machine work on your other machines, and theirs on it. Turn it on for each machine. While off, nothing new starts over a link; running tasks finish.",
55 )}
56 />
57 <MachineBetaSection
58 title={gt("Fast worktree checkout")}
59 icon="bolt.circle"
60 capability="fastWorktreeCheckout"
61 setting="fastWorktreeCheckout"
62 description={gt(
63 "On a Mac, clone new worktrees from a checkout cached in the T3 home folder instead of writing every file. Uses Git’s normal checkout when it can’t. Turning it off deletes the cache.",
64 )}
65 />
66 </ScrollView>
67 </SettingsScreen>
68 );
69 }
70
71 const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_SERVER_SETTINGS.sidebarAutoSettleAfterDays ?? 7;
72
73 /**
74 * Usage-limit and auto-settle rows. The Threads page owns their writes, so they share its queue
75 * with the page's other project overrides, and their project override reset.
76 */
77 export function AutoSettleSettingsRows(props: {
78 readonly write: (patch: ServerSettingsPatch) => void;
79 readonly pending: boolean;
80 }) {
81 const gt = useGT();
82 const { selectedTargets, projectGroups, selectedProjectKey } = useSettingsEnvironmentFilter();
83 const selectedProject = projectGroups.find((group) => group.key === selectedProjectKey);
84 const writableEnvironments = useEnvironmentsWithScope(selectedTargets, AuthSettingsWriteScope);
85 const projectSelected = selectedProjectKey !== null;
86
87 const projectMembers = projectSelected
88 ? (selectedProject?.members.map((member) => member.project) ?? [])
89 : null;
90 const displayTargets = resolveMobileSettingsTargets(
91 selectedTargets.filter(supportsSharedSettingsSync),
92 projectMembers,
93 );
94 // The page saves to all its targets, so any read-only one blocks the save.
95 const canWriteSettings =
96 displayTargets.length > 0 &&
97 resolveMobileSettingsTargets(selectedTargets, projectMembers).every((target) =>
98 writableEnvironments.has(target.environment.environmentId),
99 );
100 const reference = displayTargets[0] ?? null;
101 const referenceSettings = reference?.settings ?? null;
102
103 if (reference === null || referenceSettings === null) {
104 return null;
105 }
106
107 const writeToAll = (
108 patch: Partial<AutoSettleSettings> & {
109 autoResumeLimitedThreads?: boolean;
110 snoozeLimitedThreads?: boolean;
111 },
112 ) => props.write(patch);
113
114 const { patch: autoSettlePatch, mismatches } = planAutoSettleSettingsSync(
115 {
116 environmentId: reference.environment.environmentId,
117 projectId: reference.projectId,
118 settings: referenceSettings,
119 },
120 displayTargets.map((target) => ({
121 environmentId: target.environment.environmentId,
122 projectId: target.projectId,
123 label: target.environment.label,
124 settings: target.settings,
125 })),
126 );
127
128 const disabled = !canWriteSettings || props.pending;
129 const disabledFor = (...keys: string[]) =>
130 disabled || managedSettingReason(displayTargets, keys) !== undefined;
131 const afterDays = referenceSettings.sidebarAutoSettleAfterDays;
132
133 return (
134 <View className="gap-6">
135 {!projectSelected ? (
136 <SettingsSection title={gt("Usage limits")}>
137 <SettingsSwitchRow
138 icon="clock"
139 label={gt("Resume after usage limits")}
140 subtitle={gt(
141 "Automatically resume threads when their usage limits reset. You can cancel auto-resume in each thread.",
142 )}
143 value={uniformMobileSetting(displayTargets, "autoResumeLimitedThreads")}
144 disabled={disabledFor("autoResumeLimitedThreads")}
145 onValueChange={(value) => writeToAll({ autoResumeLimitedThreads: value })}
146 />
147 <SettingsSwitchRow
148 icon="clock"
149 label={gt("Snooze until limits reset")}
150 subtitle={gt(
151 "Snooze threads until their usage limits reset. Enable auto-resume to continue the agent after the reset.",
152 )}
153 value={uniformMobileSetting(displayTargets, "snoozeLimitedThreads")}
154 disabled={disabledFor("snoozeLimitedThreads")}
155 onValueChange={(value) => writeToAll({ snoozeLimitedThreads: value })}
156 />
157 </SettingsSection>
158 ) : null}
159 <SettingsSection title={gt("Auto-settle")}>
160 <SettingsSwitchRow
161 icon="arrow.triangle.branch"
162 label={gt("Auto-settle merged threads")}
163 value={referenceSettings.sidebarAutoSettleOnMerge}
164 disabled={disabledFor("sidebarAutoSettleOnMerge")}
165 onValueChange={(value) => writeToAll({ sidebarAutoSettleOnMerge: value })}
166 />
167 <SettingsSwitchRow
168 icon="clock"
169 label={gt("Auto-settle inactive threads")}
170 value={afterDays !== null}
171 disabled={disabledFor("sidebarAutoSettleAfterDays")}
172 onValueChange={(value) =>
173 writeToAll({ sidebarAutoSettleAfterDays: value ? AUTO_SETTLE_DEFAULT_DAYS : null })
174 }
175 />
176 {afterDays !== null ? (
177 <View className="flex-row items-center gap-4 px-4 py-4 android:min-h-14 android:py-3">
178 <View className="w-[22px] android:w-6" />
179 <Text className="flex-1 text-foreground text-lg android:text-base">
180 <T>Inactive days</T>
181 </Text>
182 <AutoSettleDaysField
183 value={afterDays}
184 disabled={disabledFor("sidebarAutoSettleAfterDays")}
185 onValueChange={(value) => writeToAll({ sidebarAutoSettleAfterDays: value })}
186 />
187 </View>
188 ) : null}
189 </SettingsSection>
190 {!props.pending && mismatches.length > 0 ? (
191 <SettingsSection title={gt("Across environments")}>
192 <View className="gap-3 p-4">
193 <Text className="text-base text-foreground">
194 <T>Auto-settle defaults differ</T>
195 </Text>
196 <Text className="text-sm text-foreground-muted">
197 {mismatches.map((mismatch) => mismatch.label).join(", ")}
198 </Text>
199 <Button
200 label={gt("Apply auto-settle defaults")}
201 tone="secondary"
202 disabled={disabledFor("sidebarAutoSettleOnMerge", "sidebarAutoSettleAfterDays")}
203 onPress={() => writeToAll(autoSettlePatch)}
204 />
205 </View>
206 </SettingsSection>
207 ) : null}
208 </View>
209 );
210 }
211
212 export function WorkingSectionSettings() {
213 const gt = useGT();
214 const savePreferences = useAtomCommand(saveMobilePreferences);
215 const preferences = useAtomValue(mobilePreferencesAtom);
216 const workingShelfEnabled =
217 AsyncResult.isSuccess(preferences) && preferences.value.workingShelfEnabled !== false;
218
219 return (
220 <View className="gap-3">
221 <SettingsSection title={gt("Sidebar")}>
222 <SettingsSwitchRow
223 icon="bolt.circle"
224 label={gt("Working section")}
225 value={workingShelfEnabled}
226 onValueChange={(value) => {
227 void savePreferences({ workingShelfEnabled: value });
228 }}
229 />
230 </SettingsSection>
231 <Text className="px-2 text-sm text-foreground-muted">
232 <T>
233 Fold working and monitoring threads into a Working section. They return to the top of the
234 list when they need you. While this is on, active threads are ordered by time and cannot
235 be moved.
236 </T>
237 </Text>
238 </View>
239 );
240 }
241
242 export function NewThreadSettingsSection() {
243 const gt = useGT();
244 const savePreferences = useAtomCommand(saveMobilePreferences);
245 const preferences = useAtomValue(mobilePreferencesAtom);
246 const useLastProject =
247 AsyncResult.isSuccess(preferences) && preferences.value.useLastProjectForNewThreads !== false;
248
249 return (
250 <SettingsSection title={gt("New threads")}>
251 <SettingsSwitchRow
252 icon="square.and.pencil"
253 label={gt("Use last project")}
254 subtitle={gt("Start new threads in the last project you used on this device.")}
255 value={useLastProject}
256 onValueChange={(value) => {
257 void savePreferences({ useLastProjectForNewThreads: value });
258 }}
259 />
260 </SettingsSection>
261 );
262 }
263
264 type MachineBetaSetting = "linkedEnvironmentsEnabled" | "fastWorktreeCheckout";
265
266 /** One switch per connected machine that offers the beta, since each machine's own setting decides. */
267 function MachineBetaSection({
268 title,
269 icon,
270 capability,
271 setting,
272 description,
273 }: {
274 readonly title: string;
275 readonly icon: ComponentProps<typeof SettingsSwitchRow>["icon"];
276 readonly capability: "peerLinks" | "fastWorktreeCheckout";
277 readonly setting: MachineBetaSetting;
278 readonly description: string;
279 }) {
280 const gt = useGT();
281 const { environments } = useEnvironments();
282 const supported = environments.filter(
283 (environment) =>
284 environment.connection.phase === "connected" &&
285 environment.serverConfig?.environment.capabilities[capability] === true,
286 );
287 const writable = useEnvironmentsWithScope(supported, AuthSettingsWriteScope);
288 const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
289 label: `${setting} beta update`,
290 reportFailure: true,
291 });
292 if (supported.length === 0) return null;
293 return (
294 <View className="gap-3">
295 <SettingsSection title={gt("Beta: {feature}", { feature: title })}>
296 {supported.map((environment) => {
297 const policy = environment.serverConfig?.managedPolicy;
298 const managed = isManagedSettingPath(policy, setting);
299 return (
300 <SettingsSwitchRow
301 key={environment.environmentId}
302 icon={icon}
303 label={environment.label}
304 {...(managed
305 ? {
306 subtitle: gt("Managed by {organization}.", {
307 organization: policy?.organization,
308 }),
309 }
310 : {})}
311 value={environment.serverConfig?.settings[setting] === true}
312 disabled={managed || !writable.has(environment.environmentId)}
313 onValueChange={(value) =>
314 void updateSettings({
315 environmentId: environment.environmentId,
316 input: {
317 patch:
318 setting === "linkedEnvironmentsEnabled"
319 ? { linkedEnvironmentsEnabled: value }
320 : { fastWorktreeCheckout: value },
321 },
322 })
323 }
324 />
325 );
326 })}
327 </SettingsSection>
328 <Text className="px-2 text-sm text-foreground-muted">{description}</Text>
329 </View>
330 );
331 }