Skip to content

Commit 8fb4132

Browse files
committed
fix(web): reset retention drafts when the settings scope changes
RetentionControl only replaced its draft when the saved age changed, so a scope with the same age kept the previous draft and could save it. Key each control by the selected targets. A late null from an off save no longer clears a draft the user already reopened.
1 parent d6f9c58 commit 8fb4132

2 files changed

Lines changed: 43 additions & 45 deletions

File tree

‎apps/web/src/components/settings/StorageSettings.browser.test.tsx‎

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const state = vi.hoisted(() => ({
88
settings: {} as typeof DEFAULT_SERVER_SETTINGS,
99
run: vi.fn(),
1010
update: vi.fn(),
11+
targets: [] as never[],
1112
report: null as StorageCleanupReport | null,
1213
}));
1314

@@ -53,7 +54,7 @@ vi.mock(import("./SettingsScopeContext"), async (original) => ({
5354
scope: { kind: "environment" },
5455
environments: [environment],
5556
connectedEnvironments: [environment],
56-
targets: [],
57+
targets: state.targets,
5758
target: null,
5859
};
5960
}) as never,
@@ -103,6 +104,7 @@ function setup() {
103104
projectSettingsOverrides: {},
104105
};
105106
state.report = null;
107+
state.targets = [];
106108
state.run.mockReset();
107109
state.update.mockReset();
108110
}
@@ -143,6 +145,12 @@ async function openDraft(label: string) {
143145
await act(async () => ruleSwitch(screen.container, label).click());
144146
return screen;
145147
}
148+
function expectSaved(rule: Record<string, number | null>) {
149+
expect(state.update).toHaveBeenCalledExactlyOnceWith({ storageCleanup: rule });
150+
}
151+
function retentionTarget(environmentId: string) {
152+
return [{ environmentId, projectId: null, sources: {}, settings: state.settings }] as never[];
153+
}
146154

147155
it.each(RETENTION_RULES)("turning %s on does not save 8 days", async (label) => {
148156
setup();
@@ -160,33 +168,25 @@ it("saves a drafted retention age only when that age is committed", async () =>
160168
setInputValue(input, "30");
161169
input.blur();
162170
});
163-
expect(state.update).toHaveBeenCalledExactlyOnceWith({
164-
storageCleanup: { worktreeAfterDays: 30 },
165-
});
171+
expectSaved({ worktreeAfterDays: 30 });
166172
expect(daysInput(screen.container, inactive)?.value).toBe("30");
167173
});
168174

