Skip to content

Commit f4f0f1a

Browse files
fix(web): a saved retention rule switched off hides its age at once
The age field stayed mounted until the server echoed null, so a blur in that window could save an edited age after null and leave the rule on. Switching a saved rule off now hides the field immediately and brings it back only if the save fails. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0534db2 commit f4f0f1a

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,24 @@ it("turns a saved retention rule off immediately", async () => {
269269
});
270270
});
271271

272+
it("switching an edited saved rule off never saves its age afterwards", async () => {
273+
setup();
274+
state.settings = {
275+
...state.settings,
276+
storageCleanup: { ...state.settings.storageCleanup, browserArtifactsAfterDays: 30 },
277+
};
278+
state.update.mockReturnValue(new Promise(() => {}));
279+
await using screen = await render(panel());
280+
await userEvent.click(daysInput(screen));
281+
await userEvent.keyboard("{Control>}a{/Control}40");
282+
await userEvent.click(ruleSwitch(screen));
283+
await expect.element(ruleSwitch(screen)).not.toBeChecked();
284+
await userEvent.click(screen.getByRole("heading", { name: rule }));
285+
expect(state.update).toHaveBeenCalledExactlyOnceWith({
286+
storageCleanup: { browserArtifactsAfterDays: null },
287+
});
288+
});
289+
272290
it("returns a newly enabled retention rule to off when its first save fails", async () => {
273291
setup();
274292
state.update.mockResolvedValue(false);

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,17 @@ function RetentionControl({
120120
// switch, so switching off never saves it first, or one stepped toward a rule's first age,
121121
// so raising it from 8 never saves a shorter age on the way.
122122
const [held, setHeld] = useState(false);
123+
// Hides a saved rule's age once it is switched off, so no blur can save it after null.
124+
const [switchedOff, setSwitchedOff] = useState(false);
123125
if (savedValue !== value) {
124126
setSavedValue(value);
125127
setDraft(value);
126128
setEnabling(null);
127129
setHeld(false);
130+
setSwitchedOff(false);
128131
}
129132
const pending = typeof enabling === "object" ? enabling : null;
133+
const on = (value !== null && !switchedOff) || enabling !== null;
130134
const row = useRef<HTMLDivElement>(null);
131135

132136
const commit = async (next: number) => {
@@ -145,6 +149,15 @@ function RetentionControl({
145149
}
146150
};
147151

152+
const switchOff = async () => {
153+
setDraft(value);
154+
setHeld(false);
155+
setEnabling(null);
156+
if (value === null && pending === null) return;
157+
setSwitchedOff(true);
158+
if (!(await onChange(null))) setSwitchedOff(false);
159+
};
160+
148161
return (
149162
<div
150163
ref={row}
@@ -155,7 +168,7 @@ function RetentionControl({
155168
else setEnabling((current) => (typeof current === "string" ? null : current));
156169
}}
157170
>
158-
{value !== null || enabling !== null ? (
171+
{on ? (
159172
<NumberField
160173
value={draft}
161174
min={minimum}
@@ -223,19 +236,14 @@ function RetentionControl({
223236
)}
224237
<Switch
225238
aria-label={label}
226-
checked={value !== null || enabling !== null}
239+
checked={on}
227240
// Keeps focus in the age field, so switching off never commits an edited age first.
228241
onMouseDown={(event) => event.preventDefault()}
229242
onCheckedChange={(enabled) => {
230243
if (enabled) {
231244
setDraft(8);
232245
setEnabling("draft");
233-
return;
234-
}
235-
if (value !== null || pending !== null) void onChange(null);
236-
setDraft(value);
237-
setHeld(false);
238-
setEnabling(null);
246+
} else void switchOff();
239247
}}
240248
/>
241249
</div>

0 commit comments

Comments
 (0)