mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: tweak root redirector
This commit is contained in:
@ -10,24 +10,34 @@ import useResponsiveWidth from "@/hooks/useResponsiveWidth";
|
||||
import Loading from "@/pages/Loading";
|
||||
import { Routes } from "@/router";
|
||||
|
||||
const HomeLayout = () => {
|
||||
const RootLayout = () => {
|
||||
const location = useLocation();
|
||||
const { sm } = useResponsiveWidth();
|
||||
const currentUser = useCurrentUser();
|
||||
const [lastVisited] = useLocalStorage<string>("lastVisited", "/home");
|
||||
const [collapsed, setCollapsed] = useLocalStorage<boolean>("navigation-collapsed", false);
|
||||
const [initialized, setInitialized] = useState(false);
|
||||
|
||||
// Redirect to explore page if not logged in.
|
||||
useEffect(() => {
|
||||
if (!currentUser) {
|
||||
if (
|
||||
!currentUser &&
|
||||
([Routes.HOME, Routes.TIMELINE, Routes.RESOURCES, Routes.INBOX, Routes.ARCHIVED, Routes.SETTING] as string[]).includes(
|
||||
([Routes.ROOT, Routes.HOME, Routes.TIMELINE, Routes.RESOURCES, Routes.INBOX, Routes.ARCHIVED, Routes.SETTING] as string[]).includes(
|
||||
location.pathname,
|
||||
)
|
||||
) {
|
||||
window.location.href = Routes.EXPLORE;
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (location.pathname === Routes.ROOT) {
|
||||
if (lastVisited && ([Routes.HOME, Routes.TIMELINE] as string[]).includes(lastVisited)) {
|
||||
window.location.href = lastVisited;
|
||||
} else {
|
||||
window.location.href = Routes.HOME;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setInitialized(true);
|
||||
}, []);
|
||||
@ -75,4 +85,4 @@ const HomeLayout = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default HomeLayout;
|
||||
export default RootLayout;
|
@ -1,20 +0,0 @@
|
||||
import { useEffect } from "react";
|
||||
import useLocalStorage from "react-use/lib/useLocalStorage";
|
||||
import useNavigateTo from "@/hooks/useNavigateTo";
|
||||
|
||||
const RootRedirector: React.FC = () => {
|
||||
const [lastVisited] = useLocalStorage<string>("lastVisited", "/home");
|
||||
const navigateTo = useNavigateTo();
|
||||
|
||||
useEffect(() => {
|
||||
if (lastVisited === "/home" || lastVisited === "/timeline") {
|
||||
navigateTo(lastVisited);
|
||||
} else {
|
||||
navigateTo("/home");
|
||||
}
|
||||
}, []);
|
||||
|
||||
return <></>;
|
||||
};
|
||||
|
||||
export default RootRedirector;
|
@ -1,6 +1,6 @@
|
||||
import { createBrowserRouter } from "react-router-dom";
|
||||
import App from "@/App";
|
||||
import HomeLayout from "@/layouts/HomeLayout";
|
||||
import RootLayout from "@/layouts/RootLayout";
|
||||
import SuspenseWrapper from "@/layouts/SuspenseWrapper";
|
||||
import About from "@/pages/About";
|
||||
import Archived from "@/pages/Archived";
|
||||
@ -12,7 +12,6 @@ import MemoDetail from "@/pages/MemoDetail";
|
||||
import NotFound from "@/pages/NotFound";
|
||||
import PermissionDenied from "@/pages/PermissionDenied";
|
||||
import Resources from "@/pages/Resources";
|
||||
import RootRedirector from "@/pages/RootRedirector";
|
||||
import Setting from "@/pages/Setting";
|
||||
import SignIn from "@/pages/SignIn";
|
||||
import SignUp from "@/pages/SignUp";
|
||||
@ -55,14 +54,10 @@ const router = createBrowserRouter([
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: "/",
|
||||
element: <HomeLayout />,
|
||||
children: [
|
||||
{
|
||||
path: Routes.ROOT,
|
||||
element: <RootRedirector />,
|
||||
},
|
||||
element: <RootLayout />,
|
||||
children: [
|
||||
{
|
||||
path: Routes.HOME,
|
||||
element: <Home />,
|
||||
|
Reference in New Issue
Block a user