memos/web/src/router/index.tsx
boojack 5d8b8c37a5
chore: upgrade vite (#728)
* chore: upgrade vite

* Revert "chore: remove lazy import component (#724)"

This reverts commit 688dc2304c.
2022-12-11 18:38:29 +08:00

100 lines
2.3 KiB
TypeScript

import { createBrowserRouter, redirect } from "react-router-dom";
import { lazy } from "react";
import { isNullorUndefined } from "../helpers/utils";
import { globalService, userService } from "../services";
const Auth = lazy(() => import("../pages/Auth"));
const Explore = lazy(() => import("../pages/Explore"));
const Home = lazy(() => import("../pages/Home"));
const MemoDetail = lazy(() => import("../pages/MemoDetail"));
const router = createBrowserRouter([
{
path: "/auth",
element: <Auth />,
loader: async () => {
try {
await globalService.initialState();
} catch (error) {
// do nth
}
return null;
},
},
{
path: "/",
element: <Home />,
loader: async () => {
try {
await globalService.initialState();
await userService.initialState();
} catch (error) {
// do nth
}
const { host, user } = userService.getState();
if (isNullorUndefined(host)) {
return redirect("/auth");
} else if (isNullorUndefined(user)) {
return redirect("/explore");
}
return null;
},
},
{
path: "/u/:userId",
element: <Home />,
loader: async () => {
try {
await globalService.initialState();
await userService.initialState();
} catch (error) {
// do nth
}
const { host } = userService.getState();
if (isNullorUndefined(host)) {
return redirect("/auth");
}
return null;
},
},
{
path: "/explore",
element: <Explore />,
loader: async () => {
try {
await globalService.initialState();
await userService.initialState();
} catch (error) {
// do nth
}
const { host } = userService.getState();
if (isNullorUndefined(host)) {
return redirect("/auth");
}
return null;
},
},
{
path: "/m/:memoId",
element: <MemoDetail />,
loader: async () => {
try {
await globalService.initialState();
await userService.initialState();
} catch (error) {
// do nth
}
const { host } = userService.getState();
if (isNullorUndefined(host)) {
return redirect("/auth");
}
return null;
},
},
]);
export default router;