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

ThreadSettingsSections.tsx

279 lines10.8 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 value={uniformMobileSetting(displayTargets, "autoResumeLimitedThreads")}
111 disabled={disabled}
112 onValueChange={(value) => writeToAll({ autoResumeLimitedThreads: value })}
113 />
114 <SettingsSwitchRow
115 icon="clock"
116 label="Snooze until limits reset"
117 value={uniformMobileSetting(displayTargets, "snoozeLimitedThreads")}
118 disabled={disabled}
119 onValueChange={(value) => writeToAll({ snoozeLimitedThreads: value })}
120 />
121 </SettingsSection>
122 ) : null}
123 <SettingsSection title="Auto-settle">
124 <SettingsSwitchRow
125 icon="arrow.triangle.branch"
126 label="Auto-settle merged threads"
127 value={referenceSettings.sidebarAutoSettleOnMerge}
128 disabled={disabled}
129 onValueChange={(value) => writeToAll({ sidebarAutoSettleOnMerge: value })}
130 />
131 <SettingsSwitchRow
132 icon="clock"
133 label="Auto-settle inactive threads"
134 value={afterDays !== null}
135 disabled={disabled}
136 onValueChange={(value) =>
137 writeToAll({ sidebarAutoSettleAfterDays: value ? AUTO_SETTLE_DEFAULT_DAYS : null })
138 }
139 />
140 {afterDays !== null ? (
141 <View className="flex-row items-center gap-4 px-4 py-4 android:min-h-14 android:py-3">
142 <View className="w-[22px] android:w-6" />
143 <Text className="flex-1 text-foreground text-lg android:text-base">Inactive days</Text>
144 <AutoSettleDaysField
145 value={afterDays}
146 disabled={disabled}
147 onValueChange={(value) => writeToAll({ sidebarAutoSettleAfterDays: value })}
148 />
149 </View>
150 ) : null}
151 </SettingsSection>
152 {!props.pending && mismatches.length > 0 ? (
153 <SettingsSection title="Across environments">
154 <View className="gap-3 p-4">
155 <Text className="text-base text-foreground">Auto-settle defaults differ</Text>
156 <Text className="text-sm text-foreground-muted">
157 {mismatches.map((mismatch) => mismatch.label).join(", ")}
158 </Text>
159 <Button
160 label="Apply auto-settle defaults"
161 tone="secondary"
162 disabled={disabled}
163 onPress={() => writeToAll(autoSettlePatch)}
164 />
165 </View>
166 </SettingsSection>
167 ) : null}
168 </View>
169 );
171​
172export function WorkingSectionSettings() {
173 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
174 const preferences = useAtomValue(mobilePreferencesAtom);
175 const workingShelfEnabled =
176 AsyncResult.isSuccess(preferences) && preferences.value.workingShelfEnabled !== false;
177​
178 return (
179 <View className="gap-3">
180 <SettingsSection title="Sidebar">
181 <SettingsSwitchRow
182 icon="bolt.circle"
183 label="Working section"
184 value={workingShelfEnabled}
185 onValueChange={(value) => savePreferences({ workingShelfEnabled: value })}
186 />
187 </SettingsSection>
188 <Text className="px-2 text-sm text-foreground-muted">
189 Fold working and monitoring threads into a Working section. They return to the top of the
190 list when they need you. While this is on, active threads are ordered by time and cannot be
191 moved.
192 </Text>
193 </View>
194 );
196​
197export function NewThreadSettingsSection() {
198 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
199 const preferences = useAtomValue(mobilePreferencesAtom);
200 const useLastProject =
201 AsyncResult.isSuccess(preferences) && preferences.value.useLastProjectForNewThreads !== false;
202​
203 return (
204 <SettingsSection title="New threads">
205 <SettingsSwitchRow
206 icon="square.and.pencil"
207 label="Use last project"
208 subtitle="Start new threads in the last project you used on this device."
209 value={useLastProject}
210 onValueChange={(value) => savePreferences({ useLastProjectForNewThreads: value })}
211 />
212 </SettingsSection>
213 );
215​
216/** Per machine, since each machine's own setting decides whether it links and answers links. */
217function BetaSettingsSection() {
218 const { environments } = useEnvironments();
219 const supported = environments.filter(
220 (environment) =>
221 environment.connection.phase === "connected" &&
222 environment.serverConfig?.environment.capabilities.peerLinks === true,
223 );
224 const writable = useEnvironmentsWithScope(supported, AuthSettingsWriteScope);
225 const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
226 label: "linked environments beta update",
227 reportFailure: true,
228 });
229 if (supported.length === 0) return null;
230 return (
231 <View className="gap-3">
232 <SettingsSection title="Beta: Linked environments">
233 {supported.map((environment) => (
234 <SettingsSwitchRow
235 key={environment.environmentId}
236 icon="link"
237 label={environment.label}
238 value={environment.serverConfig?.settings.linkedEnvironmentsEnabled === true}
239 disabled={!writable.has(environment.environmentId)}
240 onValueChange={(value) =>
241 void updateSettings({
242 environmentId: environment.environmentId,
243 input: { patch: { linkedEnvironmentsEnabled: value } },
244 })
245 }
246 />
247 ))}
248 </SettingsSection>
249 <Text className="px-2 text-sm text-foreground-muted">
250 Let agents on a machine work on your other machines, and theirs on it. Turn it on for each
251 machine. While it is off, nothing new starts over a link; tasks already running finish.
252 </Text>
253 </View>
254 );
256​
257function LegacySettingsSection() {
258 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
259 const preferences = useAtomValue(mobilePreferencesAtom);
260 const planModeEnabled =
261 AsyncResult.isSuccess(preferences) && preferences.value.planModeEnabled === true;
262​
263 return (
264 <View className="gap-3">
265 <SettingsSection title="Legacy">
266 <SettingsSwitchRow
267 icon="hammer"
268 label="Plan Mode"
269 value={planModeEnabled}
270 onValueChange={(value) => savePreferences({ planModeEnabled: value })}
271 />
272 </SettingsSection>
273 <Text className="px-2 text-sm text-foreground-muted">
274 Opt into retired interfaces kept for compatibility. Plan Mode restores the Build/Plan
275 control; otherwise every task runs in Build mode.
276 </Text>
277 </View>
278 );