Skip to content

Commit 87649a2

Browse files
committed
fix(web): commit parsed retention days and reset draft state
Enter was saving Number() of the locale-formatted field, so a grouped value such as 1.000 could persist as 1 day. Commit the number NumberField already parsed. A failed save clears holding mode, and the pointer flag clears when the pointer leaves the control.
1 parent 0951828 commit 87649a2

3 files changed

Lines changed: 56 additions & 10 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,38 @@ it("saves the visible default when Enter commits an untouched retention draft",
251251
});
252252
});
253253

254+
it("commits the parsed retention age when Enter is pressed on grouped text", async () => {
255+
const label = "Delete inactive worktrees";
256+
await render();
257+
await act(async () => retentionSwitch(label).click());
258+
const input = daysInput(label)!;
259+
await act(async () => setInputValue(input, "1000"));
260+
input.value = "1.000";
261+
await act(async () =>
262+
input.dispatchEvent(
263+
new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }),
264+
),
265+
);
266+
expect(state.update).toHaveBeenCalledExactlyOnceWith({
267+
storageCleanup: { worktreeAfterDays: 1000 },
268+
});
269+
});
270+
271+
it("returns a retention rule to off when saving its age fails", async () => {
272+
const label = "Delete inactive worktrees";
273+
state.update.mockResolvedValue(false);
274+
await render();
275+
await act(async () => retentionSwitch(label).click());
276+
await act(async () => {
277+
setInputValue(daysInput(label)!, "30");
278+
daysInput(label)!.blur();
279+
});
280+
expect(state.update).toHaveBeenCalledExactlyOnceWith({
281+
storageCleanup: { worktreeAfterDays: 30 },
282+
});
283+
expect(daysInput(label)).toBeNull();
284+
});
285+
254286
it("keeps a retention rule off when its draft is left or cancelled", async () => {
255287
const label = "Delete inactive worktrees";
256288
await render();

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function RetentionControl({
112112
}: {
113113
label: string;
114114
value: number | null;
115-
onChange: (value: number | null) => void;
115+
onChange: (value: number | null) => Promise<boolean> | void;
116116
minimum?: number;
117117
}) {
118118
const initialDays = Math.max(minimum, 8);
@@ -125,6 +125,8 @@ function RetentionControl({
125125
const rootRef = useRef<HTMLDivElement>(null);
126126
const pointerInsideRef = useRef(false);
127127
const publishedRef = useRef<number | null>(null);
128+
const valueRef = useRef(value);
129+
valueRef.current = value; // oxlint-disable-line react/refs -- The save result can resolve before effects, and holding must see the latest prop.
128130
if (savedValue !== value) {
129131
setSavedValue(value);
130132
setDraft(value);
@@ -158,7 +160,17 @@ function RetentionControl({
158160
setMode("holding");
159161
publishedRef.current = days;
160162
}
161-
onChange(days);
163+
const save = onChange(days);
164+
if (value !== null || !(save instanceof Promise)) return;
165+
void save.then((succeeded) => {
166+
if (
167+
!succeeded &&
168+
valueRef.current === null &&
169+
modeRef.current === "holding" &&
170+
publishedRef.current === days
171+
)
172+
discardDraft();
173+
});
162174
}
163175

164176
return (
@@ -168,6 +180,9 @@ function RetentionControl({
168180
onPointerDown={() => {
169181
pointerInsideRef.current = true;
170182
}}
183+
onPointerLeave={() => {
184+
pointerInsideRef.current = false;
185+
}}
171186
onPointerUp={() => {
172187
pointerInsideRef.current = false;
173188
}}
@@ -213,10 +228,7 @@ function RetentionControl({
213228
}
214229
if (event.key !== "Enter") return;
215230
event.preventDefault();
216-
const raw = event.currentTarget.value.trim();
217-
if (raw === "") return;
218-
const parsed = Number(raw);
219-
if (Number.isFinite(parsed)) commitDays(parsed);
231+
if (draft !== null) commitDays(draft);
220232
}}
221233
/>
222234
<span aria-hidden="true" className="self-center pr-2 text-xs">

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ function useRunScopedPlan() {
6161
title: "Setting not saved",
6262
description: plan.unavailableReason,
6363
});
64-
return;
64+
return Promise.resolve(false);
6565
}
66-
void persistScopedSettingsPatch(
66+
return persistScopedSettingsPatch(
6767
plan,
6868
async (request) => {
6969
const missing = requiredScopesForServerSettingsPatch(request.input.patch).find(
@@ -82,7 +82,7 @@ function useRunScopedPlan() {
8282
},
8383
persistClientSettingsPatch,
8484
).then(({ failedEnvironments, savedEnvironments, savedEnvironmentCount }) => {
85-
if (failedEnvironments.length === 0) return;
85+
if (failedEnvironments.length === 0) return true;
8686
toastManager.add({
8787
type: "error",
8888
title:
@@ -96,14 +96,16 @@ function useRunScopedPlan() {
9696
: []),
9797
].join("\n"),
9898
});
99+
return false;
99100
});
100101
};
101102
}
102103

103104
export function useUpdateScopedSettings() {
104105
const { scope, environments } = useSettingsScope();
105106
const run = useRunScopedPlan();
106-
return (patch: ScopedSettingsPatch) => run(planScopedSettingsPatch(scope, environments, patch));
107+
return (patch: ScopedSettingsPatch): Promise<boolean> | void =>
108+
run(planScopedSettingsPatch(scope, environments, patch));
107109
}
108110

109111
export function useClearScopedSettings() {

0 commit comments

Comments
 (0)