Adds front-end set-up
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
import { lazy, Suspense, type ComponentType } from 'react';
|
||||
import {
|
||||
createRootRouteWithContext,
|
||||
createRoute,
|
||||
createRouter,
|
||||
Outlet,
|
||||
redirect,
|
||||
} from '@tanstack/react-router';
|
||||
import type { AuthContextValue } from '@/contexts/auth-context';
|
||||
import { AppLayout } from '@/components/layout/AppLayout';
|
||||
import { LoginPage } from '@/pages/LoginPage';
|
||||
|
||||
export interface RouterContext {
|
||||
auth: AuthContextValue;
|
||||
}
|
||||
|
||||
function RouteFallback() {
|
||||
return (
|
||||
<div className="flex min-h-40 items-center justify-center text-muted-foreground">
|
||||
<span className="h-5 w-5 animate-spin rounded-full border-2 border-primary border-t-transparent" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap a per-feature page in a lazy boundary so Vite emits a separate chunk
|
||||
* (NFR-U1-01 / Q1-A). The auth/root shell and login stay eager for fast paint.
|
||||
*/
|
||||
function lazyPage<P extends Record<string, never>>(
|
||||
factory: () => Promise<{ [key: string]: ComponentType<P> }>,
|
||||
exportName: string,
|
||||
) {
|
||||
const Loaded = lazy(() => factory().then((module) => ({ default: module[exportName] })));
|
||||
return function LazyRouteComponent() {
|
||||
return (
|
||||
<Suspense fallback={<RouteFallback />}>
|
||||
<Loaded {...({} as P)} />
|
||||
</Suspense>
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
const rootRoute = createRootRouteWithContext<RouterContext>()({
|
||||
component: () => <Outlet />,
|
||||
});
|
||||
|
||||
// '/' redirects into the protected area; the guard sends guests to /login.
|
||||
const indexRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/',
|
||||
beforeLoad: () => {
|
||||
throw redirect({ to: '/dashboard' });
|
||||
},
|
||||
});
|
||||
|
||||
const loginRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/login',
|
||||
validateSearch: (search: Record<string, unknown>): { redirect?: string } => ({
|
||||
redirect: typeof search.redirect === 'string' ? search.redirect : undefined,
|
||||
}),
|
||||
// Authenticated users never see /login (BR-U1-06).
|
||||
beforeLoad: ({ context }) => {
|
||||
if (context.auth.isAuthenticated) {
|
||||
throw redirect({ to: '/dashboard' });
|
||||
}
|
||||
},
|
||||
component: LoginPage,
|
||||
});
|
||||
|
||||
const setupRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
path: '/setup',
|
||||
component: lazyPage(() => import('@/pages/SetupPage'), 'SetupPage'),
|
||||
});
|
||||
|
||||
// Layout route guarding every protected page (BR-U1-05).
|
||||
const authenticatedRoute = createRoute({
|
||||
getParentRoute: () => rootRoute,
|
||||
id: '_authenticated',
|
||||
beforeLoad: ({ context, location }) => {
|
||||
if (!context.auth.isAuthenticated) {
|
||||
throw redirect({ to: '/login', search: { redirect: location.href } });
|
||||
}
|
||||
},
|
||||
component: AppLayout,
|
||||
});
|
||||
|
||||
const dashboardRoute = createRoute({
|
||||
getParentRoute: () => authenticatedRoute,
|
||||
path: '/dashboard',
|
||||
component: lazyPage(() => import('@/pages/DashboardPage'), 'DashboardPage'),
|
||||
});
|
||||
|
||||
const usersRoute = createRoute({
|
||||
getParentRoute: () => authenticatedRoute,
|
||||
path: '/users',
|
||||
component: lazyPage(() => import('@/pages/UsersPage'), 'UsersPage'),
|
||||
});
|
||||
|
||||
const cmsRoute = createRoute({
|
||||
getParentRoute: () => authenticatedRoute,
|
||||
path: '/cms',
|
||||
component: lazyPage(() => import('@/pages/CmsPage'), 'CmsPage'),
|
||||
});
|
||||
|
||||
export const routeTree = rootRoute.addChildren([
|
||||
indexRoute,
|
||||
loginRoute,
|
||||
setupRoute,
|
||||
authenticatedRoute.addChildren([dashboardRoute, usersRoute, cmsRoute]),
|
||||
]);
|
||||
|
||||
export const router = createRouter({
|
||||
routeTree,
|
||||
defaultPreload: 'intent',
|
||||
// Real auth is injected per render via RouterProvider's `context` prop.
|
||||
context: { auth: undefined as unknown as AuthContextValue },
|
||||
});
|
||||
|
||||
declare module '@tanstack/react-router' {
|
||||
interface Register {
|
||||
router: typeof router;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user