169175
it("saves the visible default when Enter commits an untouched retention draft", async () => {
170176
setup();
171177
await using screen = await openDraft(inactive);
172178
await act(async () => pressKey(daysInput(screen.container, inactive)!, "Enter"));
173-
expect(state.update).toHaveBeenCalledExactlyOnceWith({
174-
storageCleanup: { worktreeAfterDays: 8 },
175-
});
179+
expectSaved({ worktreeAfterDays: 8 });
176180
});
177-
178181
it("commits the parsed retention age when Enter is pressed on grouped text", async () => {
179182
setup();
180183
await using screen = await openDraft(inactive);
181184
const input = daysInput(screen.container, inactive)!;
182185
await act(async () => setInputValue(input, "1000"));
183186
input.value = "1.000";
184187
await act(async () => pressKey(input, "Enter"));
185-
expect(state.update).toHaveBeenCalledExactlyOnceWith({
186-
storageCleanup: { worktreeAfterDays: 1000 },
187-
});
188+
expectSaved({ worktreeAfterDays: 1000 });
188189
});
189-
190190
it("returns a retention rule to off when saving its age fails", async () => {
191191
setup();
192192
state.update.mockResolvedValue(false);
@@ -196,12 +196,9 @@ it("returns a retention rule to off when saving its age fails", async () => {
196196
setInputValue(input, "30");
197197
input.blur();
198198
});
199-
expect(state.update).toHaveBeenCalledExactlyOnceWith({
200-
storageCleanup: { worktreeAfterDays: 30 },
201-
});
199+
expectSaved({ worktreeAfterDays: 30 });
202200
expect(daysInput(screen.container, inactive)).toBeNull();
203201
});
204-
205202
it("keeps a retention rule off when its draft is left or cancelled", async () => {
206203
setup();
207204
await using screen = await openDraft(inactive);
@@ -223,11 +220,8 @@ it("saves a retention step from the drafted age", async () => {
223220
await using screen = await openDraft(label);
224221
const step = screen.container.querySelector<HTMLElement>(`[aria-label="Increase ${label}"]`)!;
225222
await act(async () => step.click());
226-
expect(state.update).toHaveBeenCalledExactlyOnceWith({
227-
storageCleanup: { logsAfterDays: 9 },
228-
});
223+
expectSaved({ logsAfterDays: 9 });
229224
});
230-
231225
it("does not save a drafted age when that rule's switch is clicked", async () => {
232226
setup();
233227
await using screen = await openDraft(inactive);
@@ -245,25 +239,27 @@ it("does not save a drafted age when that rule's switch is clicked", async () =>
245239
it("turns an enabled retention rule off immediately and edits it without saving 8", async () => {
246240
const label = "Delete old browser artifacts";
247241
setup();
248-
state.settings = {
249-
...state.settings,
250-
storageCleanup: { ...state.settings.storageCleanup, browserArtifactsAfterDays: 30 },
251-
};
242+
Object.assign(state.settings.storageCleanup, { browserArtifactsAfterDays: 30 });
252243
await using screen = await render(panel());
253244
const input = daysInput(screen.container, label)!;
254245
await act(async () => {
255246
input.focus();
256247
setInputValue(input, "14");
257248
input.blur();
258249
});
259-
expect(state.update).toHaveBeenCalledExactlyOnceWith({
260-
storageCleanup: { browserArtifactsAfterDays: 14 },
261-
});
250+
expectSaved({ browserArtifactsAfterDays: 14 });
262251
state.update.mockClear();
263252
await act(async () => ruleSwitch(screen.container, label).click());
264-
expect(state.update).toHaveBeenCalledExactlyOnceWith({
265-
storageCleanup: { browserArtifactsAfterDays: null },
266-
});
253+
expectSaved({ browserArtifactsAfterDays: null });
254+
});
255+
it("drops a retention draft when the settings scope changes", async () => {
256+
setup();
257+
state.targets = retentionTarget("a");
258+
await using screen = await openDraft(inactive);
259+
state.targets = retentionTarget("b");
260+
await act(async () => screen.rerender(panel()));
261+
expect(daysInput(screen.container, inactive)).toBeNull();
262+
expect(state.update).not.toHaveBeenCalled();
267263
});
268264

269265
it("keeps cleanup disabled when settled retention and all other rules are off", async () => {

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,11 @@ function RetentionControl({
136136
const valueRef = useRef(value);
137137
if (savedValue !== value) {
138138
setSavedValue(value);
139-
setDraft(value);
140-
// A queued age can arrive after the user already chose Off. Keep that choice until null lands.
141-
if (value === null || mode !== "off") setMode("synced");
139+
// A draft outlives a late null from its off save. Off still waits for that null.
140+
if (mode !== "draft") {
141+
setDraft(value);
142+
if (value === null || mode !== "off") setMode("synced");
143+
}
142144
}
143145

144146
useLayoutEffect(() => {
@@ -167,9 +169,8 @@ function RetentionControl({
167169
if (value === null && modeRef.current === "holding" && publishedRef.current === days) return;
168170
setDraft(days);
169171
if (value === null) {
170-
modeRef.current = "holding";
171-
setMode("holding");
172172
publishedRef.current = days;
173+
setMode((modeRef.current = "holding"));
173174
}
174175
const save = onChange(days);
175176
if (value !== null || !(save instanceof Promise)) return;
@@ -193,10 +194,9 @@ function RetentionControl({
193194
onPointerUp={() => (pointerInsideRef.current = false)}
194195
onPointerCancel={() => (pointerInsideRef.current = false)}
195196
onBlur={(event) => {
196-
if (modeRef.current !== "draft") return;
197197
const next = event.relatedTarget;
198+
if (modeRef.current !== "draft" || pointerInsideRef.current) return;
198199
if (next instanceof Node && event.currentTarget.contains(next)) return;
199-
if (pointerInsideRef.current) return;
200200
discardDraft();
201201
}}
202202
>
@@ -223,17 +223,15 @@ function RetentionControl({
223223
size={new Intl.NumberFormat().format(draft ?? value ?? initialDays).length}
224224
className="field-sizing-content w-auto min-w-[1ch] grow-0 text-right"
225225
onKeyDown={(event) => {
226-
if (event.nativeEvent.isComposing) return;
227-
if (modeRef.current !== "draft") return;
226+
if (event.nativeEvent.isComposing || modeRef.current !== "draft") return;
228227
if (event.key === "Escape") {
229228
event.preventDefault();
230229
event.stopPropagation();
231230
discardDraft();
232-
return;
231+
} else if (event.key === "Enter") {
232+
event.preventDefault();
233+
if (draft !== null) commitDays(draft);
233234
}
234-
if (event.key !== "Enter") return;
235-
event.preventDefault();
236-
if (draft !== null) commitDays(draft);
237235
}}
238236
/>
239237
<span aria-hidden="true" className="self-center pr-2 text-xs">
@@ -253,8 +251,7 @@ function RetentionControl({
253251
offGenRef.current += 1;
254252
publishedRef.current = null;
255253
setDraft(initialDays);
256-
modeRef.current = "draft";
257-
setMode("draft");
254+
setMode((modeRef.current = "draft"));
258255
return;
259256
}
260257
const publishOff = value !== null || modeRef.current === "holding";
@@ -556,6 +553,7 @@ export function StorageSettingsPanel() {
556553
: undefined;
557554
const update = (patch: Partial<StorageCleanupSettings>) =>
558555
updateSettings({ storageCleanup: patch });
556+
const scopeKey = targets.map((entry) => `${entry.environmentId}:${entry.projectId}`).join(",");
559557
const updateWorktree = (patch: Partial<WorktreeCleanupRules>) =>
560558
isProjectScope
561559
? updateSettings({ worktreeCleanup: { mode: "custom", rules: patch } })
@@ -673,6 +671,7 @@ export function StorageSettingsPanel() {
673671
serverScoped={!isProjectScope}
674672
control={
675673
<RetentionControl
674+
key={scopeKey}
676675
label="Delete inactive worktrees"
677676
value={settings.worktreeAfterDays}
678677
onChange={(worktreeAfterDays) => updateWorktree({ worktreeAfterDays })}
@@ -699,6 +698,7 @@ export function StorageSettingsPanel() {
699698
serverScoped={!isProjectScope}
700699
control={
701700
<RetentionControl
701+
key={scopeKey}
702702
label="Delete settled worktrees"
703703
value={settings.worktreeSettledAfterDays}
704704
minimum={0}
@@ -791,6 +791,7 @@ export function StorageSettingsPanel() {
791791
serverScoped
792792
control={
793793
<RetentionControl
794+
key={scopeKey}
794795
label="Delete old browser artifacts"
795796
value={settings.browserArtifactsAfterDays}
796797
onChange={(browserArtifactsAfterDays) => update({ browserArtifactsAfterDays })}
@@ -804,6 +805,7 @@ export function StorageSettingsPanel() {
804805
serverScoped
805806
control={
806807
<RetentionControl
808+
key={scopeKey}
807809
label="Delete old rotated logs"
808810
value={settings.logsAfterDays}
809811
onChange={(logsAfterDays) => update({ logsAfterDays })}

0 commit comments

Comments
 (0)