侧边栏选中效果修复

This commit is contained in:
liamzi
2025-01-15 11:08:08 +08:00
parent 1eb2e0dd57
commit 3d7b8d705d
6 changed files with 31 additions and 14 deletions

View File

@@ -18,9 +18,7 @@ const Sidebar = ({ collapsed }) => {
if (!user?.id||user.menukeys?.length===0) return [];
return getMenuItems(user?.menukeys || []);
}, [user]);
useEffect(()=>{
console.log(menuClient,'menuClient');
},[menuClient])
const defaultOpenKeys = useMemo(() => {
const pathSegments = location.pathname.split("/").filter(Boolean);
return pathSegments.reduce((acc, _, index) => {
@@ -30,6 +28,10 @@ useEffect(()=>{
}, []);
}, [location.pathname]);
const selectedKey = useMemo(() => {
return location.pathname;
}, [location.pathname]);
const handleMenuClick = ({ key }) => {
navigate(key);
};
@@ -48,7 +50,7 @@ useEffect(()=>{
<Menu
theme={isDarkMode ? "dark" : "light"}
mode="inline"
selectedKeys={[location.pathname]}
selectedKeys={[selectedKey]}
defaultOpenKeys={defaultOpenKeys}
items={menuClient}
onClick={handleMenuClick}

View File

@@ -2,7 +2,7 @@ import React from "react";
import { Navigate, useLocation } from "react-router-dom";
import { useAuth } from "@/contexts/AuthContext";
const PUBLIC_PATHS = ['login', '404'];
const PUBLIC_PATHS = ['login', '404','home'];
export const ProtectedRoute = ({ children }) => {
const { user } = useAuth();
const location = useLocation();
@@ -15,13 +15,12 @@ export const ProtectedRoute = ({ children }) => {
return currentPath === key || currentPath.startsWith(`${key}/`);
});
if (!hasPermission ) {
return <Navigate to="/404" replace />;
if (!hasPermission) {
return <Navigate to="/home" replace />;
}
return children;
}
// 如果用户未登录,重定向到登录页
return <Navigate to="/login" state={{ from: location }} replace />;
};