refactor(admin): unify webui fetch/copy/format helpers; fix audit tab load and delete-group count

- apiFetch + shared needLogin (any 401 shows the login card) replaces six
  duplicated fetch/401/error-handling blocks in the admin composables
- useCopy composable: clipboard + execCommand fallback with timed reset,
  used by MembersPanel and WebhookPanel
- utils/format.ts (fmtTime/splitList/parseMatch) removes inline duplicates
  across SendLogs, AuditLog, RouteEditor and GroupEditor
- fix: load the audit log when the audit tab becomes active (client-side
  navigation never remounted the page, leaving the list empty on first visit)
- fix: fetch the real route count for a group before confirming deletion
  instead of reusing the currently open group's route list
This commit is contained in:
RhenCloud 2026-08-14 07:18:55 +08:00
parent c438fe96dc
commit e59b10f739
No known key found for this signature in database
GPG key ID: A574A617378C4E0B
16 changed files with 137 additions and 202 deletions

View file

@ -311,9 +311,18 @@ onMounted(() => {
}
loadGroups();
loadLogs(50, logFilterGroup.value || undefined);
if (view.value === "audit") loadAudit(50, auditFilterGroup.value || undefined);
});
// Tab switches are client-side navigations (no remount), so load the audit
// log whenever the audit view becomes active including direct deep links.
watch(
view,
(v) => {
if (v === "audit") loadAudit(50, auditFilterGroup.value || undefined);
},
{ immediate: true },
);
function switchView(next: "groups" | "logs" | "audit"): void {
const path = next === "groups" ? "/admin" : `/admin/${next}`;
if (route.path !== path) router.replace(path);
@ -469,7 +478,17 @@ async function onSaveGroup(group: Group): Promise<void> {
}
async function onDeleteGroup(group: Group): Promise<void> {
const used = groupRoutes.value.filter((r) => r.groupId === group.id).length;
// 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;
try {
const data = await apiFetch<{ routes?: Route[] }>(
`/admin/api/groups/${encodeURIComponent(group.id)}/routes`,
);
used = (data.routes ?? []).length;
} catch {
// Count is best-effort; proceed without the warning.
}
const warn = used ? t("confirm.deleteGroupWarn", { n: used }) : "";
if (!window.confirm(t("confirm.deleteGroup", { name: group.name || group.id }) + warn)) return;
try {