Files
browser/platform_web/stores/navigation.ts
T
2026-07-11 14:56:10 +08:00

24 lines
901 B
TypeScript

import type { PageId, PageParams } from "../contracts/page";
import type { CurrentUserView } from "../contracts/workspace";
import { canAccessRoute, defaultPageForUser, resolveRouteHash } from "../routes/routes";
import { firstPartyRoutes } from "../routes/routes";
const routeIds: PageId[] = firstPartyRoutes.map((route) => route.id);
export function isPageId(value: string): value is PageId {
return routeIds.includes(value as PageId);
}
export interface NavigationState {
pageId: PageId;
params: PageParams;
}
export function initialNavigationState(user: CurrentUserView, hash = typeof window === "undefined" ? "" : window.location.hash): NavigationState {
const resolved = resolveRouteHash(hash, user);
if (!canAccessRoute(user, resolved.route.id)) {
return { pageId: defaultPageForUser(user), params: {} };
}
return { pageId: resolved.route.id, params: resolved.params };
}