import type { QueryClient } from "@tanstack/react-query"; import { createRootRouteWithContext, createRoute, createRouter, lazyRouteComponent, Outlet, redirect, } from "@tanstack/react-router"; import { RouteError, RoutePending } from "./components/RouteStates.tsx"; import { LOCALE_LIST, withMessages } from "./i18n/index.tsx"; import { type AnalyticsSearch, validateAnalyticsSearch } from "./lib/analytics-search.ts"; import { api } from "./lib/api.ts"; import { authClient } from "./lib/auth.ts"; import { isQueueTab, type QueueTab } from "./lib/comments-shell.ts"; import { listingSearch } from "./lib/listing-search.ts"; import { cachedSession } from "./lib/session-cache.ts"; import { AccountShell } from "./routes/AccountShell.tsx"; import { SiteShell } from "./routes/SiteShell.tsx"; /** * Screens load on demand (one chunk each); the shells above stay in the main bundle so the frame * renders immediately. `defaultPreload: "intent"` fetches a screen's chunk on hover/focus. */ const AccountPage = lazyRouteComponent(() => import("./routes/AccountPage.tsx"), "AccountPage"); const AnalyticsPage = lazyRouteComponent( () => withMessages(import("./routes/AnalyticsPage.tsx"), ["analytics"]), "AnalyticsPage", ); const ActivityPage = lazyRouteComponent(() => import("./routes/ActivityPage.tsx"), "ActivityPage"); const AgentsPage = lazyRouteComponent( () => withMessages(import("./routes/AgentsPage.tsx"), [ "analyticsSettings", "commentsSettings", "connectedAgents", ]), "AgentsPage", ); const ApprovalsPage = lazyRouteComponent( () => withMessages(import("./routes/ApprovalsPage.tsx"), ["comments"]), "ApprovalsPage", ); const ArticleEditorPage = lazyRouteComponent( () => withMessages(import("./routes/ArticleEditorPage.tsx"), [ "readers", "comments", "bodyEditor", "media", "menus", "fields", ]), "ArticleEditorPage", ); const ArticlesPage = lazyRouteComponent( () => withMessages(import("./routes/ArticlesPage.tsx"), ["readers", "menus", "listings"]), "ArticlesPage", ); const CollectionsPage = lazyRouteComponent( () => withMessages(import("./routes/CollectionsPage.tsx"), ["collections"]), "CollectionsPage", ); const CollectionSettingsPage = lazyRouteComponent( () => withMessages(import("./routes/CollectionSettingsPage.tsx"), [ "collections", "commentsSettings", "fields", "listings", ]), "CollectionSettingsPage", ); const CommentsPage = lazyRouteComponent( () => withMessages(import("./routes/CommentsPage.tsx"), ["comments"]), "CommentsPage", ); const CommentThreadPage = lazyRouteComponent( () => withMessages(import("./routes/CommentThreadPage.tsx"), ["comments"]), "CommentThreadPage", ); const CommenterPage = lazyRouteComponent( () => withMessages(import("./routes/CommenterPage.tsx"), ["comments"]), "CommenterPage", ); const LoginPage = lazyRouteComponent(() => import("./routes/LoginPage.tsx"), "LoginPage"); const OAuthConsentPage = lazyRouteComponent( () => withMessages(import("./routes/OAuthConsentPage.tsx"), ["oauthConsent"]), "OAuthConsentPage", ); const InvitePage = lazyRouteComponent(() => import("./routes/InvitePage.tsx"), "InvitePage"); const ResetPasswordPage = lazyRouteComponent( () => import("./routes/ResetPasswordPage.tsx"), "ResetPasswordPage", ); const ForgotPasswordPage = lazyRouteComponent( () => import("./routes/ForgotPasswordPage.tsx"), "ForgotPasswordPage", ); const MembersPage = lazyRouteComponent(() => import("./routes/MembersPage.tsx"), "MembersPage"); const PageEditorPage = lazyRouteComponent( () => withMessages(import("./routes/PageEditorPage.tsx"), [ "readers", "bodyEditor", "media", "menus", "listings", ]), "PageEditorPage", ); const BuildPage = lazyRouteComponent( () => withMessages(import("./routes/BuildPage.tsx"), ["build"]), "BuildPage", ); const PagesPage = lazyRouteComponent( () => withMessages(import("./routes/PagesPage.tsx"), ["readers", "menus"]), "PagesPage", ); const SiteOverviewPage = lazyRouteComponent( () => withMessages(import("./routes/SiteOverviewPage.tsx"), ["readers"]), "SiteOverviewPage", ); const SiteSettingsPage = lazyRouteComponent( // The privacy notice is offered in every language, so every locale's settings words load. () => withMessages( import("./routes/SiteSettingsPage.tsx"), [ "analyticsSettings", "commentsSettings", "scheduleSettings", "aiSettings", "appearance", "media", ], LOCALE_LIST, ), "SiteSettingsPage", ); const SitesPage = lazyRouteComponent(() => import("./routes/SitesPage.tsx"), "SitesPage"); const ProjectPage = lazyRouteComponent(() => import("./routes/ProjectPage.tsx"), "ProjectPage"); const SkillsPage = lazyRouteComponent(() => import("./routes/SkillsPage.tsx"), "SkillsPage"); const SnapshotsPage = lazyRouteComponent( () => withMessages(import("./routes/SnapshotsPage.tsx"), ["menus"]), "SnapshotsPage", ); const TaxonomyPage = lazyRouteComponent(() => import("./routes/TaxonomyPage.tsx"), "TaxonomyPage"); const TermEditorPage = lazyRouteComponent( () => withMessages(import("./routes/TermEditorPage.tsx"), ["media", "menus"]), "TermEditorPage", ); const NavigationPage = lazyRouteComponent( () => withMessages(import("./routes/NavigationPage.tsx"), ["menus"]), "NavigationPage", ); const MediaPage = lazyRouteComponent( () => withMessages(import("./routes/MediaPage.tsx"), ["media"]), "MediaPage", ); const AssetPage = lazyRouteComponent( () => withMessages(import("./routes/AssetPage.tsx"), ["media"]), "AssetPage", ); const TranslationsPage = lazyRouteComponent( () => import("./routes/TranslationsPage.tsx"), "TranslationsPage", ); const RedirectsPage = lazyRouteComponent( () => import("./routes/RedirectsPage.tsx"), "RedirectsPage", ); const TERM_FILTERS = new Set(["all", "public", "internal", "proposed", "retired"]); const slugParam = (v: unknown): string | undefined => typeof v === "string" && /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(v) ? v : undefined; export interface RouterContext { queryClient: QueryClient; } async function requireSession() { // Kept between navigations (lib/session-cache.ts): asking each time held every screen back. const last: { status?: number } = {}; const data = await cachedSession(async () => { const r = await authClient.getSession(); if (r.error) last.status = r.error.status; return r.data; }); if (data) return data; // RFC-0036 §5: a playground has no sign-in (its API refuses /api/auth with 403) and answers // every call as its one fixed principal. // (A caller that shared another's request has no status of its own: it asks too.) if ((last.status ?? 403) === 403 && (await isPlayground())) return null; throw redirect({ to: "/login" }); } async function isPlayground(): Promise { try { return (await api.instance()).features.playground !== undefined; } catch { return false; } } const rootRoute = createRootRouteWithContext()({ component: () => }); const loginRoute = createRoute({ getParentRoute: () => rootRoute, path: "/login", component: LoginPage, }); /** * Public token pages (RFC-0005 §7): the link's secret is in the fragment (`#chinv_…`, `#chrst_…`), * which the page takes and strips at once; no session needed. */ const inviteRoute = createRoute({ getParentRoute: () => rootRoute, path: "/invite", component: InvitePage, }); /** RFC-0011: asks for a reset link by email; public, answers the same for every address. */ const forgotPasswordRoute = createRoute({ getParentRoute: () => rootRoute, path: "/forgot-password", component: ForgotPasswordPage, }); const resetPasswordRoute = createRoute({ getParentRoute: () => rootRoute, path: "/reset-password", component: ResetPasswordPage, }); const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: "/", beforeLoad: async () => { await requireSession(); throw redirect({ to: "/sites" }); }, }); // Account surface const accountRoute = createRoute({ getParentRoute: () => rootRoute, id: "account", beforeLoad: requireSession, component: AccountShell, }); /** RFC-0044 §4: an MCP client's authorization request, after sign-in (the provider sends it here). */ const oauthConsentRoute = createRoute({ getParentRoute: () => accountRoute, path: "/oauth/consent", component: OAuthConsentPage, }); const sitesRoute = createRoute({ getParentRoute: () => accountRoute, path: "/sites", component: SitesPage, }); const projectRoute = createRoute({ getParentRoute: () => accountRoute, path: "/projects/$projectId", component: ProjectPage, }); const agentsRoute = createRoute({ getParentRoute: () => accountRoute, path: "/agents", component: AgentsPage, }); const skillsRoute = createRoute({ getParentRoute: () => accountRoute, path: "/agents/skills", component: SkillsPage, }); const accountSettingsRoute = createRoute({ getParentRoute: () => accountRoute, path: "/account", component: AccountPage, }); /** Settings › Team (RFC-0005): members, invites; `?view=activity` for the deployment's events. */ const teamRoute = createRoute({ getParentRoute: () => accountRoute, path: "/team", validateSearch: (search: Record): { view?: "activity" } => search.view === "activity" ? { view: "activity" } : {}, component: MembersPage, }); // Site surface const siteRoute = createRoute({ getParentRoute: () => rootRoute, path: "/sites/$siteId", beforeLoad: requireSession, component: SiteShell, }); const siteIndexRoute = createRoute({ getParentRoute: () => siteRoute, path: "/", component: SiteOverviewPage, }); /** Site › Overview › Analytics (RFC-0006 §10.1): `?range=7d` or `?from=&to=`, `&compare=0`, `&filter=`. */ const analyticsRoute = createRoute({ getParentRoute: () => siteRoute, path: "/analytics", validateSearch: (search: Record): AnalyticsSearch => validateAnalyticsSearch(search), component: AnalyticsPage, }); const pagesRoute = createRoute({ getParentRoute: () => siteRoute, path: "/pages", component: PagesPage, }); /** `?category=&tag=`: the facet filters (linked from Categories & tags). */ function facetSearch(search: Record): { category?: string; tag?: string } { const category = slugParam(search.category); const tag = slugParam(search.tag); return { ...(category ? { category } : {}), ...(tag ? { tag } : {}) }; } /** RFC-0040: Articles is the `posts` collection; old links keep working. */ const articlesRoute = createRoute({ getParentRoute: () => siteRoute, path: "/articles", validateSearch: facetSearch, beforeLoad: ({ params, search }) => { throw redirect({ to: "/sites/$siteId/collections/$collection", params: { siteId: params.siteId, collection: "posts" }, search, replace: true, }); }, }); /** RFC-0040 §4: every collection of the registry, with "New collection". */ const collectionsRoute = createRoute({ getParentRoute: () => siteRoute, path: "/collections", component: CollectionsPage, }); const collectionRoute = createRoute({ getParentRoute: () => siteRoute, path: "/collections/$collection", // RFC-0042 §4: list-field filters (`f_`) and the field sort (`sort`) live in the URL too. validateSearch: (search: Record) => ({ ...facetSearch(search), ...listingSearch(search), }), component: ArticlesPage, }); const collectionSettingsRoute = createRoute({ getParentRoute: () => siteRoute, path: "/collections/$collection/settings", component: CollectionSettingsPage, }); /** Site › Categories & tags (RFC-0002 §8): `?taxonomy=&filter=`. */ const taxonomyRoute = createRoute({ getParentRoute: () => siteRoute, path: "/taxonomy", validateSearch: ( search: Record, ): { taxonomy?: string; filter?: "all" | "public" | "internal" | "proposed" | "retired"; } => { const taxonomy = slugParam(search.taxonomy); const filter = typeof search.filter === "string" && TERM_FILTERS.has(search.filter) ? (search.filter as "all") : undefined; return { ...(taxonomy ? { taxonomy } : {}), ...(filter ? { filter } : {}) }; }, component: TaxonomyPage, }); const termEditorRoute = createRoute({ getParentRoute: () => siteRoute, path: "/taxonomy/$termId", component: TermEditorPage, }); /** Site › Settings › Navigation (RFC-0003 §8): `?menu=&item=`. */ const navigationRoute = createRoute({ getParentRoute: () => siteRoute, path: "/navigation", validateSearch: (search: Record): { menu?: string; item?: string } => { const menu = slugParam(search.menu); const item = typeof search.item === "string" && /^item_[A-Za-z0-9]{6,32}$/.test(search.item) ? search.item : undefined; return { ...(menu ? { menu } : {}), ...(item ? { item } : {}) }; }, component: NavigationPage, }); /** Site › Content › Media (RFC-0004 §8): the library, and one file's page. */ const mediaRoute = createRoute({ getParentRoute: () => siteRoute, path: "/media", component: MediaPage, }); const assetRoute = createRoute({ getParentRoute: () => siteRoute, path: "/media/$assetId", component: AssetPage, }); /** Site › Content › Comments (RFC-0007 §14): the moderation queue, `?tab=reported` etc. */ const commentsRoute = createRoute({ getParentRoute: () => siteRoute, path: "/comments", validateSearch: (search: Record): { tab?: QueueTab } => isQueueTab(search.tab) && search.tab !== "pending" ? { tab: search.tab } : {}, component: CommentsPage, }); const commentIdParam = (v: unknown): string | undefined => typeof v === "string" && /^[A-Za-z0-9_-]{1,64}$/.test(v) ? v : undefined; /** One article's thread: `?comment=` lands on a comment, `?reply=` opens the reply form. */ const commentThreadRoute = createRoute({ getParentRoute: () => siteRoute, path: "/comments/$nodeId", validateSearch: (search: Record): { comment?: string; reply?: string } => { const comment = commentIdParam(search.comment); const reply = commentIdParam(search.reply); return { ...(comment ? { comment } : {}), ...(reply ? { reply } : {}) }; }, component: CommentThreadPage, }); /** A commenter (`commenters:data`): identity, their comments, ban, export, erase. */ const commenterRoute = createRoute({ getParentRoute: () => siteRoute, path: "/commenters/$commenterId", component: CommenterPage, }); /** Site › Translations (RFC-0033 §8): the coverage view. */ const translationsRoute = createRoute({ getParentRoute: () => siteRoute, path: "/translations", component: TranslationsPage, }); /** Site › Settings › Redirects. */ const redirectsRoute = createRoute({ getParentRoute: () => siteRoute, path: "/redirects", component: RedirectsPage, }); /** * Site › Automation › Schedules (RFC-0008 §15.1): Calendar · Automations · History, a release — * lazy screens with their words. They check their own URL (`schedules-search.ts`), so the entry * carries none of it. */ const scheduleRoute =

(path: P, load: () => Promise, name: string) => createRoute({ getParentRoute: () => siteRoute, path, component: lazyRouteComponent(() => withMessages(load(), ["schedules"]), name as never), }); const schedulesRoute = scheduleRoute( "/schedules", () => import("./routes/SchedulesPage.tsx"), "SchedulesPage", ); const automationsRoute = scheduleRoute( "/schedules/automations", () => import("./routes/AutomationsPage.tsx"), "AutomationsPage", ); const scheduleHistoryRoute = scheduleRoute( "/schedules/history", () => import("./routes/ScheduleHistoryPage.tsx"), "ScheduleHistoryPage", ); const releaseRoute = scheduleRoute( "/schedules/releases/$releaseId", () => import("./routes/ReleasePage.tsx"), "ReleasePage", ); const snapshotsRoute = createRoute({ getParentRoute: () => siteRoute, path: "/snapshots", /** `?diff=` opens that snapshot's changes (linked from Activity). */ validateSearch: (search: Record): { diff?: string } => typeof search.diff === "string" && search.diff ? { diff: search.diff } : {}, component: SnapshotsPage, }); const activityRoute = createRoute({ getParentRoute: () => siteRoute, path: "/activity", component: ActivityPage, }); /** Activity › Approvals: agents' gated calls waiting for a person (ADR-0014). */ const approvalsRoute = createRoute({ getParentRoute: () => siteRoute, path: "/approvals", component: ApprovalsPage, }); const siteSettingsRoute = createRoute({ getParentRoute: () => siteRoute, path: "/settings", component: SiteSettingsPage, }); const pageEditorRoute = createRoute({ getParentRoute: () => siteRoute, path: "/pages/$pageId", component: PageEditorPage, }); // RFC-0020 §6: a page as agents build it, live. const buildRoute = createRoute({ getParentRoute: () => siteRoute, path: "/pages/$pageId/build", // The editor's "Change with AI" aims Build chat at a section or component of the page. validateSearch: (search: Record): { section?: string; component?: string } => ({ ...(typeof search.section === "string" && search.section.length <= 64 ? { section: search.section } : {}), ...(typeof search.component === "string" && search.component.length <= 64 ? { component: search.component } : {}), }), component: BuildPage, }); /** RFC-0040 §4: an entry's editor, under its collection. */ const entryEditorRoute = createRoute({ getParentRoute: () => siteRoute, path: "/collections/$collection/$entryId", component: ArticleEditorPage, }); /** Old links and bookmarks: `posts`, which the editor corrects to the entry's own collection. */ const articleEditorRoute = createRoute({ getParentRoute: () => siteRoute, path: "/articles/$entryId", beforeLoad: ({ params }) => { throw redirect({ to: "/sites/$siteId/collections/$collection/$entryId", params: { siteId: params.siteId, collection: "posts", entryId: params.entryId }, replace: true, }); }, }); const routeTree = rootRoute.addChildren([ loginRoute, inviteRoute, resetPasswordRoute, forgotPasswordRoute, indexRoute, accountRoute.addChildren([ oauthConsentRoute, sitesRoute, projectRoute, agentsRoute, skillsRoute, accountSettingsRoute, teamRoute, ]), siteRoute.addChildren([ siteIndexRoute, analyticsRoute, pagesRoute, articlesRoute, collectionsRoute, collectionRoute, collectionSettingsRoute, entryEditorRoute, snapshotsRoute, activityRoute, approvalsRoute, siteSettingsRoute, pageEditorRoute, buildRoute, articleEditorRoute, taxonomyRoute, termEditorRoute, redirectsRoute, translationsRoute, navigationRoute, mediaRoute, assetRoute, commentsRoute, commentThreadRoute, commenterRoute, schedulesRoute, automationsRoute, scheduleHistoryRoute, releaseRoute, ]), ]); export function createStudioRouter(queryClient: QueryClient) { return createRouter({ routeTree, context: { queryClient }, defaultPreload: "intent", // The router's own default waits a second before showing the pending screen, so a short wait // looked frozen: the URL had changed and nothing else had. defaultPendingMs: 150, defaultPendingMinMs: 300, defaultPendingComponent: RoutePending, defaultErrorComponent: RouteError, }); } declare module "@tanstack/react-router" { interface Register { router: ReturnType; } }