Skip to content

Commit 6affdeb

Browse files
committed
fix(settings): changing the inactivity scope keeps each target's duration
1 parent 0337dba commit 6affdeb

7 files changed

Lines changed: 249 additions & 24 deletions

File tree

‎apps/mobile/src/features/settings/SettingsThreadsRouteScreen.tsx‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
import { planAutoSettleSettingsSync, type AutoSettleSettings } from "./autoSettleSettingsSync";
2828
import { useSettingsEnvironmentFilter } from "./settings-environment-filter";
2929
import {
30+
planMobileAutoSettleScopePatch,
3031
planMobileScopedSettingsClear,
3132
planMobileScopedSettingsPatch,
3233
resolveMobileSettingsTargets,
@@ -94,20 +95,14 @@ function AutoSettleSettingsRows() {
9495
return null;
9596
}
9697

97-
const writeToAll = (
98-
patch: Partial<AutoSettleSettings> & {
99-
autoResumeLimitedThreads?: boolean;
100-
snoozeLimitedThreads?: boolean;
101-
},
102-
) => {
98+
const runWrites = (writes: ReturnType<typeof planMobileScopedSettingsPatch>) => {
10399
if (
104100
writeInFlight.current ||
105101
!syncTargets.every((target) =>
106102
readEnvironmentScope(target.environment.environmentId, AuthSettingsWriteScope),
107103
)
108104
)
109105
return;
110-
const writes = planMobileScopedSettingsPatch(syncTargets, projectSelected, patch);
111106
if (writes.length === 0) return;
112107
writeInFlight.current = true;
113108
setPendingTargets(syncTargets);
@@ -122,6 +117,12 @@ function AutoSettleSettingsRows() {
122117
setPendingWrites((count) => count - 1);
123118
});
124119
};
120+
const writeToAll = (
121+
patch: Partial<AutoSettleSettings> & {
122+
autoResumeLimitedThreads?: boolean;
123+
snoozeLimitedThreads?: boolean;
124+
},
125+
) => runWrites(planMobileScopedSettingsPatch(syncTargets, projectSelected, patch));
125126

126127
const { patch: autoSettlePatch, mismatches } = planAutoSettleSettingsSync(
127128
{
@@ -264,11 +265,18 @@ function AutoSettleSettingsRows() {
264265
if (disabled) return;
265266
const value = nativeEvent.event;
266267
if (value !== "off" && value !== "all" && value !== "without-pr") return;
267-
writeToAll({
268-
sidebarAutoSettleAfterDays:
269-
value === "off" ? null : (afterDays ?? AUTO_SETTLE_DEFAULT_DAYS),
270-
sidebarAutoSettleScope: value === "off" ? "all" : value,
271-
});
268+
if (value === "off") {
269+
writeToAll({ sidebarAutoSettleAfterDays: null, sidebarAutoSettleScope: "all" });
270+
return;
271+
}
272+
runWrites(
273+
planMobileAutoSettleScopePatch(
274+
syncTargets,
275+
projectSelected,
276+
value,
277+
AUTO_SETTLE_DEFAULT_DAYS,
278+
),
279+
);
272280
}}
273281
>
274282
<SettingsControlRow

‎apps/mobile/src/features/settings/settings-scoped-server.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { describe, expect, it } from "vite-plus/test";
88

99
import type { SettingsTarget } from "./settings-environment-filter";
1010
import {
11+
planMobileAutoSettleScopePatch,
1112
planMobileScopedSettingsClear,
1213
planMobileScopedSettingsPatch,
1314
resolveMobileSettingsTargets,
@@ -116,6 +117,73 @@ describe("mobile project settings scope", () => {
116117
},
117118
);
118119

120+
it("keeps each environment's duration when changing the inactivity scope", () => {
121+
const targets = resolveMobileSettingsTargets(
122+
[
123+
environment(firstId, { ...DEFAULT_SERVER_SETTINGS, sidebarAutoSettleAfterDays: 30 }, true),
124+
environment(
125+
secondId,
126+
{ ...DEFAULT_SERVER_SETTINGS, sidebarAutoSettleAfterDays: null },
127+
true,
128+
),
129+
],
130+
null,
131+
);
132+
expect(planMobileAutoSettleScopePatch(targets, false, "without-pr", 3)).toEqual([
133+
{ environmentId: firstId, patch: { sidebarAutoSettleScope: "without-pr" } },
134+
{
135+
environmentId: secondId,
136+
patch: { sidebarAutoSettleScope: "without-pr", sidebarAutoSettleAfterDays: 3 },
137+
},
138+
]);
139+
});
140+
141+
it("keeps each project override's duration when changing the inactivity scope", () => {
142+
const targets = resolveMobileSettingsTargets(
143+
[
144+
environment(
145+
firstId,
146+
{
147+
...DEFAULT_SERVER_SETTINGS,
148+
projectSettingsOverrides: { [firstProject]: { sidebarAutoSettleAfterDays: 14 } },
149+
},
150+
true,
151+
),
152+
environment(
153+
secondId,
154+
{
155+
...DEFAULT_SERVER_SETTINGS,
156+
sidebarAutoSettleAfterDays: 7,
157+
projectSettingsOverrides: { [secondProject]: { sidebarAutoSettleAfterDays: null } },
158+
},
159+
true,
160+
),
161+
],
162+
[
163+
{ environmentId: firstId, id: firstProject },
164+
{ environmentId: secondId, id: secondProject },
165+
],
166+
);
167+
expect(planMobileAutoSettleScopePatch(targets, true, "all", 3)).toEqual([
168+
{
169+
environmentId: firstId,
170+
patch: {
171+
projectSettingsOverrides: {
172+
[firstProject]: { sidebarAutoSettleAfterDays: 14, sidebarAutoSettleScope: "all" },
173+
},
174+
},
175+
},
176+
{
177+
environmentId: secondId,
178+
patch: {
179+
projectSettingsOverrides: {
180+
[secondProject]: { sidebarAutoSettleAfterDays: 3, sidebarAutoSettleScope: "all" },
181+
},
182+
},
183+
},
184+
]);
185+
});
186+
119187
it("edits each checkout's own override without changing either environment default", () => {
120188
const firstSettings: ServerSettings = {
121189
...DEFAULT_SERVER_SETTINGS,

‎apps/mobile/src/features/settings/settings-scoped-server.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,51 @@ export function planMobileScopedSettingsPatch(
101101
}));
102102
}
103103

104+
/**
105+
* Changing the inactivity scope keeps each target's own duration. Only a
106+
* target whose inactivity rule is off gets `defaultDays` so the scope applies.
107+
*/
108+
export function planMobileAutoSettleScopePatch(
109+
targets: readonly ScopedMobileSettingsTarget[],
110+
projectSelected: boolean,
111+
value: ServerSettings["sidebarAutoSettleScope"],
112+
defaultDays: number,
113+
) {
114+
const isOff = (environmentId: EnvironmentId, projectId: ProjectId | null) =>
115+
targets.some(
116+
(target) =>
117+
target.environment.environmentId === environmentId &&
118+
target.projectId === projectId &&
119+
target.settings.sidebarAutoSettleAfterDays === null,
120+
);
121+
return planMobileScopedSettingsPatch(targets, projectSelected, {
122+
sidebarAutoSettleScope: value,
123+
}).map(({ environmentId, patch }) => {
124+
const overrides = patch.projectSettingsOverrides;
125+
if (overrides === undefined) {
126+
return {
127+
environmentId,
128+
patch: isOff(environmentId, null)
129+
? { ...patch, sidebarAutoSettleAfterDays: defaultDays }
130+
: patch,
131+
};
132+
}
133+
return {
134+
environmentId,
135+
patch: {
136+
projectSettingsOverrides: Object.fromEntries(
137+
Object.entries(overrides).map(([projectId, entry]) => [
138+
projectId,
139+
entry !== null && isOff(environmentId, projectId as ProjectId)
140+
? { ...entry, sidebarAutoSettleAfterDays: defaultDays }
141+
: entry,
142+
]),
143+
),
144+
} as ServerSettingsPatch,
145+
};
146+
});
147+
}
148+
104149
/** A mixed selection has no single value to display. */
105150
export function uniformMobileSetting<K extends keyof ServerSettings>(
106151
targets: readonly Pick<ScopedMobileSettingsTarget, "settings">[],

‎apps/web/src/components/settings/SettingsPanels.tsx‎

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import { useLocalStorage } from "../../hooks/useLocalStorage";
8282
import {
8383
useScopedSettings,
8484
useScopedSettingsMixed,
85+
useUpdateAutoSettleScope,
8586
useUpdateScopedSettings,
8687
} from "./useScopedSettings";
8788
import { useScopedModelDisabledReason } from "./useScopedModelAvailability";
@@ -2197,8 +2198,9 @@ export function GeneralSettingsPanel() {
21972198
] as const;
21982199
const settings = useScopedSettings();
21992200
const updateSettings = useUpdateScopedSettings();
2201+
const updateAutoSettleScope = useUpdateAutoSettleScope();
22002202
const navigate = useNavigate();
2201-
const { scope, environment, connectedEnvironments, targets } = useSettingsScope();
2203+
const { scope, environment, connectedEnvironments } = useSettingsScope();
22022204
// The representative environment supplies the provider list for pickers;
22032205
// a fanned-out model choice is validated against every target before it
22042206
// is written. Per-machine tuning (background activity overrides) still
@@ -2238,17 +2240,7 @@ export function GeneralSettingsPanel() {
22382240
updateSettings({ sidebarAutoSettleAfterDays: null, sidebarAutoSettleScope: "all" });
22392241
return;
22402242
}
2241-
// A scope change keeps each target's own duration. Only a selection that
2242-
// includes a target with inactivity off needs a duration written.
2243-
const turnsOn =
2244-
autoSettleAfterDays === null ||
2245-
targets.some((target) => target.settings.sidebarAutoSettleAfterDays === null);
2246-
updateSettings({
2247-
...(turnsOn
2248-
? { sidebarAutoSettleAfterDays: autoSettleAfterDays ?? AUTO_SETTLE_DEFAULT_DAYS }
2249-
: {}),
2250-
sidebarAutoSettleScope: value,
2251-
});
2243+
updateAutoSettleScope(value, AUTO_SETTLE_DEFAULT_DAYS);
22522244
}
22532245

22542246
const supportsRestartContinuation =

‎apps/web/src/components/settings/scopedSettings.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { SidebarProjectSnapshot } from "../../sidebarProjectGrouping";
1414
import {
1515
listProjectOverrides,
1616
persistScopedSettingsPatch,
17+
planAutoSettleScopePatch,
1718
planProjectOverridesClear,
1819
planScopedSettingsClear,
1920
planScopedSettingsPatch,
@@ -183,6 +184,61 @@ describe("scoped settings writes", () => {
183184
},
184185
);
185186

187+
it("keeps each environment's duration when changing the inactivity scope", () => {
188+
const custom = environment("Laptop", {
189+
settlementScope: true,
190+
settings: { sidebarAutoSettleAfterDays: 30 },
191+
});
192+
const off = environment("Server", {
193+
settlementScope: true,
194+
settings: { sidebarAutoSettleAfterDays: null },
195+
});
196+
const targets = resolveScopedSettingsTargets(all, [custom, off]);
197+
const plan = planAutoSettleScopePatch(all, [custom, off], targets, "without-pr", 3);
198+
expect(plan.serverWrites.map(({ environmentId, patch }) => ({ environmentId, patch }))).toEqual(
199+
[
200+
{ environmentId: custom.environmentId, patch: { sidebarAutoSettleScope: "without-pr" } },
201+
{
202+
environmentId: off.environmentId,
203+
patch: { sidebarAutoSettleScope: "without-pr", sidebarAutoSettleAfterDays: 3 },
204+
},
205+
],
206+
);
207+
});
208+
209+
it("keeps each project override's duration when changing the inactivity scope", () => {
210+
const custom = environment("Laptop", {
211+
settlementScope: true,
212+
settings: {
213+
projectSettingsOverrides: { [laptopProjectId]: { sidebarAutoSettleAfterDays: 14 } },
214+
},
215+
});
216+
const off = environment("Server", {
217+
settlementScope: true,
218+
settings: {
219+
sidebarAutoSettleAfterDays: 7,
220+
projectSettingsOverrides: { [projectId]: { sidebarAutoSettleAfterDays: null } },
221+
},
222+
});
223+
const targets = resolveScopedSettingsTargets(project, [custom, off]);
224+
const plan = planAutoSettleScopePatch(project, [custom, off], targets, "all", 3);
225+
expect(
226+
Object.fromEntries(
227+
plan.serverWrites.map(({ environmentId, patch }) => [
228+
environmentId,
229+
patch.projectSettingsOverrides,
230+
]),
231+
),
232+
).toEqual({
233+
[custom.environmentId]: {
234+
[laptopProjectId]: { sidebarAutoSettleAfterDays: 14, sidebarAutoSettleScope: "all" },
235+
},
236+
[off.environmentId]: {
237+
[projectId]: { sidebarAutoSettleAfterDays: 3, sidebarAutoSettleScope: "all" },
238+
},
239+
});
240+
});
241+
186242
it("edits the effective machine policy without changing other machines' rules", () => {
187243
const custom = environment("Laptop", {
188244
settings: {

‎apps/web/src/components/settings/scopedSettings.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,50 @@ export function planScopedSettingsPatch(
332332
return { clientPatch, hasClientWrite, serverWrites, unavailableReason };
333333
}
334334

335+
/**
336+
* Changing the inactivity scope keeps each target's own duration. Only a
337+
* target whose inactivity rule is off gets `defaultDays` so the scope applies.
338+
*/
339+
export function planAutoSettleScopePatch(
340+
scope: ResolvedSettingsScope,
341+
environments: readonly ScopedSettingsEnvironment[],
342+
targets: readonly Pick<ScopedSettingsTarget, "environmentId" | "projectId" | "settings">[],
343+
value: ServerSettings["sidebarAutoSettleScope"],
344+
defaultDays: number,
345+
) {
346+
const plan = planScopedSettingsPatch(scope, environments, { sidebarAutoSettleScope: value });
347+
const isOff = (environmentId: EnvironmentId, projectId: ProjectId | null) =>
348+
targets.some(
349+
(target) =>
350+
target.environmentId === environmentId &&
351+
target.projectId === projectId &&
352+
target.settings.sidebarAutoSettleAfterDays === null,
353+
);
354+
const serverWrites = plan.serverWrites.map((write) => {
355+
const overrides = write.patch.projectSettingsOverrides;
356+
if (overrides === undefined) {
357+
return isOff(write.environmentId, null)
358+
? { ...write, patch: { ...write.patch, sidebarAutoSettleAfterDays: defaultDays } }
359+
: write;
360+
}
361+
return {
362+
...write,
363+
patch: {
364+
...write.patch,
365+
projectSettingsOverrides: Object.fromEntries(
366+
Object.entries(overrides).map(([projectId, entry]) => [
367+
projectId,
368+
entry !== null && isOff(write.environmentId, projectId as ProjectId)
369+
? { ...entry, sidebarAutoSettleAfterDays: defaultDays }
370+
: entry,
371+
]),
372+
),
373+
},
374+
};
375+
});
376+
return { ...plan, serverWrites };
377+
}
378+
335379
/** Remove the keys' project overrides so each member inherits its environment value again. */
336380
export function planScopedSettingsClear(
337381
scope: ResolvedSettingsScope,

‎apps/web/src/components/settings/useScopedSettings.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { toastManager } from "../ui/toast";
2626
import { useOptionalSettingsScope, useSettingsScope } from "./SettingsScopeContext";
2727
import {
2828
persistScopedSettingsPatch,
29+
planAutoSettleScopePatch,
2930
planProjectOverridesClear,
3031
planScopedSettingsClear,
3132
planScopedSettingsPatch,
@@ -120,6 +121,17 @@ export function useUpdateScopedSettings() {
120121
);
121122
}
122123

124+
/** Set the inactivity scope on every target without overwriting their durations. */
125+
export function useUpdateAutoSettleScope() {
126+
const { scope, environments, targets } = useSettingsScope();
127+
const run = useRunScopedPlan();
128+
return useCallback(
129+
(value: ServerSettings["sidebarAutoSettleScope"], defaultDays: number) =>
130+
run(planAutoSettleScopePatch(scope, environments, targets, value, defaultDays)),
131+
[environments, run, scope, targets],
132+
);
133+
}
134+
123135
/**
124136
* Drop the project overrides for `keys` so the selected checkouts inherit
125137
* again. Rows also render outside the settings layout (provider cards,

0 commit comments

Comments
 (0)