1
1
import { QueryClient , useQueryClient } from '@tanstack/react-query' ;
2
2
import { useMemo } from 'react' ;
3
- import { RouterProvider , createBrowserRouter } from 'react-router-dom' ;
3
+ import { createBrowserRouter } from 'react-router' ;
4
+ import { RouterProvider } from 'react-router/dom' ;
4
5
5
6
import { paths } from '@/config/paths' ;
6
7
import { ProtectedRoute } from '@/lib/auth' ;
7
8
8
- import { AppRoot , AppRootErrorBoundary } from './routes/app/root' ;
9
+ import {
10
+ default as AppRoot ,
11
+ ErrorBoundary as AppRootErrorBoundary ,
12
+ } from './routes/app/root' ;
13
+
14
+ const convert = ( queryClient : QueryClient ) => ( m : any ) => {
15
+ const { clientLoader, clientAction, default : Component , ...rest } = m ;
16
+ return {
17
+ ...rest ,
18
+ loader : clientLoader ?.( queryClient ) ,
19
+ action : clientAction ?.( queryClient ) ,
20
+ Component,
21
+ } ;
22
+ } ;
9
23
10
24
export const createAppRouter = ( queryClient : QueryClient ) =>
11
25
createBrowserRouter ( [
12
26
{
13
27
path : paths . home . path ,
14
- lazy : async ( ) => {
15
- const { LandingRoute } = await import ( './routes/landing' ) ;
16
- return { Component : LandingRoute } ;
17
- } ,
28
+ lazy : ( ) => import ( './routes/landing' ) . then ( convert ( queryClient ) ) ,
18
29
} ,
19
30
{
20
31
path : paths . auth . register . path ,
21
- lazy : async ( ) => {
22
- const { RegisterRoute } = await import ( './routes/auth/register' ) ;
23
- return { Component : RegisterRoute } ;
24
- } ,
32
+ lazy : ( ) => import ( './routes/auth/register' ) . then ( convert ( queryClient ) ) ,
25
33
} ,
26
34
{
27
35
path : paths . auth . login . path ,
28
- lazy : async ( ) => {
29
- const { LoginRoute } = await import ( './routes/auth/login' ) ;
30
- return { Component : LoginRoute } ;
31
- } ,
36
+ lazy : ( ) => import ( './routes/auth/login' ) . then ( convert ( queryClient ) ) ,
32
37
} ,
33
38
{
34
39
path : paths . app . root . path ,
@@ -41,74 +46,36 @@ export const createAppRouter = (queryClient: QueryClient) =>
41
46
children : [
42
47
{
43
48
path : paths . app . discussions . path ,
44
- lazy : async ( ) => {
45
- const { DiscussionsRoute, discussionsLoader } = await import (
46
- './routes/app/discussions/discussions'
47
- ) ;
48
- return {
49
- Component : DiscussionsRoute ,
50
- loader : discussionsLoader ( queryClient ) ,
51
- } ;
52
- } ,
53
- ErrorBoundary : AppRootErrorBoundary ,
49
+ lazy : ( ) =>
50
+ import ( './routes/app/discussions/discussions' ) . then (
51
+ convert ( queryClient ) ,
52
+ ) ,
54
53
} ,
55
54
{
56
55
path : paths . app . discussion . path ,
57
- lazy : async ( ) => {
58
- const { DiscussionRoute, discussionLoader } = await import (
59
- './routes/app/discussions/discussion'
60
- ) ;
61
- return {
62
- Component : DiscussionRoute ,
63
- loader : discussionLoader ( queryClient ) ,
64
- } ;
65
- } ,
66
- ErrorBoundary : AppRootErrorBoundary ,
56
+ lazy : ( ) =>
57
+ import ( './routes/app/discussions/discussion' ) . then (
58
+ convert ( queryClient ) ,
59
+ ) ,
67
60
} ,
68
61
{
69
62
path : paths . app . users . path ,
70
- lazy : async ( ) => {
71
- const { UsersRoute, usersLoader } = await import (
72
- './routes/app/users'
73
- ) ;
74
- return {
75
- Component : UsersRoute ,
76
- loader : usersLoader ( queryClient ) ,
77
- } ;
78
- } ,
79
- ErrorBoundary : AppRootErrorBoundary ,
63
+ lazy : ( ) => import ( './routes/app/users' ) . then ( convert ( queryClient ) ) ,
80
64
} ,
81
65
{
82
66
path : paths . app . profile . path ,
83
- lazy : async ( ) => {
84
- const { ProfileRoute } = await import ( './routes/app/profile' ) ;
85
- return {
86
- Component : ProfileRoute ,
87
- } ;
88
- } ,
89
- ErrorBoundary : AppRootErrorBoundary ,
67
+ lazy : ( ) => import ( './routes/app/profile' ) . then ( convert ( queryClient ) ) ,
90
68
} ,
91
69
{
92
70
path : paths . app . dashboard . path ,
93
- lazy : async ( ) => {
94
- const { DashboardRoute } = await import ( './routes/app/dashboard' ) ;
95
- return {
96
- Component : DashboardRoute ,
97
- } ;
98
- } ,
99
- ErrorBoundary : AppRootErrorBoundary ,
71
+ lazy : ( ) =>
72
+ import ( './routes/app/dashboard' ) . then ( convert ( queryClient ) ) ,
100
73
} ,
101
74
] ,
102
75
} ,
103
76
{
104
77
path : '*' ,
105
- lazy : async ( ) => {
106
- const { NotFoundRoute } = await import ( './routes/not-found' ) ;
107
- return {
108
- Component : NotFoundRoute ,
109
- } ;
110
- } ,
111
- ErrorBoundary : AppRootErrorBoundary ,
78
+ lazy : ( ) => import ( './routes/not-found' ) . then ( convert ( queryClient ) ) ,
112
79
} ,
113
80
] ) ;
114
81
0 commit comments