-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathrouter.tsx
88 lines (81 loc) · 2.42 KB
/
router.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { QueryClient, useQueryClient } from '@tanstack/react-query';
import { useMemo } from 'react';
import { createBrowserRouter } from 'react-router';
import { RouterProvider } from 'react-router/dom';
import { paths } from '@/config/paths';
import { ProtectedRoute } from '@/lib/auth';
import {
default as AppRoot,
ErrorBoundary as AppRootErrorBoundary,
} from './routes/app/root';
const convert = (queryClient: QueryClient) => (m: any) => {
const { clientLoader, clientAction, default: Component, ...rest } = m;
return {
...rest,
loader: clientLoader?.(queryClient),
action: clientAction?.(queryClient),
Component,
};
};
export const createAppRouter = (queryClient: QueryClient) =>
createBrowserRouter([
{
path: paths.home.path,
lazy: () => import('./routes/landing').then(convert(queryClient)),
},
{
path: paths.auth.register.path,
lazy: () => import('./routes/auth/register').then(convert(queryClient)),
},
{
path: paths.auth.login.path,
lazy: () => import('./routes/auth/login').then(convert(queryClient)),
},
{
path: paths.app.root.path,
element: (
<ProtectedRoute>
<AppRoot />
</ProtectedRoute>
),
ErrorBoundary: AppRootErrorBoundary,
children: [
{
path: paths.app.discussions.path,
lazy: () =>
import('./routes/app/discussions/discussions').then(
convert(queryClient),
),
},
{
path: paths.app.discussion.path,
lazy: () =>
import('./routes/app/discussions/discussion').then(
convert(queryClient),
),
},
{
path: paths.app.users.path,
lazy: () => import('./routes/app/users').then(convert(queryClient)),
},
{
path: paths.app.profile.path,
lazy: () => import('./routes/app/profile').then(convert(queryClient)),
},
{
path: paths.app.dashboard.path,
lazy: () =>
import('./routes/app/dashboard').then(convert(queryClient)),
},
],
},
{
path: '*',
lazy: () => import('./routes/not-found').then(convert(queryClient)),
},
]);
export const AppRouter = () => {
const queryClient = useQueryClient();
const router = useMemo(() => createAppRouter(queryClient), [queryClient]);
return <RouterProvider router={router} />;
};