Open History to see the last commit
1import { AutoSettleDaysField } from "./components/AutoSettleDaysField";
2import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView";
3import { useAtomSet, useAtomValue } from "@effect/atom-react";
4import { AsyncResult } from "effect/unstable/reactivity";
5import { useRef, useState } from "react";
6import { Pressable, View } from "react-native";
7import { useSafeAreaInsets } from "react-native-safe-area-context";
8
9import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
10import { supportsSharedSettingsSync } from "@t3tools/client-runtime/state/shared-settings";
11import { AppText as Text } from "../../components/AppText";
12import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../state/preferences";
13import { serverEnvironment } from "../../state/server";
14import { useAtomCommand } from "../../state/use-atom-command";
15import { SettingsSection } from "./components/SettingsSection";
16import { SettingsProjectOverridesSection } from "./components/SettingsProjectOverridesSection";
17import { SettingsSwitchRow } from "./components/SettingsSwitchRow";
18import { SettingsScreen } from "./components/SettingsScreen";
19import {
20 AndroidSettingsEnvironmentFilter,
21 SettingsEnvironmentFilterHeader,
22} from "./components/SettingsEnvironmentFilterHeader";
23import { planAutoSettleSettingsSync, type AutoSettleSettings } from "./autoSettleSettingsSync";
24import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
25import {
26 planMobileScopedSettingsClear,
27 planMobileScopedSettingsPatch,
28 resolveMobileSettingsTargets,
29 uniformMobileSetting,
30 type ScopedMobileSettingsTarget,
31} from "./settings-scoped-server";
32
33export function SettingsThreadsRouteScreen() {
34 const insets = useSafeAreaInsets();
35
36 return (
37 <>
38 <SettingsEnvironmentFilterHeader />
39 <SettingsScreen title="Thread behavior" trailing={<AndroidSettingsEnvironmentFilter />}>
40 <ScrollView
41 contentInsetAdjustmentBehavior="automatic"
42 showsVerticalScrollIndicator={false}
43 className="flex-1"
44 contentContainerClassName="gap-6 px-5 pt-4"
45 contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }}
46 >
47 <AutoSettleSettingsRows />
48 <LegacySettingsSection />
49 </ScrollView>
50 </SettingsScreen>
51 </>
52 );
53}
54
55const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_SERVER_SETTINGS.sidebarAutoSettleAfterDays ?? 3;
56
57/**
58 * Mobile edits auto-settle defaults across selected capable targets.
59 */
60function AutoSettleSettingsRows() {
61 const { selectedTargets, projectGroups, selectedProjectKey } = useSettingsEnvironmentFilter();
62 const selectedProject = projectGroups.find((group) => group.key === selectedProjectKey);
63 const projectSelected = selectedProjectKey !== null;
64 const [pendingWrites, setPendingWrites] = useState(0);
65 const writeInFlight = useRef(false);
66 const [pendingTargets, setPendingTargets] = useState<
67 readonly ScopedMobileSettingsTarget[] | null
68 >(null);
69 const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
70 label: "server settings update",
71 reportFailure: true,
72 });
73
74 const syncEnvironments = selectedTargets.filter(supportsSharedSettingsSync);
75 const syncTargets = resolveMobileSettingsTargets(
76 syncEnvironments,
77 projectSelected ? (selectedProject?.members.map((member) => member.project) ?? []) : null,
78 );
79 const displayTargets =
80 pendingWrites > 0 && pendingTargets !== null ? pendingTargets : syncTargets;
81 const reference = displayTargets[0] ?? null;
82 const referenceSettings = reference?.settings ?? null;
83
84 if (reference === null || referenceSettings === null) {
85 return null;
86 }
87
88 const writeToAll = (
89 patch: Partial<AutoSettleSettings> & {
90 autoResumeLimitedThreads?: boolean;
91 snoozeLimitedThreads?: boolean;
92 },
93 ) => {
94 if (writeInFlight.current) return;
95 const writes = planMobileScopedSettingsPatch(syncTargets, projectSelected, patch);
96 if (writes.length === 0) return;
97 writeInFlight.current = true;
98 setPendingTargets(syncTargets);
99 setPendingWrites((count) => count + 1);
100 void Promise.allSettled(
101 writes.map((entry) =>
102 updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
103 ),
104 ).finally(() => {
105 writeInFlight.current = false;
106 setPendingTargets(null);
107 setPendingWrites((count) => count - 1);
108 });
109 };
110
111 const { patch: autoSettlePatch, mismatches } = planAutoSettleSettingsSync(
112 {
113 environmentId: reference.environment.environmentId,
114 projectId: reference.projectId,
115 settings: referenceSettings,
116 },
117 displayTargets.map((target) => ({
118 environmentId: target.environment.environmentId,
119 projectId: target.projectId,
120 label: target.environment.label,
121 settings: target.settings,
122 })),
123 );
124
125 const supportsProjectOverrides = syncTargets.every(
126 (target) =>
127 target.environment.serverConfig.environment.capabilities.projectSettingsOverrides === true,
128 );
129 const disabled = pendingWrites > 0 || (projectSelected && !supportsProjectOverrides);
130 const hasProjectOverrides =
131 projectSelected &&
132 syncTargets.some(
133 (target) =>
134 target.sources.sidebarAutoSettleOnMerge === "project" ||
135 target.sources.sidebarAutoSettleAfterDays === "project",
136 );
137 const clearProjectOverrides = () => {
138 if (writeInFlight.current) return;
139 const writes = planMobileScopedSettingsClear(syncTargets, [
140 "sidebarAutoSettleOnMerge",
141 "sidebarAutoSettleAfterDays",
142 ]);
143 if (writes.length === 0) return;
144 writeInFlight.current = true;
145 setPendingTargets(syncTargets);
146 setPendingWrites((count) => count + 1);
147 void Promise.allSettled(
148 writes.map((entry) =>
149 updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
150 ),
151 ).finally(() => {
152 writeInFlight.current = false;
153 setPendingTargets(null);
154 setPendingWrites((count) => count - 1);
155 });
156 };
157
158 const afterDays = referenceSettings.sidebarAutoSettleAfterDays;
159
160 return (
161 <View className="gap-6">
162 {projectSelected ? (
163 <SettingsProjectOverridesSection
164 projectLabel={selectedProject?.label ?? "Unavailable project"}
165 hasOverrides={hasProjectOverrides}
166 supportsOverrides={supportsProjectOverrides}
167 pending={pendingWrites > 0}
168 onClear={clearProjectOverrides}
169 />
170 ) : null}
171 {!projectSelected ? (
172 <SettingsSection title="Usage limits">
173 <SettingsSwitchRow
174 icon="clock"
175 label="Auto-resume limited threads"
176 value={uniformMobileSetting(displayTargets, "autoResumeLimitedThreads")}
177 disabled={disabled}
178 onValueChange={(value) => writeToAll({ autoResumeLimitedThreads: value })}
179 />
180 <SettingsSwitchRow
181 icon="clock"
182 label="Snooze limited threads"
183 value={uniformMobileSetting(displayTargets, "snoozeLimitedThreads")}
184 disabled={disabled}
185 onValueChange={(value) => writeToAll({ snoozeLimitedThreads: value })}
186 />
187 </SettingsSection>
188 ) : null}
189 <SettingsSection title="Auto-settle">
190 <SettingsSwitchRow
191 icon="arrow.triangle.branch"
192 label="Auto-settle merged threads"
193 value={referenceSettings.sidebarAutoSettleOnMerge}
194 disabled={disabled}
195 onValueChange={(value) => writeToAll({ sidebarAutoSettleOnMerge: value })}
196 />
197 <SettingsSwitchRow
198 icon="clock"
199 label="Auto-settle inactive threads"
200 value={afterDays !== null}
201 disabled={disabled}
202 onValueChange={(value) =>
203 writeToAll({ sidebarAutoSettleAfterDays: value ? AUTO_SETTLE_DEFAULT_DAYS : null })
204 }
205 />
206 {afterDays !== null ? (
207 <View className="flex-row items-center gap-4 px-4 py-4 android:min-h-14 android:py-3">
208 <View className="w-[22px] android:w-6" />
209 <Text className="flex-1 text-foreground text-lg android:text-base">Inactive days</Text>
210 <AutoSettleDaysField
211 value={afterDays}
212 disabled={disabled}
213 onValueChange={(value) => writeToAll({ sidebarAutoSettleAfterDays: value })}
214 />
215 </View>
216 ) : null}
217 </SettingsSection>
218 {pendingWrites === 0 && mismatches.length > 0 ? (
219 <SettingsSection title="Across environments">
220 <View className="gap-3 p-4">
221 <Text className="text-base text-foreground">Auto-settle defaults differ</Text>
222 <Text className="text-sm text-foreground-muted">
223 {mismatches.map((mismatch) => mismatch.label).join(", ")}
224 </Text>
225 <Pressable
226 accessibilityRole="button"
227 disabled={disabled}
228 onPress={() => writeToAll(autoSettlePatch)}
229 className="self-start rounded-full bg-subtle px-4 py-2 active:opacity-70"
230 >
231 <Text className="text-sm font-t3-medium text-foreground">
232 Apply auto-settle defaults
233 </Text>
234 </Pressable>
235 </View>
236 </SettingsSection>
237 ) : null}
238 </View>
239 );
240}
241
242/**
243 * Device-local legacy toggles. Mobile has no client-settings sync, so this is
244 * the counterpart of web's Settings → General → Legacy features backed by
245 * mobile preferences.
246 */
247function LegacySettingsSection() {
248 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
249 const preferences = useAtomValue(mobilePreferencesAtom);
250 const planModeEnabled =
251 AsyncResult.isSuccess(preferences) && preferences.value.planModeEnabled === true;
252
253 return (
254 <View className="gap-3">
255 <SettingsSection title="Legacy">
256 <SettingsSwitchRow
257 icon="hammer"
258 label="Plan Mode"
259 value={planModeEnabled}
260 onValueChange={(value) => savePreferences({ planModeEnabled: value })}
261 />
262 </SettingsSection>
263 <Text className="px-2 text-sm text-foreground-muted">
264 Opt into retired interfaces kept for compatibility. Plan Mode restores the Build/Plan
265 control; otherwise every task runs in Build mode.
266 </Text>
267 </View>
268 );
269}