You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
memos/web/src/router/index.tsx

245 lines
5.6 KiB
TypeScript

import { lazy } from "react";
import { createBrowserRouter, redirect } from "react-router-dom";
import { isNullorUndefined } from "@/helpers/utils";
import Archived from "@/pages/Archived";
import DailyReview from "@/pages/DailyReview";
import ResourcesDashboard from "@/pages/ResourcesDashboard";
import Setting from "@/pages/Setting";
import store from "@/store";
import { initialGlobalState, initialUserState } from "@/store/module";
const Root = lazy(() => import("@/layouts/Root"));
const Auth = lazy(() => import("@/pages/Auth"));
const AuthCallback = lazy(() => import("@/pages/AuthCallback"));
const Explore = lazy(() => import("@/pages/Explore"));
const Home = lazy(() => import("@/pages/Home"));
const UserProfile = lazy(() => import("@/pages/UserProfile"));
const MemoDetail = lazy(() => import("@/pages/MemoDetail"));
const EmbedMemo = lazy(() => import("@/pages/EmbedMemo"));
const NotFound = lazy(() => import("@/pages/NotFound"));
const initialGlobalStateLoader = (() => {
let done = false;
return async () => {
if (done) {
return;
}
done = true;
try {
await initialGlobalState();
} catch (error) {
// do nth
}
};
})();
const router = createBrowserRouter([
{
path: "/auth",
element: <Auth />,
loader: async () => {
await initialGlobalStateLoader();
return null;
},
},
{
path: "/auth/callback",
element: <AuthCallback />,
},
{
path: "/",
element: <Root />,
children: [
{
path: "",
element: <Home />,
loader: async () => {
await initialGlobalStateLoader();
try {
await initialUserState();
} catch (error) {
// do nth
}
const { user } = store.getState().user;
const { systemStatus } = store.getState().global;
// if user is authenticated, then show home
if (!isNullorUndefined(user)) {
return null;
}
// if user is anonymous, then redirect to auth if disabled public memos, else redirect to explore
if (systemStatus.disablePublicMemos) {
return redirect("/auth");
}
return redirect("/explore");
},
},
{
path: "explore",
element: <Explore />,
loader: async () => {
await initialGlobalStateLoader();
try {
await initialUserState();
} catch (error) {
// do nth
}
const { user } = store.getState().user;
const { systemStatus } = store.getState().global;
if (isNullorUndefined(user) && systemStatus.disablePublicMemos) {
return redirect("/auth");
}
return null;
},
},
{
path: "review",
element: <DailyReview />,
loader: async () => {
await initialGlobalStateLoader();
feat: new resource dashboard (#1346) * feat: refator the file dashboard * feat: support select resouce file * feat: suppor delete select files * feat: support share menu, implement rename and delete * chore: change the color of hover * chore: refator file dashboard to page * feat: add i18n for button * feat: beautify the button * fix: the error position of button * feat: only select when click circle instead of whole card * feat: beautify file dashboard * chore: factor the filecard code * feat: using dropdown component intead of component * feat: add i18n for delete selected resource button * feat: delete the unused style of title * chore: refactor file cover * feat: support more type file cover * feat: use memo to reduce unused computing in filecover * feat: when no file be selected, click the delete will error * feat: store the select resource id instead of source to save memory * chore: delete unused code * feat: refactor the file card * chore: delete unused style file * chore: change file to resource * chore: delete unused import * chore: fix the typo * fix: the error of handle check click * fix: the error of handle of uncheck * chore: change the name of selectList to selectedList * chore: change the name of selectList to selectedList * chore: change the name of selectList to selectedList * chore: delete unused import * feat: support Responsive Design * feat: min display two card in a line * feat: adjust the num of a line in responsive design * feat: adjust the num of a line to 6 when using md * feat: add the color of hover source card when dark * chore: refactor resource cover css to reduce code * chore: delete unnessnary change * chore: change the type of callback function * chore: delete unused css code * feat: add zh-hant i18n * feat: change the position of buttons * feat: add title for the icon button * feat: add opacity for icon * feat: refactor searchbar * feat:move Debounce to search * feat: new resource search bar * feat: reduce the size of cover * support file search * Update web/src/pages/ResourcesDashboard.tsx Co-authored-by: boojack <stevenlgtm@gmail.com> * Update web/src/components/ResourceCard.tsx Co-authored-by: boojack <stevenlgtm@gmail.com> * chore: reduce css code * feat: support lowcase and uppercase search * chore: reserve the searchbar * feat: refator resource Search bar * chore: change the param name * feat: resource bar support dark mode * feat: beautify the UI of dashboard * chore: extract positionClassName from actionsClassName * feat: reduce the length of search bar --------- Co-authored-by: boojack <stevenlgtm@gmail.com>
2 years ago
try {
await initialUserState();
} catch (error) {
// do nth
}
const { user } = store.getState().user;
if (isNullorUndefined(user)) {
feat: new resource dashboard (#1346) * feat: refator the file dashboard * feat: support select resouce file * feat: suppor delete select files * feat: support share menu, implement rename and delete * chore: change the color of hover * chore: refator file dashboard to page * feat: add i18n for button * feat: beautify the button * fix: the error position of button * feat: only select when click circle instead of whole card * feat: beautify file dashboard * chore: factor the filecard code * feat: using dropdown component intead of component * feat: add i18n for delete selected resource button * feat: delete the unused style of title * chore: refactor file cover * feat: support more type file cover * feat: use memo to reduce unused computing in filecover * feat: when no file be selected, click the delete will error * feat: store the select resource id instead of source to save memory * chore: delete unused code * feat: refactor the file card * chore: delete unused style file * chore: change file to resource * chore: delete unused import * chore: fix the typo * fix: the error of handle check click * fix: the error of handle of uncheck * chore: change the name of selectList to selectedList * chore: change the name of selectList to selectedList * chore: change the name of selectList to selectedList * chore: delete unused import * feat: support Responsive Design * feat: min display two card in a line * feat: adjust the num of a line in responsive design * feat: adjust the num of a line to 6 when using md * feat: add the color of hover source card when dark * chore: refactor resource cover css to reduce code * chore: delete unnessnary change * chore: change the type of callback function * chore: delete unused css code * feat: add zh-hant i18n * feat: change the position of buttons * feat: add title for the icon button * feat: add opacity for icon * feat: refactor searchbar * feat:move Debounce to search * feat: new resource search bar * feat: reduce the size of cover * support file search * Update web/src/pages/ResourcesDashboard.tsx Co-authored-by: boojack <stevenlgtm@gmail.com> * Update web/src/components/ResourceCard.tsx Co-authored-by: boojack <stevenlgtm@gmail.com> * chore: reduce css code * feat: support lowcase and uppercase search * chore: reserve the searchbar * feat: refator resource Search bar * chore: change the param name * feat: resource bar support dark mode * feat: beautify the UI of dashboard * chore: extract positionClassName from actionsClassName * feat: reduce the length of search bar --------- Co-authored-by: boojack <stevenlgtm@gmail.com>
2 years ago
return redirect("/auth");
}
return null;
},
},
{
path: "resources",
element: <ResourcesDashboard />,
loader: async () => {
await initialGlobalStateLoader();
try {
await initialUserState();
} catch (error) {
// do nth
}
const { user } = store.getState().user;
if (isNullorUndefined(user)) {
return redirect("/auth");
}
return null;
},
},
{
path: "archived",
element: <Archived />,
loader: async () => {
await initialGlobalStateLoader();
try {
await initialUserState();
} catch (error) {
// do nth
}
const { user } = store.getState().user;
if (isNullorUndefined(user)) {
return redirect("/auth");
}
return null;
},
},
{
path: "setting",
element: <Setting />,
loader: async () => {
await initialGlobalStateLoader();
try {
await initialUserState();
} catch (error) {
// do nth
}
const { user } = store.getState().user;
if (isNullorUndefined(user)) {
return redirect("/auth");
}
return null;
},
},
],
},
{
path: "/m/:memoId",
element: <MemoDetail />,
loader: async () => {
await initialGlobalStateLoader();
try {
await initialUserState();
} catch (error) {
// do nth
}
const { user } = store.getState().user;
const { systemStatus } = store.getState().global;
if (isNullorUndefined(user) && systemStatus.disablePublicMemos) {
return redirect("/auth");
}
return null;
},
},
{
path: "/m/:memoId/embed",
element: <EmbedMemo />,
loader: async () => {
await initialGlobalStateLoader();
try {
await initialUserState();
} catch (error) {
// do nth
}
return null;
},
},
{
path: "/u/:username",
element: <UserProfile />,
loader: async () => {
await initialGlobalStateLoader();
try {
await initialUserState();
} catch (error) {
// do nth
}
return null;
},
},
{
path: "*",
element: <NotFound />,
loader: async () => {
await initialGlobalStateLoader();
return null;
},
},
]);
export default router;