import { createRootRoute, createRoute, createRouter, redirect } from "@tanstack/react-router"; import { AppShell } from "@/client/components/app-shell"; import { meQueryOptions } from "@/client/auth/use-me"; import { paths } from "@/client/lib/paths"; import { queryClient } from "@/client/query"; import { AddressbooksPage } from "@/client/pages/addressbooks"; import { CalendarsPage } from "@/client/pages/calendars"; import { FilesPage } from "@/client/pages/files"; import { LoginPage } from "@/client/pages/login"; import { MePage } from "@/client/pages/me"; import { NotFoundPage } from "@/client/pages/not-found"; import { PatsPage } from "@/client/pages/pats"; const rootRoute = createRootRoute({ component: AppShell, notFoundComponent: NotFoundPage }); async function requireSession({ location }: { location: { pathname: string } }): Promise { const me = await queryClient.ensureQueryData(meQueryOptions); if (!me) { throw redirect({ to: paths.login, search: { return_to: location.pathname }, }); } } const loginRoute = createRoute({ getParentRoute: () => rootRoute, path: paths.login, validateSearch: (search: Record) => ({ return_to: typeof search.return_to === "string" ? search.return_to : paths.home, }), component: LoginPage, }); const homeRoute = createRoute({ getParentRoute: () => rootRoute, path: paths.home, beforeLoad: async ({ location }) => { await requireSession({ location }); throw redirect({ to: paths.me, replace: true }); }, }); const meRoute = createRoute({ getParentRoute: () => rootRoute, path: paths.me, beforeLoad: ({ location }) => requireSession({ location }), component: MePage, }); const patsRoute = createRoute({ getParentRoute: () => rootRoute, path: paths.pats, beforeLoad: ({ location }) => requireSession({ location }), component: PatsPage, }); const calendarsRoute = createRoute({ getParentRoute: () => rootRoute, path: paths.calendars, beforeLoad: ({ location }) => requireSession({ location }), component: CalendarsPage, }); const addressbooksRoute = createRoute({ getParentRoute: () => rootRoute, path: paths.addressbooks, beforeLoad: ({ location }) => requireSession({ location }), component: AddressbooksPage, }); const filesRoute = createRoute({ getParentRoute: () => rootRoute, path: paths.files, beforeLoad: ({ location }) => requireSession({ location }), validateSearch: (search: Record) => ({ path: typeof search.path === "string" ? search.path : "/files/", }), component: FilesPage, }); const routeTree = rootRoute.addChildren([ homeRoute, loginRoute, meRoute, patsRoute, calendarsRoute, addressbooksRoute, filesRoute, ]); export const router = createRouter({ routeTree, context: { queryClient }, defaultPreload: "intent", }); declare module "@tanstack/react-router" { interface Register { router: typeof router; } }