fix(groups): serialize edits and isolate group detail panels

Prevent overlapping whole-list writes, avoid duplicate ids after rename, and discard delete confirmations prepared on a page that has since been left. Key member and webhook panels by group so local state cannot cross tenants.
This commit is contained in:
翅膀 committed 2026-10-17 16:17:42 +08:00
1 parent 79c9eba1df
commit 93f111b5b2
2 files changed
+34 -5

No files matched your search

+32 -5
View File
@@ -209,6 +209,7 @@
</section>
<MembersPanel
:key="selectedGroup.id"
:group="selectedGroup"
:can-edit="canEditGroup(selectedGroup.id)"
:saving="savingGroup"
@@ -217,6 +218,7 @@
<WebhookPanel
v-if="canEditGroup(selectedGroup.id)"
:key="selectedGroup.id"
:group-id="selectedGroup.id"
:can-edit="canEditGroup(selectedGroup.id)"
/>
@@ -250,6 +252,7 @@
<RcButton
v-if="canCreateGroup"
size="sm"
:disabled="savingGroup"
@click="openNewGroup"
><Plus :size="15" />{{ t("app.newGroup") }}</RcButton
>
@@ -291,6 +294,7 @@
variant="ghost"
size="icon"
class="h-7 w-7 text-muted hover:text-text"
:disabled="savingGroup"
:title="t('groupEditor.editTitle')"
@click="openEditGroup(g)"
>
@@ -301,6 +305,7 @@
color="error"
size="icon"
class="h-7 w-7 text-muted hover:text-bad"
:disabled="savingGroup"
:title="t('groups.delete')"
:aria-label="t('groups.delete')"
@click="onDeleteGroup(g)"
@@ -358,7 +363,7 @@
<section v-if="!groupsLoading && !groups.length" class="console-empty">
<RcEmptyState :title="t('groups.empty')"
><RcButton v-if="isSuper" @click="openNewGroup"
><RcButton v-if="isSuper" :disabled="savingGroup" @click="openNewGroup"
><Plus :size="16" />{{ t("groups.createFirst") }}</RcButton
></RcEmptyState
>
@@ -589,7 +594,6 @@ const groupMissing = computed(
() => !!routeGroupId.value && groupsLoaded.value && !groupsError.value && !selectedGroup.value,
);
const groupEditorOpen = ref(false);
const editingGroup = ref<Group | null>(null);
const savingGroup = ref(false);
@@ -618,6 +622,7 @@ function canWriteRoutes(): boolean {
push(t("toast.routesNotReady"), "bad");
return false;
}
// Bumped whenever an editor opens: a save may only close the editor it was
// made from, never one opened since (on another page, for another route).
let routeEditorSession = 0;
@@ -629,6 +634,7 @@ const orderChanged = computed(
orderDraft.value.length !== groupRoutes.value.length ||
orderDraft.value.some((r, i) => r.id !== groupRoutes.value[i]?.id),
);
const canCreateRoute = computed(
() =>
!needLogin.value &&
@@ -640,7 +646,6 @@ const canReorder = computed(
() =>
!!selectedGroup.value && canEditRoutes(selectedGroup.value.id) && groupRoutes.value.length > 1,
);
/**
* Labels of the group bar's buttons, given up so the group name stays whole:
* first Reorder turns into an icon, then New route; only with both on icons
@@ -751,6 +756,8 @@ watch(
{ immediate: true },
);
// Every page is a history entry: the browser's (or device's) back button
// returns from a group's routes to the group list.
function switchView(next: View): void {
const path = next === "overview" ? "/admin" : `/admin/${next}`;
if (route.path !== path) router.push(path);
@@ -1028,7 +1035,18 @@ function openEditGroup(group: Group): void {
groupEditorOpen.value = true;
}
/**
* 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.
*/
function canWriteGroups(): boolean {
if (!savingGroup.value) return true;
push(t("toast.groupsBusy"), "bad");
return false;
}
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));
@@ -1043,6 +1061,7 @@ async function onSaveGroupFromPanel(group: Group): Promise<void> {
}
async function onSaveGroup(group: Group): Promise<void> {
if (!canWriteGroups()) return;
const session = groupEditorSession;
savingGroup.value = true;
try {
@@ -1053,7 +1072,8 @@ async function onSaveGroup(group: Group): Promise<void> {
// 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);
next = [...groups.value.filter((g) => g.id !== editing.id), group];
// 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];
} else {
next = groups.value.map((g) => (g.id === editing.id ? group : g));
}
@@ -1076,6 +1096,8 @@ async function onSaveGroup(group: Group): Promise<void> {
}
async function onDeleteGroup(group: Group): Promise<void> {
if (!canWriteGroups()) return;
const visit = pageVisit;
// 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;
@@ -1087,16 +1109,21 @@ async function onDeleteGroup(group: Group): Promise<void> {
} catch {
// Count is best-effort; proceed without the warning.
}
// The list was left while the count loaded: never ask on another page.
if (visit !== pageVisit) return;
const confirmed = await confirm({
title: t("confirm.deleteGroup", { name: group.name || group.id }),
description: used ? t("confirm.deleteGroupWarn", { n: used }).trim() : undefined,
});
if (!confirmed) return;
if (!confirmed || visit !== pageVisit || !canWriteGroups()) return;
savingGroup.value = true;
try {
await saveGroups(groups.value.filter((g) => g.id !== group.id));
push(t("toast.groupDeleted"));
} catch (err) {
push(t("toast.deleteFailed", { msg: err instanceof Error ? err.message : String(err) }), "bad");
} finally {
savingGroup.value = false;
}
}
</script>
+2
View File
@@ -422,6 +422,7 @@ const en: Dict = {
"audit.allGroups": "All groups",
"audit.refresh": "⟳ Refresh",
"toast.routesSaved": "Routes saved",
"toast.groupsBusy": "Another group change is still saving; try again in a moment",
"toast.routesNotReady": "The routes are still loading or saving; try again in a moment",
"toast.routeDeleted": "Route deleted",
"toast.groupSaved": "Groups saved",
@@ -862,6 +863,7 @@ const zh: Dict = {
"audit.allGroups": "全部分组",
"audit.refresh": "⟳ 刷新",
"toast.routesSaved": "路由已保存",
"toast.groupsBusy": "另一项分组修改仍在保存,请稍后再试",
"toast.routesNotReady": "路由仍在加载或保存中,请稍后再试",
"toast.routeDeleted": "路由已删除",
"toast.groupSaved": "分组已保存",