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