Skip to content

Commit 4b67a6d

Browse files
fix(web): report partial shared settings saves
1 parent a3f92c8 commit 4b67a6d

2 files changed

Lines changed: 70 additions & 7 deletions

File tree

‎apps/web/src/hooks/useSettings.sync.test.tsx‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,40 @@ afterEach(async () => {
122122
});
123123

124124
describe("shared settings writes", () => {
125+
it("waits for the remaining target before reporting a partial save", async () => {
126+
let finishRemote = () => {};
127+
const remote = new Promise((resolve) => {
128+
finishRemote = () => resolve(AsyncResult.success(DEFAULT_SERVER_SETTINGS));
129+
});
130+
state.persist.mockResolvedValueOnce(
131+
AsyncResult.failure(Cause.fail(new Error("Permission denied"))),
132+
);
133+
state.persist.mockReturnValueOnce(remote);
134+
await mountEditor();
135+
await act(async () => saveSharedSettings());
136+
expect(state.toast).not.toHaveBeenCalled();
137+
await act(async () => finishRemote());
138+
expect(state.toast).toHaveBeenCalledExactlyOnceWith({
139+
type: "error",
140+
title: "Setting saved on some environments",
141+
description: "Could not save on primary: Permission denied\nSaved on remote.",
142+
});
143+
});
144+
145+
it("keeps a single-target failure specific to that environment", async () => {
146+
state.registry!.set(state.sessions.get(remoteId)!, AsyncResult.success(session([])));
147+
state.persist.mockResolvedValueOnce(
148+
AsyncResult.failure(Cause.fail(new Error("Permission denied"))),
149+
);
150+
await mountEditor();
151+
await act(async () => saveSharedSettings());
152+
expect(state.toast).toHaveBeenCalledExactlyOnceWith({
153+
type: "error",
154+
title: "Setting not saved",
155+
description: "Could not save on primary: Permission denied",
156+
});
157+
});
158+
125159
it("names the failed environment when a server rejects a save after the grant check", async () => {
126160
state.persist.mockResolvedValueOnce(AsyncResult.success(DEFAULT_SERVER_SETTINGS));
127161
state.persist.mockResolvedValueOnce(
@@ -131,8 +165,8 @@ describe("shared settings writes", () => {
131165
await act(async () => saveSharedSettings());
132166
expect(state.toast).toHaveBeenCalledExactlyOnceWith({
133167
type: "error",
134-
title: "Setting not saved",
135-
description: "Could not save on remote: Permission denied",
168+
title: "Setting saved on some environments",
169+
description: "Could not save on remote: Permission denied\nSaved on primary.",
136170
});
137171
});
138172

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
540540
if (environmentId) {
541541
targets.add(environmentId);
542542
}
543-
let wroteToTarget = false;
543+
const writes: Array<Parameters<typeof persist>[0]> = [];
544544
const deniedLabels: string[] = [];
545545
for (const targetId of targets) {
546546
const target = environments.find((candidate) => candidate.environmentId === targetId);
@@ -562,28 +562,57 @@ function useUpdateSettingsTarget(environmentId: EnvironmentId | null) {
562562
deniedLabels.push(target?.label ?? targetId);
563563
continue;
564564
}
565-
wroteToTarget = true;
566-
void persistServerSettings({
565+
writes.push({
567566
environmentId: targetId,
568567
input: { patch: targetPatch },
569568
});
570569
}
571-
if (!wroteToTarget) {
570+
if (writes.length === 0) {
572571
warnUnsaved(
573572
deniedLabels.length > 0
574573
? `This connection lacks permission to change settings on ${deniedLabels.join(", ")}.`
575574
: targets.size > 0
576575
? "Update older servers to save this setting."
577576
: undefined,
578577
);
578+
} else {
579+
void Promise.all(
580+
writes.map(async (request) => ({
581+
label:
582+
environments.find((target) => target.environmentId === request.environmentId)
583+
?.label ?? request.environmentId,
584+
result: await persist(request),
585+
})),
586+
).then((outcomes) => {
587+
const failures = outcomes.flatMap(({ label, result }) => {
588+
if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return [];
589+
const error = squashAtomCommandFailure(result);
590+
return [
591+
`Could not save on ${label}: ${error instanceof Error ? error.message : "The save failed. Try reconnecting and saving again."}`,
592+
];
593+
});
594+
if (failures.length === 0) return;
595+
const saved = outcomes
596+
.filter(({ result }) => result._tag === "Success")
597+
.map(({ label }) => label);
598+
toastManager.add({
599+
type: "error",
600+
title:
601+
saved.length > 0 ? "Setting saved on some environments" : "Setting not saved",
602+
description: [
603+
...failures,
604+
...(saved.length > 0 ? [`Saved on ${saved.join(", ")}.`] : []),
605+
].join("\n"),
606+
});
607+
});
579608
}
580609
}
581610
}
582611
if (Object.keys(clientPatch).length > 0) {
583612
void persistClientSettingsPatch(clientPatch);
584613
}
585614
},
586-
[environmentId, environments, persistServerSettings, sharedSettingsSyncTargetIds],
615+
[environmentId, environments, persist, persistServerSettings, sharedSettingsSyncTargetIds],
587616
);
588617

589618
return updateSettings;

0 commit comments

Comments
 (0)