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,23 +10,33 @@ import useResponsiveWidth from "@/hooks/useResponsiveWidth";
|
|||||||
import Loading from "@/pages/Loading";
|
import Loading from "@/pages/Loading";
|
||||||
import { Routes } from "@/router";
|
import { Routes } from "@/router";
|
||||||
|
|
||||||
const HomeLayout = () => {
|
const RootLayout = () => {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const { sm } = useResponsiveWidth();
|
const { sm } = useResponsiveWidth();
|
||||||
const currentUser = useCurrentUser();
|
const currentUser = useCurrentUser();
|
||||||
|
const [lastVisited] = useLocalStorage<string>("lastVisited", "/home");
|
||||||
const [collapsed, setCollapsed] = useLocalStorage<boolean>("navigation-collapsed", false);
|
const [collapsed, setCollapsed] = useLocalStorage<boolean>("navigation-collapsed", false);
|
||||||
const [initialized, setInitialized] = useState(false);
|
const [initialized, setInitialized] = useState(false);
|
||||||
|
|
||||||
// Redirect to explore page if not logged in.
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (
|
if (!currentUser) {
|
||||||
!currentUser &&
|
if (
|
||||||
([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,
|
location.pathname,
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
window.location.href = Routes.EXPLORE;
|
window.location.href = Routes.EXPLORE;
|
||||||
return;
|
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);
|
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 { createBrowserRouter } from "react-router-dom";
|
||||||
import App from "@/App";
|
import App from "@/App";
|
||||||
import HomeLayout from "@/layouts/HomeLayout";
|
import RootLayout from "@/layouts/RootLayout";
|
||||||
import SuspenseWrapper from "@/layouts/SuspenseWrapper";
|
import SuspenseWrapper from "@/layouts/SuspenseWrapper";
|
||||||
import About from "@/pages/About";
|
import About from "@/pages/About";
|
||||||
import Archived from "@/pages/Archived";
|
import Archived from "@/pages/Archived";
|
||||||
@ -12,7 +12,6 @@ import MemoDetail from "@/pages/MemoDetail";
|
|||||||
import NotFound from "@/pages/NotFound";
|
import NotFound from "@/pages/NotFound";
|
||||||
import PermissionDenied from "@/pages/PermissionDenied";
|
import PermissionDenied from "@/pages/PermissionDenied";
|
||||||
import Resources from "@/pages/Resources";
|
import Resources from "@/pages/Resources";
|
||||||
import RootRedirector from "@/pages/RootRedirector";
|
|
||||||
import Setting from "@/pages/Setting";
|
import Setting from "@/pages/Setting";
|
||||||
import SignIn from "@/pages/SignIn";
|
import SignIn from "@/pages/SignIn";
|
||||||
import SignUp from "@/pages/SignUp";
|
import SignUp from "@/pages/SignUp";
|
||||||
@ -56,13 +55,9 @@ const router = createBrowserRouter([
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/",
|
path: Routes.ROOT,
|
||||||
element: <HomeLayout />,
|
element: <RootLayout />,
|
||||||
children: [
|
children: [
|
||||||
{
|
|
||||||
path: Routes.ROOT,
|
|
||||||
element: <RootRedirector />,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
path: Routes.HOME,
|
path: Routes.HOME,
|
||||||
element: <Home />,
|
element: <Home />,
|
||||||
|
Reference in New Issue
Block a user