|
1 | 1 | import type { ReactElement } from "react"; |
| 2 | +import type { QueryClient } from "@tanstack/react-query"; |
2 | 3 | import { useQueryClient } from "@tanstack/react-query"; |
3 | | -import { RouterProvider, Route, createHashRouter, createRoutesFromElements, Outlet } from "react-router-dom"; |
4 | | -import { PrivateRouteLogic } from "./logic/PrivateRouteLogic"; |
5 | | -import { PublicRouteLogic } from "./logic/PublicRouteLogic"; |
6 | | -import { PrivateRoutes } from "./PrivateRoutes"; |
7 | | -import { PublicRoutes } from "./PublicRoutes"; |
| 4 | +import { RouterProvider, createHashHistory, createRouter } from "@tanstack/react-router"; |
| 5 | +import { routeTree } from "../../routeTree.gen"; |
| 6 | +import { useAuth } from "../auth/UseAuth"; |
| 7 | +import type { AuthStatus } from "~/classes/auth/AuthStatus"; |
| 8 | + |
| 9 | +export interface RouterContext { |
| 10 | + queryClient: QueryClient; |
| 11 | + authStatus: AuthStatus; |
| 12 | +} |
| 13 | + |
| 14 | +const hashHistory = createHashHistory(); |
| 15 | + |
| 16 | +const router = createRouter({ |
| 17 | + routeTree, |
| 18 | + history: hashHistory, |
| 19 | + context: { |
| 20 | + queryClient: undefined!, |
| 21 | + authStatus: undefined!, |
| 22 | + }, |
| 23 | + defaultPreload: "intent", |
| 24 | + defaultPreloadStaleTime: 0, |
| 25 | +}); |
| 26 | + |
| 27 | +declare module "@tanstack/react-router" { |
| 28 | + interface Register { |
| 29 | + router: typeof router; |
| 30 | + } |
| 31 | +} |
8 | 32 |
|
9 | 33 | export function AppRoutes(): ReactElement { |
10 | 34 | const queryClient = useQueryClient(); |
| 35 | + const { status } = useAuth(); |
11 | 36 |
|
12 | | - return ( |
13 | | - <RouterProvider |
14 | | - router={createHashRouter( |
15 | | - createRoutesFromElements( |
16 | | - <Route path="/" element={<Outlet />}> |
17 | | - <Route element={<PublicRouteLogic />}> |
18 | | - {PublicRoutes(queryClient).map((route) => ( |
19 | | - <Route key={route.path} {...route} /> |
20 | | - ))} |
21 | | - </Route> |
22 | | - <Route element={<PrivateRouteLogic />}> |
23 | | - {PrivateRoutes(queryClient).map((route) => ( |
24 | | - <Route key={route.path} {...route} /> |
25 | | - ))} |
26 | | - </Route> |
27 | | - <Route path="*" lazy={() => import("../../pages/not-found/NotFoundRoute")} /> |
28 | | - </Route> |
29 | | - ) |
30 | | - )} |
31 | | - /> |
32 | | - ); |
| 37 | + return <RouterProvider router={router} context={{ queryClient, authStatus: status }} />; |
33 | 38 | } |
0 commit comments