fix: wrong remembered nav while switch

This commit is contained in:
Tristan Yang
2023-03-09 20:38:45 +08:00
parent ae974e1319
commit 8000c83a60
+11 -1
View File
@@ -1,6 +1,7 @@
import { memo } from "react"; import { memo, useEffect } from "react";
import { Outlet, NavLink, useLocation, useMatch } from "react-router-dom"; import { Outlet, NavLink, useLocation, useMatch } from "react-router-dom";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useDispatch } from "react-redux";
import User from "./User"; import User from "./User";
import Loading from "../../common/component/Loading"; import Loading from "../../common/component/Loading";
@@ -15,10 +16,12 @@ import FavIcon from "../../assets/icons/bookmark.svg";
import FolderIcon from "../../assets/icons/folder.svg"; import FolderIcon from "../../assets/icons/folder.svg";
import { useAppSelector } from "../../app/store"; import { useAppSelector } from "../../app/store";
import MobileNavs from "./MobileNavs"; import MobileNavs from "./MobileNavs";
import { updateRememberedNavs } from "../../app/slices/ui";
function HomePage() { function HomePage() {
const { t } = useTranslation(); const { t } = useTranslation();
const dispatch = useDispatch();
const isHomePath = useMatch(`/`); const isHomePath = useMatch(`/`);
const isChatHomePath = useMatch(`/chat`); const isChatHomePath = useMatch(`/chat`);
const { pathname } = useLocation(); const { pathname } = useLocation();
@@ -37,6 +40,12 @@ function HomePage() {
}; };
}); });
const { success } = usePreload(); const { success } = usePreload();
useEffect(() => {
if (isChatHomePath) {
dispatch(updateRememberedNavs({ key: "chat", path: "/chat" }));
}
}, [isChatHomePath]);
if (!success || !ready) { if (!success || !ready) {
return <Loading reload={true} fullscreen={true} />; return <Loading reload={true} fullscreen={true} />;
} }
@@ -47,6 +56,7 @@ function HomePage() {
} }
// 有点绕 // 有点绕
const chatNav = isChatHomePath ? "/chat" : chatPath || "/chat"; const chatNav = isChatHomePath ? "/chat" : chatPath || "/chat";
console.log("navvvv", isChatHomePath, chatPath);
const userNav = userPath || "/users"; const userNav = userPath || "/users";
const linkClass = `flex items-center gap-2.5 px-3 py-2 font-semibold text-sm text-gray-600 rounded-lg md:hover:bg-gray-800/10`; const linkClass = `flex items-center gap-2.5 px-3 py-2 font-semibold text-sm text-gray-600 rounded-lg md:hover:bg-gray-800/10`;
return ( return (