Skip to content

Commit 252cb82

Browse files
fix(web): tabbing to a retention switch holds an edited age
Keyboard users who typed an age and tabbed to the row's switch committed that age on blur, so switching the rule off still saved it first. The control now holds an age edited when focus moves to its own switch and saves it only when focus leaves the row. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2d67e12 commit 252cb82

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ const daysInput = (screen: Screen) => screen.getByLabelText(`${rule} in days`);
134134

135135
it("saves a newly enabled retention rule only once its age is committed", async () => {
136136
setup();
137+
state.update.mockResolvedValue(true);
137138
await using screen = await render(panel());
138139
await userEvent.click(ruleSwitch(screen));
139140
expect(state.update).not.toHaveBeenCalled();
@@ -145,6 +146,8 @@ it("saves a newly enabled retention rule only once its age is committed", async
145146
expect(state.update).toHaveBeenCalledExactlyOnceWith({
146147
storageCleanup: { browserArtifactsAfterDays: 30 },
147148
});
149+
await expect.element(ruleSwitch(screen)).toBeChecked();
150+
await expect.element(daysInput(screen)).toHaveValue("30");
148151
});
149152

150153
it("discards a retention draft on Escape or when switched back off", async () => {
@@ -175,12 +178,15 @@ it("discards an untouched retention draft when focus leaves it", async () => {
175178
expect(state.update).not.toHaveBeenCalled();
176179
});
177180

178-
it("saves an edited draft when focus leaves it", async () => {
181+
it("saves an edited draft when focus leaves its row", async () => {
179182
setup();
180183
await using screen = await render(panel());
181184
await userEvent.click(ruleSwitch(screen));
182185
await userEvent.fill(daysInput(screen), "30");
183186
await userEvent.tab();
187+
await expect.element(ruleSwitch(screen)).toHaveFocus();
188+
expect(state.update).not.toHaveBeenCalled();
189+
await userEvent.tab();
184190
expect(state.update).toHaveBeenCalledExactlyOnceWith({
185191
storageCleanup: { browserArtifactsAfterDays: 30 },
186192
});
@@ -196,11 +202,24 @@ it("saves a re-entered age that matches the shown value", async () => {
196202
await userEvent.click(daysInput(screen));
197203
await userEvent.keyboard("{Control>}a{/Control}30");
198204
await userEvent.tab();
205+
await userEvent.tab();
199206
expect(state.update).toHaveBeenCalledExactlyOnceWith({
200207
storageCleanup: { browserArtifactsAfterDays: 30 },
201208
});
202209
});
203210

211+
it("switching an edited draft off from the keyboard saves nothing", async () => {
212+
setup();
213+
await using screen = await render(panel());
214+
await userEvent.click(ruleSwitch(screen));
215+
await userEvent.fill(daysInput(screen), "30");
216+
await userEvent.tab();
217+
await userEvent.keyboard(" ");
218+
await expect.element(ruleSwitch(screen)).not.toBeChecked();
219+
await userEvent.tab();
220+
expect(state.update).not.toHaveBeenCalled();
221+
});
222+
204223
it("turns a saved retention rule off immediately", async () => {
205224
setup();
206225
state.settings = {
@@ -264,7 +283,9 @@ it("switching off during a pending first save does not save an edited age", asyn
264283
await userEvent.fill(daysInput(screen), "30");
265284
await userEvent.keyboard("{Enter}");
266285
await userEvent.fill(daysInput(screen), "1");
267-
await userEvent.click(ruleSwitch(screen));
286+
await userEvent.tab();
287+
await userEvent.keyboard(" ");
288+
await userEvent.tab();
268289
expect(state.update.mock.calls).toEqual([
269290
[{ storageCleanup: { browserArtifactsAfterDays: 30 } }],
270291
[{ storageCleanup: { browserArtifactsAfterDays: null } }],

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

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,21 @@ function RetentionControl({
116116
// Saving an age can start a cleanup sweep, so turning a rule on only opens a draft.
117117
// While its first save is in flight this holds that submission.
118118
const [enabling, setEnabling] = useState<"draft" | "edited" | { days: number } | null>(null);
119+
// An edited age is held while focus is on the switch, so switching off never saves it first.
120+
const [held, setHeld] = useState(false);
119121
if (savedValue !== value) {
120122
setSavedValue(value);
121123
setDraft(value);
122124
setEnabling(null);
125+
setHeld(false);
123126
}
124127
const pending = typeof enabling === "object" ? enabling : null;
128+
const row = useRef<HTMLDivElement>(null);
125129

126130
const commit = async (next: number) => {
127131
const days = Math.min(3650, Math.max(minimum, Math.round(next)));
128132
setDraft(days);
133+
setHeld(false);
129134
if (days === pending?.days) return;
130135
if (value !== null) {
131136
void onChange(days);
@@ -140,10 +145,12 @@ function RetentionControl({
140145

141146
return (
142147
<div
148+
ref={row}
143149
className="flex items-center gap-3"
144150
onBlur={(event) => {
145151
if (event.currentTarget.contains(event.relatedTarget)) return;
146-
setEnabling((current) => (typeof current === "string" ? null : current));
152+
if (held && draft !== null) void commit(draft);
153+
else setEnabling((current) => (typeof current === "string" ? null : current));
147154
}}
148155
>
149156
{value !== null || enabling !== null ? (
@@ -156,11 +163,20 @@ function RetentionControl({
156163
className="w-auto"
157164
onValueChange={(next) => {
158165
setDraft(next);
166+
setHeld(false);
159167
setEnabling((current) => (current === "draft" ? "edited" : current));
160168
}}
161-
onValueCommitted={(next) => {
169+
onValueCommitted={(next, { reason, event }) => {
162170
if (next === null) setDraft(value);
163-
else void commit(next);
171+
else if (
172+
reason === "input-blur" &&
173+
event instanceof FocusEvent &&
174+
event.relatedTarget instanceof Node &&
175+
row.current?.contains(event.relatedTarget)
176+
) {
177+
setDraft(next);
178+
setHeld(true);
179+
} else void commit(next);
164180
}}
165181
>
166182
<NumberFieldGroup>
@@ -180,11 +196,13 @@ function RetentionControl({
180196
draft !== null && input.value === new Intl.NumberFormat().format(draft);
181197
if (enabling === "draft" && shown) void commit(draft);
182198
input.blur();
183-
} else if (event.key === "Escape" && typeof enabling === "string") {
199+
} else if (event.key === "Escape" && (held || typeof enabling === "string")) {
184200
// An unhandled Escape closes the settings page.
185201
event.preventDefault();
186202
event.stopPropagation();
187-
setEnabling(null);
203+
setDraft(pending?.days ?? value);
204+
setHeld(false);
205+
setEnabling((current) => (typeof current === "string" ? null : current));
188206
}
189207
}}
190208
/>
@@ -209,6 +227,8 @@ function RetentionControl({
209227
return;
210228
}
211229
if (value !== null || pending !== null) void onChange(null);
230+
setDraft(value);
231+
setHeld(false);
212232
setEnabling(null);
213233
}}
214234
/>

0 commit comments

Comments
 (0)