Skip to content

Commit ab75817

Browse files
committed
fix(web): restore interval drafts after rejected settings saves
1 parent c62d2b2 commit ab75817

5 files changed

Lines changed: 79 additions & 29 deletions

File tree

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,14 @@ function BackgroundActivityAdvancedDialog({
183183
value={automaticGitFetchIntervalSeconds}
184184
min={0}
185185
step={5}
186-
onCommit={(seconds) =>
186+
onCommit={(seconds, options) =>
187187
updateSettings(
188188
backgroundActivityOverrideSettings(
189189
settings.backgroundActivity,
190190
resolvedBackgroundActivity,
191191
{ automaticGitFetchInterval: Duration.seconds(seconds) },
192192
),
193+
options,
193194
)
194195
}
195196
/>
@@ -211,13 +212,14 @@ function BackgroundActivityAdvancedDialog({
211212
value={providerHealthRefreshIntervalSeconds}
212213
min={0}
213214
step={PROVIDER_HEALTH_INTERVAL_STEP_SECONDS}
214-
onCommit={(seconds) =>
215+
onCommit={(seconds, options) =>
215216
updateSettings(
216217
backgroundActivityOverrideSettings(
217218
settings.backgroundActivity,
218219
resolvedBackgroundActivity,
219220
{ providerHealthRefreshInterval: Duration.seconds(seconds) },
220221
),
222+
options,
221223
)
222224
}
223225
/>
@@ -238,13 +240,14 @@ function BackgroundActivityAdvancedDialog({
238240
value={hostPowerMonitorActiveIntervalSeconds}
239241
min={5}
240242
step={5}
241-
onCommit={(seconds) =>
243+
onCommit={(seconds, options) =>
242244
updateSettings(
243245
backgroundActivityOverrideSettings(
244246
settings.backgroundActivity,
245247
resolvedBackgroundActivity,
246248
{ hostPowerMonitorActiveInterval: Duration.seconds(seconds) },
247249
),
250+
options,
248251
)
249252
}
250253
/>
@@ -265,13 +268,14 @@ function BackgroundActivityAdvancedDialog({
265268
value={hostPowerMonitorIdleIntervalSeconds}
266269
min={5}
267270
step={30}
268-
onCommit={(seconds) =>
271+
onCommit={(seconds, options) =>
269272
updateSettings(
270273
backgroundActivityOverrideSettings(
271274
settings.backgroundActivity,
272275
resolvedBackgroundActivity,
273276
{ hostPowerMonitorIdleInterval: Duration.seconds(seconds) },
274277
),
278+
options,
275279
)
276280
}
277281
/>

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

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,50 @@ export function IntervalSecondsField({
1919
value: number;
2020
min: number;
2121
step: number;
22-
onCommit: (seconds: number) => void;
22+
onCommit: (seconds: number, options: { onFailed: () => void }) => void;
2323
}) {
24-
const [draft, setDraft] = useState<number | null>(value);
25-
const [savedValue, setSavedValue] = useState(value);
26-
const [committedValue, setCommittedValue] = useState(value);
27-
if (savedValue !== value) {
28-
setSavedValue(value);
29-
setCommittedValue(value);
30-
setDraft(value);
24+
const [field, setField] = useState({
25+
saved: value,
26+
draft: value as number | null,
27+
committed: value,
28+
revision: 0,
29+
});
30+
if (field.saved !== value) {
31+
setField({ ...field, saved: value, draft: value, committed: value });
3132
}
3233

3334
return (
3435
<NumberField
35-
value={draft}
36+
value={field.draft}
3637
min={min}
3738
step={step}
3839
size="sm"
3940
className="w-32"
40-
onValueChange={setDraft}
41+
onValueChange={(draft) => setField((current) => ({ ...current, draft }))}
4142
onValueCommitted={(next) => {
4243
if (next === null) {
43-
setDraft(committedValue);
44+
setField((current) => ({ ...current, draft: current.saved }));
4445
return;
4546
}
4647
const seconds = normalizeIntervalSeconds(next, min);
47-
setDraft(seconds);
48-
if (seconds !== committedValue) {
49-
setCommittedValue(seconds);
50-
onCommit(seconds);
48+
if (seconds === field.committed) {
49+
setField((current) => ({ ...current, draft: seconds }));
50+
return;
5151
}
52+
const revision = field.revision + 1;
53+
setField((current) => ({ ...current, draft: seconds, committed: seconds, revision }));
54+
onCommit(seconds, {
55+
onFailed: () =>
56+
setField((current) =>
57+
current.revision !== revision
58+
? current
59+
: {
60+
...current,
61+
committed: current.saved,
62+
draft: current.draft === seconds ? current.saved : current.draft,
63+
},
64+
),
65+
});
5266
}}
5367
>
5468
<NumberFieldGroup>

‎apps/web/src/components/settings/ProviderSettingsPanel.environment.browser.test.tsx‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ describe("EnvironmentProviderSettings routing", () => {
360360
});
361361

362362
describe("EnvironmentProviderSettings health check interval", () => {
363-
it("saves a typed interval once, when editing ends", async () => {
363+
it("commits interval drafts and allows retrying rejected edits", async () => {
364364
await setup();
365365
await using _screen = await render(panel());
366366
const input = page.getByRole("textbox", { name: "Provider health check interval in seconds" });
@@ -384,6 +384,21 @@ describe("EnvironmentProviderSettings health check interval", () => {
384384
Duration.toSeconds(patch.backgroundActivity.overrides.providerHealthRefreshInterval),
385385
),
386386
).toEqual([120]);
387+
388+
await act(() => state.updateSettings.mock.calls.at(-1)?.[1]?.onFailed?.());
389+
await expect.element(input).toHaveValue("300");
390+
await userEvent.type(input, "{Backspace}{Backspace}{Backspace}120");
391+
await userEvent.tab();
392+
expect(
393+
state.updateSettings.mock.calls.map(([patch]) =>
394+
Duration.toSeconds(patch.backgroundActivity.overrides.providerHealthRefreshInterval),
395+
),
396+
).toEqual([120, 120]);
397+
398+
const rejectPrevious = state.updateSettings.mock.calls.at(-1)?.[1]?.onFailed;
399+
await userEvent.type(input, "{ArrowUp}");
400+
await act(() => rejectPrevious?.());
401+
await expect.element(input).toHaveValue("150");
387402
});
388403

389404
it("restores the saved interval when a cleared field loses focus", async () => {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,13 +1121,14 @@ export function EnvironmentProviderSettings({
11211121
value={providerHealthRefreshIntervalSeconds}
11221122
min={0}
11231123
step={PROVIDER_HEALTH_INTERVAL_STEP_SECONDS}
1124-
onCommit={(seconds) =>
1124+
onCommit={(seconds, options) =>
11251125
updateSettings(
11261126
backgroundActivityOverrideSettings(
11271127
settings.backgroundActivity,
11281128
resolvedBackgroundActivity,
11291129
{ providerHealthRefreshInterval: Duration.seconds(seconds) },
11301130
),
1131+
options,
11311132
)
11321133
}
11331134
/>

‎apps/web/src/hooks/useSettings.ts‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,12 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
588588
reportFailure: false,
589589
});
590590
const { environments } = useEnvironments();
591-
const persistServerSettings = async (request: Parameters<typeof persist>[0]) => {
591+
const persistServerSettings = async (
592+
request: Parameters<typeof persist>[0],
593+
onFailed?: () => void,
594+
) => {
592595
const result = await persist(request);
596+
if (result._tag === "Failure") onFailed?.();
593597
if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
594598
const label =
595599
environments.find((environment) => environment.environmentId === request.environmentId)
@@ -604,7 +608,10 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
604608
return result;
605609
};
606610
const sharedSettingsSyncTargetIds = useSharedSettingsSyncTargetIds(true);
607-
const updateSettings = (patch: UnifiedSettingsPatch) => {
611+
const updateSettings = (
612+
patch: UnifiedSettingsPatch,
613+
options?: { readonly onFailed?: () => void },
614+
) => {
608615
const { serverPatch, clientPatch } = splitPatch(patch);
609616

610617
const canWriteServerPatch =
@@ -613,6 +620,7 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
613620
readEnvironmentScope(environmentId, scope),
614621
);
615622
if (Object.keys(serverPatch).length > 0 && !canWriteServerPatch) {
623+
options?.onFailed?.();
616624
toastManager.add({
617625
type: "warning",
618626
title: "Setting not saved",
@@ -622,18 +630,20 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
622630
if (Object.keys(serverPatch).length > 0 && canWriteServerPatch) {
623631
const { sharedPatch, localPatch } = splitSharedServerPatch(serverPatch);
624632
// Dropping the write silently leaves the control looking saved.
625-
const warnUnsaved = (description = PRIMARY_SETTINGS_UNAVAILABLE_MESSAGE) =>
626-
toastManager.add({
633+
const warnUnsaved = (description = PRIMARY_SETTINGS_UNAVAILABLE_MESSAGE) => {
634+
options?.onFailed?.();
635+
return toastManager.add({
627636
type: "warning",
628637
title: "Setting not saved",
629638
description,
630639
});
640+
};
631641
if (Object.keys(localPatch).length > 0) {
632642
if (environmentId) {
633-
void persistServerSettings({
634-
environmentId,
635-
input: { patch: localPatch },
636-
});
643+
void persistServerSettings(
644+
{ environmentId, input: { patch: localPatch } },
645+
options?.onFailed,
646+
);
637647
} else {
638648
warnUnsaved();
639649
}
@@ -688,6 +698,12 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
688698
result: await persist(request),
689699
})),
690700
).then((outcomes) => {
701+
if (
702+
deniedLabels.length > 0 ||
703+
outcomes.some(({ result }) => result._tag === "Failure")
704+
) {
705+
options?.onFailed?.();
706+
}
691707
const failures = outcomes.flatMap(({ label, result }) => {
692708
if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return [];
693709
const error = squashAtomCommandFailure(result);

0 commit comments

Comments
 (0)