feat(console): give each group its own routes URL

Resolve the selected group from /admin/groups/{groupId}, validate console paths, and use browser history for back navigation. Keep Groups active and let an active mobile-tab tap return to the list. Scope pending route loads to their group and retain the count beside long headings.
This commit is contained in:
翅膀 committed 2026-10-17 11:13:09 +08:00
1 parent ba95456e67
commit 535bfa98ce
11 files changed
+249 -63

No files matched your search

+47 -7
View File
@@ -5,30 +5,70 @@ export function useGroupRoutesApi() {
const routes = ref<Route[]>([]);
const loading = ref(false);
const error = ref("");
/** Group whose routes are shown; responses for any other group are dropped. */
const groupId = ref<string | null>(null);
/** Group whose routes were last loaded (or saved) successfully. */
const loadedId = ref<string | null>(null);
// Bumped by every load/clear, so only the latest request may land.
let request = 0;
async function load(groupId: string): Promise<void> {
/**
* The shown routes are the group's real routes. Saving replaces a group's
* whole route list, so a write is only safe from here — never from the
* empty list of a load still running or one that failed.
*/
const ready = computed(() => !!groupId.value && loadedId.value === groupId.value && !error.value);
async function load(id: string): Promise<void> {
const mine = ++request;
if (groupId.value !== id) {
routes.value = [];
loadedId.value = null;
}
groupId.value = id;
loading.value = true;
error.value = "";
needLogin.value = false;
try {
const data = await apiFetch<{ routes?: Route[] }>(
`/admin/api/groups/${encodeURIComponent(groupId)}/routes`,
`/admin/api/groups/${encodeURIComponent(id)}/routes`,
);
if (mine !== request) return;
routes.value = data.routes ?? [];
loadedId.value = id;
} catch (err) {
if (!needLogin.value) error.value = err instanceof Error ? err.message : String(err);
if (mine === request && !needLogin.value)
error.value = err instanceof Error ? err.message : String(err);
} finally {
loading.value = false;
if (mine === request) loading.value = false;
}
}
async function save(groupId: string, next: Route[]): Promise<void> {
await apiFetch(`/admin/api/groups/${encodeURIComponent(groupId)}/routes`, {
/** Forget the shown group (its page was left); late responses are ignored. */
function clear(): void {
request++;
groupId.value = null;
loadedId.value = null;
routes.value = [];
loading.value = false;
error.value = "";
}
async function save(id: string, next: Route[]): Promise<void> {
await apiFetch(`/admin/api/groups/${encodeURIComponent(id)}/routes`, {
method: "PUT",
body: JSON.stringify({ routes: next }),
});
// The page may have moved on to another group while the request ran.
if (groupId.value !== id) return;
// What was just written is the newest state: a load of this group that
// started earlier may return the old routes, so it must not land.
request++;
loading.value = false;
error.value = "";
routes.value = next;
loadedId.value = id;
}
return { routes, loading, needLogin, error, load, save };
return { routes, loading, needLogin, error, groupId, ready, load, clear, save };
}
+4
View File
@@ -8,6 +8,8 @@ export function useGroupsApi() {
const roles = ref<Record<string, GroupRole>>({});
const loading = ref(false);
const error = ref("");
/** A load has succeeded: an id missing from `groups` really is unknown. */
const loaded = ref(false);
function roleOf(groupId: string): GroupRole | undefined {
return isSuper.value ? "owner" : roles.value[groupId];
@@ -36,6 +38,7 @@ export function useGroupsApi() {
groups.value = data.groups ?? [];
isSuper.value = data.isSuper ?? false;
roles.value = data.roles ?? {};
loaded.value = true;
} catch (err) {
if (!needLogin.value) error.value = err instanceof Error ? err.message : String(err);
} finally {
@@ -67,6 +70,7 @@ export function useGroupsApi() {
canEditGroup,
canEditRoutes,
loading,
loaded,
needLogin,
error,
load,
+6 -2
View File
@@ -138,8 +138,10 @@ const en: Dict = {
"groups.myRole": "MY ROLE",
"groups.any": "any",
"groups.open": "Open",
"group.back": "Groups",
"groups.delete": "Delete group",
"group.notFound": "This group does not exist, or you have no access to it",
"group.allGroups": "All groups",
"group.routeCount": "{count} routes",
"group.routesIn": "Routes in {name}",
"routes.emptyGroup": "No routes in this group yet.",
"routes.createFirst": "Create your first route",
@@ -575,8 +577,10 @@ const zh: Dict = {
"groups.myRole": "我的角色",
"groups.any": "任意",
"groups.open": "进入",
"group.back": "返回分组",
"groups.delete": "删除分组",
"group.notFound": "分组不存在,或你没有访问权限",
"group.allGroups": "全部分组",
"group.routeCount": "{count} 条路由",
"group.routesIn": "{name} 的路由",
"routes.emptyGroup": "该分组还没有路由。",
"routes.createFirst": "创建第一个路由",