|
| 1 | +import { createBrowserRouter, redirect } from 'react-router-dom'; |
| 2 | +import { queryClient } from '@/lib/query-client'; |
| 3 | +import i18n from './i18n'; |
| 4 | +import { |
| 5 | + Channels, |
| 6 | + ForgotPassword, |
| 7 | + Login, |
| 8 | + Mail, |
| 9 | + Profiles, |
| 10 | + Register, |
| 11 | + SetPassword, |
| 12 | +} from '@/pages'; |
| 13 | +import { useAppLayoutStore, useSettingStore, useUserStore } from '@/store'; |
| 14 | +import { |
| 15 | + currentUserQueryOptions, |
| 16 | + settingQueryOption, |
| 17 | +} from '@/lib/query-options'; |
| 18 | +import { AppLayout, AuthLayout, SettingLayout } from '@/components/layouts'; |
| 19 | +import { TChannelQuery } from './types/channel'; |
| 20 | +import { queryStringToObject } from './utils'; |
| 21 | +import { queryChannelsOption } from './lib/query-options/channel'; |
| 22 | + |
| 23 | +export const router = createBrowserRouter([ |
| 24 | + { |
| 25 | + children: [ |
| 26 | + { |
| 27 | + loader: async ({ request }) => { |
| 28 | + const redirectUrl = new URL(request.url).searchParams.get( |
| 29 | + 'redirect' |
| 30 | + ); |
| 31 | + const user = await queryClient.ensureQueryData( |
| 32 | + currentUserQueryOptions() |
| 33 | + ); |
| 34 | + |
| 35 | + if (user) { |
| 36 | + return redirect(redirectUrl || '/chatbots'); |
| 37 | + } |
| 38 | + |
| 39 | + return null; |
| 40 | + }, |
| 41 | + Component: AuthLayout, |
| 42 | + children: [ |
| 43 | + { |
| 44 | + path: '/login', |
| 45 | + Component: Login, |
| 46 | + index: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + path: '/register', |
| 50 | + Component: Register, |
| 51 | + }, |
| 52 | + { |
| 53 | + path: '/forgot-password', |
| 54 | + Component: ForgotPassword, |
| 55 | + }, |
| 56 | + { |
| 57 | + path: '/set-password', |
| 58 | + Component: SetPassword, |
| 59 | + }, |
| 60 | + ], |
| 61 | + }, |
| 62 | + { |
| 63 | + Component: AppLayout, |
| 64 | + loader: async () => { |
| 65 | + const user = await queryClient.ensureQueryData( |
| 66 | + currentUserQueryOptions() |
| 67 | + ); |
| 68 | + |
| 69 | + const url = new URLSearchParams({ |
| 70 | + redirect: location.href, |
| 71 | + }); |
| 72 | + |
| 73 | + if (!user) { |
| 74 | + return redirect(`/login?${url.toString()}`); |
| 75 | + } |
| 76 | + |
| 77 | + useUserStore.getState().setUser(user); |
| 78 | + |
| 79 | + return null; |
| 80 | + }, |
| 81 | + children: [ |
| 82 | + { |
| 83 | + path: '/dashboard', |
| 84 | + index: true, |
| 85 | + loader: async () => { |
| 86 | + return redirect('/chatbots'); |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + path: '/chatbots', |
| 91 | + element: <div>a</div>, |
| 92 | + loader: () => { |
| 93 | + useAppLayoutStore |
| 94 | + .getState() |
| 95 | + .setTitle(i18n.t('common:chatbots')); |
| 96 | + |
| 97 | + return null; |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + path: '/channels', |
| 102 | + Component: Channels, |
| 103 | + loader: async ({ request }) => { |
| 104 | + const query: TChannelQuery = queryStringToObject( |
| 105 | + request.url |
| 106 | + ); |
| 107 | + |
| 108 | + await queryClient.ensureQueryData( |
| 109 | + queryChannelsOption(query) |
| 110 | + ); |
| 111 | + |
| 112 | + useAppLayoutStore |
| 113 | + .getState() |
| 114 | + .setTitle(i18n.t('common:channels')); |
| 115 | + |
| 116 | + return null; |
| 117 | + }, |
| 118 | + }, |
| 119 | + { |
| 120 | + path: '/settings', |
| 121 | + Component: SettingLayout, |
| 122 | + loader: async () => { |
| 123 | + const data = |
| 124 | + await queryClient.ensureQueryData(settingQueryOption()); |
| 125 | + |
| 126 | + useSettingStore.getState().setSetting(data); |
| 127 | + useAppLayoutStore |
| 128 | + .getState() |
| 129 | + .setTitle(i18n.t('common:settings')); |
| 130 | + |
| 131 | + return null; |
| 132 | + }, |
| 133 | + children: [ |
| 134 | + { |
| 135 | + path: 'mail', |
| 136 | + Component: Mail, |
| 137 | + }, |
| 138 | + { |
| 139 | + path: 'profiles', |
| 140 | + Component: Profiles, |
| 141 | + }, |
| 142 | + { |
| 143 | + index: true, |
| 144 | + loader: async () => { |
| 145 | + return redirect('/settings/profiles'); |
| 146 | + }, |
| 147 | + }, |
| 148 | + ], |
| 149 | + }, |
| 150 | + ], |
| 151 | + }, |
| 152 | + { |
| 153 | + path: '/', |
| 154 | + element: <div>Hi</div>, |
| 155 | + }, |
| 156 | + ], |
| 157 | + }, |
| 158 | +]); |
0 commit comments