forked from alan2207/bulletproof-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.tsx
121 lines (115 loc) · 3.43 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { QueryClient, useQueryClient } from '@tanstack/react-query';
import { useMemo } from 'react';
import { RouterProvider, createBrowserRouter } from 'react-router-dom';
import { paths } from '@/config/paths';
import { ProtectedRoute } from '@/lib/auth';
import { AppRoot, AppRootErrorBoundary } from './routes/app/root';
export const createAppRouter = (queryClient: QueryClient) =>
createBrowserRouter([
{
path: paths.home.path,
lazy: async () => {
const { LandingRoute } = await import('./routes/landing');
return { Component: LandingRoute };
},
},
{
path: paths.auth.register.path,
lazy: async () => {
const { RegisterRoute } = await import('./routes/auth/register');
return { Component: RegisterRoute };
},
},
{
path: paths.auth.login.path,
lazy: async () => {
const { LoginRoute } = await import('./routes/auth/login');
return { Component: LoginRoute };
},
},
{
path: paths.app.root.path,
element: (
<ProtectedRoute>
<AppRoot />
</ProtectedRoute>
),
ErrorBoundary: AppRootErrorBoundary,
children: [
{
path: paths.app.discussions.path,
lazy: async () => {
const { DiscussionsRoute, discussionsLoader } = await import(
'./routes/app/discussions/discussions'
);
return {
Component: DiscussionsRoute,
loader: discussionsLoader(queryClient),
};
},
ErrorBoundary: AppRootErrorBoundary,
},
{
path: paths.app.discussion.path,
lazy: async () => {
const { DiscussionRoute, discussionLoader } = await import(
'./routes/app/discussions/discussion'
);
return {
Component: DiscussionRoute,
loader: discussionLoader(queryClient),
};
},
ErrorBoundary: AppRootErrorBoundary,
},
{
path: paths.app.users.path,
lazy: async () => {
const { UsersRoute, usersLoader } = await import(
'./routes/app/users'
);
return {
Component: UsersRoute,
loader: usersLoader(queryClient),
};
},
ErrorBoundary: AppRootErrorBoundary,
},
{
path: paths.app.profile.path,
lazy: async () => {
const { ProfileRoute } = await import('./routes/app/profile');
return {
Component: ProfileRoute,
};
},
ErrorBoundary: AppRootErrorBoundary,
},
{
path: paths.app.dashboard.path,
lazy: async () => {
const { DashboardRoute } = await import('./routes/app/dashboard');
return {
Component: DashboardRoute,
};
},
ErrorBoundary: AppRootErrorBoundary,
},
],
},
{
path: '*',
lazy: async () => {
const { NotFoundRoute } = await import('./routes/not-found');
return {
Component: NotFoundRoute,
};
},
ErrorBoundary: AppRootErrorBoundary,
},
]);
export const AppRouter = () => {
const queryClient = useQueryClient();
const router = useMemo(() => createAppRouter(queryClient), [queryClient]);
return <RouterProvider router={router} />;
};