fix(console): save groups with guarded writes

Whole-list saves can overwrite unrelated groups. Use per-group
revision-aware writes, retain rename results and confirm deletion
on the exact group, routes and version shown to the user.
This commit is contained in:
翅膀 committed 2026-10-23 23:23:43 +08:00
1 parent 85c73d1122
commit 6eb9cbab4e
2 files changed
+119 -29

No files matched your search

+41 -18
View File
@@ -568,7 +568,9 @@ const {
needLogin,
error: groupsError,
load: loadGroups,
save: saveGroups,
update: updateGroup,
create: createGroup,
remove: removeGroup,
rename: groupsRename,
} = useGroupsApi();
const {
@@ -1036,8 +1038,8 @@ function openEditGroup(group: Group): void {
}
/**
* Group writes replace the whole group list, so they run one at a time, each
* built from the list the previous one saved; true when one may start.
* Group writes run one at a time, each built on what the previous one saved;
* true when one may start.
*/
function canWriteGroups(): boolean {
if (!savingGroup.value) return true;
@@ -1049,8 +1051,7 @@ async function onSaveGroupFromPanel(group: Group): Promise<void> {
if (!canWriteGroups()) return;
savingGroup.value = true;
try {
const next = groups.value.map((g) => (g.id === group.id ? group : g));
await saveGroups(next);
await updateGroup(group);
push(t("toast.groupSaved"));
} catch (err) {
push(t("toast.saveFailed", { msg: err instanceof Error ? err.message : String(err) }), "bad");
@@ -1066,25 +1067,34 @@ async function onSaveGroup(group: Group): Promise<void> {
savingGroup.value = true;
try {
const editing = editingGroup.value;
let next: Group[];
if (editing) {
// The editor does not show these fields; keep what the group has.
const edited: Group = { ...group, isolated: editing.isolated, revision: editing.revision };
if (group.id !== editing.id) {
// Id changed: rename first so routes/webhook secret/invites follow,
// then persist the remaining edits under the new id.
await groupsRename(editing.id, group.id);
// A list loaded meanwhile may already hold the new id as well.
next = [...groups.value.filter((g) => g.id !== editing.id && g.id !== group.id), group];
// Id changed: rename first so routes/webhook secret/invites follow
// (refused if the group changed since the editor opened), then
// persist the remaining edits under the new id, on top of exactly the
// renamed group. If that second step fails, the editor already points
// at the new id, so saving again only retries the update.
const renamed = await groupsRename(editing.id, group.id, editing.revision);
if (session === groupEditorSession) editingGroup.value = renamed;
// Server-decided fields (e.g. isolation made explicit by the rename)
// come from the renamed group, not from the editor's older copy.
await updateGroup({
...edited,
isolated: renamed.isolated,
revision: renamed.revision,
});
} else {
next = groups.value.map((g) => (g.id === editing.id ? group : g));
await updateGroup(edited);
}
} else {
if (groups.value.some((g) => g.id === group.id)) {
push(t("toast.groupIdExists"), "bad");
return;
}
next = [...groups.value, group];
await createGroup(group);
}
await saveGroups(next);
if (session === groupEditorSession) groupEditorOpen.value = false;
push(t("toast.groupSaved"));
await loadGroups();
@@ -1101,13 +1111,25 @@ async function onDeleteGroup(group: Group): Promise<void> {
// Fetch the real route count for this group instead of relying on the
// routes of whichever group happens to be open in the detail view.
let used = 0;
let routesRevision: string | undefined;
let version: number | null = null;
try {
const data = await apiFetch<{ routes?: Route[] }>(
const data = await apiFetch<{ routes?: Route[]; revision?: string; version?: number | null }>(
`/admin/api/groups/${encodeURIComponent(group.id)}/routes`,
);
used = (data.routes ?? []).length;
} catch {
// Count is best-effort; proceed without the warning.
routesRevision = data.revision;
version = data.version ?? null;
} catch (err) {
// The deletion is confirmed on what the routes are now: without them,
// a route added meanwhile could be deleted unseen.
if (visit === pageVisit) {
push(
t("toast.deleteFailed", { msg: err instanceof Error ? err.message : String(err) }),
"bad",
);
}
return;
}
// The list was left while the count loaded: never ask on another page.
if (visit !== pageVisit) return;
@@ -1118,7 +1140,8 @@ async function onDeleteGroup(group: Group): Promise<void> {
if (!confirmed || visit !== pageVisit || !canWriteGroups()) return;
savingGroup.value = true;
try {
await saveGroups(groups.value.filter((g) => g.id !== group.id));
// Deletes the group as confirmed: a route added meanwhile makes it fail.
await removeGroup(group.id, group.revision, routesRevision, version);
push(t("toast.groupDeleted"));
} catch (err) {
push(t("toast.deleteFailed", { msg: err instanceof Error ? err.message : String(err) }), "bad");