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 <BetaSettingsSection />
49 <LegacySettingsSection />
50 </ScrollView>
51 </SettingsScreen>
52 </>
53 );
54}
55
56const AUTO_SETTLE_DEFAULT_DAYS = DEFAULT_SERVER_SETTINGS.sidebarAutoSettleAfterDays ?? 3;
57
58/**
59 * Mobile edits auto-settle defaults across selected capable targets.
60 */
61function AutoSettleSettingsRows() {
62 const { selectedTargets, projectGroups, selectedProjectKey } = useSettingsEnvironmentFilter();
63 const selectedProject = projectGroups.find((group) => group.key === selectedProjectKey);
64 const projectSelected = selectedProjectKey !== null;
65 const [pendingWrites, setPendingWrites] = useState(0);
66 const writeInFlight = useRef(false);
67 const [pendingTargets, setPendingTargets] = useState<
68 readonly ScopedMobileSettingsTarget[] | null
69 >(null);
70 const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
71 label: "server settings update",
72 reportFailure: true,
73 });
74
75 const syncEnvironments = selectedTargets.filter(supportsSharedSettingsSync);
76 const syncTargets = resolveMobileSettingsTargets(
77 syncEnvironments,
78 projectSelected ? (selectedProject?.members.map((member) => member.project) ?? []) : null,
79 );
80 const displayTargets =
81 pendingWrites > 0 && pendingTargets !== null ? pendingTargets : syncTargets;
82 const reference = displayTargets[0] ?? null;
83 const referenceSettings = reference?.settings ?? null;
84
85 if (reference === null || referenceSettings === null) {
86 return null;
87 }
88
89 const writeToAll = (
90 patch: Partial<AutoSettleSettings> & {
91 autoResumeLimitedThreads?: boolean;
92 snoozeLimitedThreads?: boolean;
93 },
94 ) => {
95 if (writeInFlight.current) return;
96 const writes = planMobileScopedSettingsPatch(syncTargets, projectSelected, patch);
97 if (writes.length === 0) return;
98 writeInFlight.current = true;
99 setPendingTargets(syncTargets);
100 setPendingWrites((count) => count + 1);
101 void Promise.allSettled(
102 writes.map((entry) =>
103 updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
104 ),
105 ).finally(() => {
106 writeInFlight.current = false;
107 setPendingTargets(null);
108 setPendingWrites((count) => count - 1);
109 });
110 };
111
112 const { patch: autoSettlePatch, mismatches } = planAutoSettleSettingsSync(
113 {
114 environmentId: reference.environment.environmentId,
115 projectId: reference.projectId,
116 settings: referenceSettings,
117 },
118 displayTargets.map((target) => ({
119 environmentId: target.environment.environmentId,
120 projectId: target.projectId,
121 label: target.environment.label,
122 settings: target.settings,
123 })),
124 );
125
126 const supportsProjectOverrides = syncTargets.every(
127 (target) =>
128 target.environment.serverConfig.environment.capabilities.projectSettingsOverrides === true,
129 );
130 const disabled = pendingWrites > 0 || (projectSelected && !supportsProjectOverrides);
131 const hasProjectOverrides =
132 projectSelected &&
133 syncTargets.some(
134 (target) =>
135 target.sources.sidebarAutoSettleOnMerge === "project" ||
136 target.sources.sidebarAutoSettleAfterDays === "project",
137 );
138 const clearProjectOverrides = () => {
139 if (writeInFlight.current) return;
140 const writes = planMobileScopedSettingsClear(syncTargets, [
141 "sidebarAutoSettleOnMerge",
142 "sidebarAutoSettleAfterDays",
143 ]);
144 if (writes.length === 0) return;
145 writeInFlight.current = true;
146 setPendingTargets(syncTargets);
147 setPendingWrites((count) => count + 1);
148 void Promise.allSettled(
149 writes.map((entry) =>
150 updateSettings({ environmentId: entry.environmentId, input: { patch: entry.patch } }),
151 ),
152 ).finally(() => {
153 writeInFlight.current = false;
154 setPendingTargets(null);
155 setPendingWrites((count) => count - 1);
156 });
157 };
158
159 const afterDays = referenceSettings.sidebarAutoSettleAfterDays;
160
161 return (
162 <View className="gap-6">
163 {projectSelected ? (
164 <SettingsProjectOverridesSection
165 projectLabel={selectedProject?.label ?? "Unavailable project"}
166 hasOverrides={hasProjectOverrides}
167 supportsOverrides={supportsProjectOverrides}
168 pending={pendingWrites > 0}
169 onClear={clearProjectOverrides}
170 />
171 ) : null}
172 {!projectSelected ? (
173 <SettingsSection title="Usage limits">
174 <SettingsSwitchRow
175 icon="clock"
176 label="Auto-resume limited threads"
177 value={uniformMobileSetting(displayTargets, "autoResumeLimitedThreads")}
178 disabled={disabled}
179 onValueChange={(value) => writeToAll({ autoResumeLimitedThreads: value })}
180 />
181 <SettingsSwitchRow
182 icon="clock"
183 label="Snooze limited threads"
184 value={uniformMobileSetting(displayTargets, "snoozeLimitedThreads")}
185 disabled={disabled}
186 onValueChange={(value) => writeToAll({ snoozeLimitedThreads: value })}
187 />
188 </SettingsSection>
189 ) : null}
190 <SettingsSection title="Auto-settle">
191 <SettingsSwitchRow
192 icon="arrow.triangle.branch"
193 label="Auto-settle merged threads"
194 value={referenceSettings.sidebarAutoSettleOnMerge}
195 disabled={disabled}
196 onValueChange={(value) => writeToAll({ sidebarAutoSettleOnMerge: value })}
197 />
198 <SettingsSwitchRow
199 icon="clock"
200 label="Auto-settle inactive threads"
201 value={afterDays !== null}
202 disabled={disabled}
203 onValueChange={(value) =>
204 writeToAll({ sidebarAutoSettleAfterDays: value ? AUTO_SETTLE_DEFAULT_DAYS : null })
205 }
206 />
207 {afterDays !== null ? (
208 <View className="flex-row items-center gap-4 px-4 py-4 android:min-h-14 android:py-3">
209 <View className="w-[22px] android:w-6" />
210 <Text className="flex-1 text-foreground text-lg android:text-base">Inactive days</Text>
211 <AutoSettleDaysField
212 value={afterDays}
213 disabled={disabled}
214 onValueChange={(value) => writeToAll({ sidebarAutoSettleAfterDays: value })}
215 />
216 </View>
217 ) : null}
218 </SettingsSection>
219 {pendingWrites === 0 && mismatches.length > 0 ? (
220 <SettingsSection title="Across environments">
221 <View className="gap-3 p-4">
222 <Text className="text-base text-foreground">Auto-settle defaults differ</Text>
223 <Text className="text-sm text-foreground-muted">
224 {mismatches.map((mismatch) => mismatch.label).join(", ")}
225 </Text>
226 <Pressable
227 accessibilityRole="button"
228 disabled={disabled}
229 onPress={() => writeToAll(autoSettlePatch)}
230 className="self-start rounded-full bg-subtle px-4 py-2 active:opacity-70"
231 >
232 <Text className="text-sm font-t3-medium text-foreground">
233 Apply auto-settle defaults
234 </Text>
235 </Pressable>
236 </View>
237 </SettingsSection>
238 ) : null}
239 </View>
240 );
241}
242
243/**
244 * Device-local beta toggles, the counterpart of web's Working section (beta)
245 * in Settings → General.
246 */
247function BetaSettingsSection() {
248 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
249 const preferences = useAtomValue(mobilePreferencesAtom);
250 const workingShelfEnabled =
251 AsyncResult.isSuccess(preferences) && preferences.value.workingShelfEnabled === true;
252
253 return (
254 <View className="gap-3">
255 <SettingsSection title="Beta">
256 <SettingsSwitchRow
257 icon="bolt.circle"
258 label="Working section"
259 value={workingShelfEnabled}
260 onValueChange={(value) => savePreferences({ workingShelfEnabled: value })}
261 />
262 </SettingsSection>
263 <Text className="px-2 text-sm text-foreground-muted">
264 Fold working and monitoring threads into a Working section. They return to the top of the
265 list when they need you. While this is on, active threads are ordered by time and cannot be
266 moved.
267 </Text>
268 </View>
269 );
270}
271
272/**
273 * Device-local legacy toggles. Mobile has no client-settings sync, so this is
274 * the counterpart of web's Settings → General → Legacy features backed by
275 * mobile preferences.
276 */
277function LegacySettingsSection() {
278 const savePreferences = useAtomSet(updateMobilePreferencesAtom);
279 const preferences = useAtomValue(mobilePreferencesAtom);
280 const planModeEnabled =
281 AsyncResult.isSuccess(preferences) && preferences.value.planModeEnabled === true;
282
283 return (
284 <View className="gap-3">
285 <SettingsSection title="Legacy">
286 <SettingsSwitchRow
287 icon="hammer"
288 label="Plan Mode"
289 value={planModeEnabled}
290 onValueChange={(value) => savePreferences({ planModeEnabled: value })}
291 />
292 </SettingsSection>
293 <Text className="px-2 text-sm text-foreground-muted">
294 Opt into retired interfaces kept for compatibility. Plan Mode restores the Build/Plan
295 control; otherwise every task runs in Build mode.
296 </Text>
297 </View>
298 );
299}