优化菜单标签页逻辑

dev-tmp1
tangjie 2026-08-10 16:18:22 +08:00
parent a5b3a2ae18
commit 8b74f1691c
1 changed files with 23 additions and 9 deletions

View File

@ -26,12 +26,26 @@ const { Text } = Typography;
const TABS_KEY = "sim_layout_tabs"; const TABS_KEY = "sim_layout_tabs";
const ACTIVE_KEY = "sim_layout_active"; const ACTIVE_KEY = "sim_layout_active";
/* ---- 路径规范化:统一补全 /safetyEval 前缀,避免同一路由生成多个标签 ---- */
const normalizePath = (path) => {
if (!path) return path;
return path.startsWith("/safetyEval") ? path : `/safetyEval${path}`;
};
/* ---- sessionStorage 持久化 ---- */ /* ---- sessionStorage 持久化 ---- */
function loadState() { function loadState() {
try { try {
const tabs = JSON.parse(sessionStorage.getItem(TABS_KEY) || "[]"); const rawTabs = JSON.parse(sessionStorage.getItem(TABS_KEY) || "[]");
const activeKey = sessionStorage.getItem(ACTIVE_KEY) || ""; const tabs = rawTabs.map((t) => ({ ...t, key: normalizePath(t.key) }));
return { tabs, activeKey }; //
const seen = new Set();
const deduped = tabs.filter((t) => {
if (seen.has(t.key)) return false;
seen.add(t.key);
return true;
});
const activeKey = normalizePath(sessionStorage.getItem(ACTIVE_KEY) || "");
return { tabs: deduped, activeKey };
} catch { } catch {
return { tabs: [], activeKey: "" }; return { tabs: [], activeKey: "" };
} }
@ -45,12 +59,12 @@ function saveState(tabs, activeKey) {
export default function SimulatedLayout({ children, history }) { export default function SimulatedLayout({ children, history }) {
const [collapsed, setCollapsed] = useState(false); const [collapsed, setCollapsed] = useState(false);
const [state, setState] = useState(loadState); const [state, setState] = useState(loadState);
const [currentPath, setCurrentPath] = useState(window.location.pathname); const [currentPath, setCurrentPath] = useState(() => normalizePath(window.location.pathname));
// currentPath // currentPath
useEffect(() => { useEffect(() => {
const unlisten = history?.listen?.((location) => { const unlisten = history?.listen?.((location) => {
setCurrentPath(location.pathname || window.location.pathname); setCurrentPath(normalizePath(location.pathname || window.location.pathname));
}); });
return () => unlisten?.(); return () => unlisten?.();
}, [history]); }, [history]);
@ -80,10 +94,10 @@ export default function SimulatedLayout({ children, history }) {
}, []); }, []);
const navigateTo = useCallback((path) => { const navigateTo = useCallback((path) => {
if (path !== currentPath) { const normalized = normalizePath(path);
history.replace(path.replace(/^\/safetyEval/, "")); if (normalized !== currentPath) {
// history.listen currentPath history.replace(normalized.replace(/^\/safetyEval/, ""));
setCurrentPath(window.location.pathname); setCurrentPath(normalized);
} }
}, [currentPath, history]); }, [currentPath, history]);