Files
WebHooker/app/utils/admin-path.ts
T
翅膀 535bfa98ce 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.
2026-10-17 11:13:09 +08:00

28 lines
1.1 KiB
TypeScript

/** Top-level pages of the admin console. */
export type AdminView = "overview" | "groups" | "logs" | "audit" | "metrics";
const VIEWS: readonly string[] = ["overview", "groups", "logs", "audit", "metrics"];
/**
* Read the console page from the segments after /admin: `[]` (overview),
* `["groups"]`, `["groups", groupId]` (that group's routes), `["logs"]`,
* `["audit"]`, `["metrics"]`. Anything else is no console page (null → 404).
*/
export function parseAdminPath(
slug: string | string[] | undefined,
): { view: AdminView; groupId: string | null } | null {
// A trailing slash adds an empty segment.
const [view, groupId, ...rest] = (Array.isArray(slug) ? slug : slug ? [slug] : []).filter(
Boolean,
);
if (!view) return { view: "overview", groupId: null };
if (!VIEWS.includes(view) || rest.length) return null;
// Only groups have sub-pages.
if (groupId !== undefined && view !== "groups") return null;
return { view: view as AdminView, groupId: groupId ?? null };
}
export function adminGroupPath(groupId: string): string {
return `/admin/groups/${encodeURIComponent(groupId)}`;
}