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

ThreadSettingsSections.tsx

296 lines11.7 KB

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