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

ThreadSettingsSections.tsx

281 lines11.0 KB

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