|
@@ -1,12 +1,14 @@
|
|
|
import {menuStore} from '@stores';
|
|
|
import {ParseMenuType} from '@utils';
|
|
|
-import {useState} from 'react';
|
|
|
-import {useNavigate} from 'react-router-dom';
|
|
|
+import {useEffect, useState} from 'react';
|
|
|
+import {useLocation, useNavigate} from 'react-router-dom';
|
|
|
import {useStore} from 'zustand';
|
|
|
|
|
|
export function useOpenKey(menus: ParseMenuType[]) {
|
|
|
const navigate = useNavigate();
|
|
|
+ const {pathname} = useLocation();
|
|
|
const pages = useStore(menuStore, state => state.menus);
|
|
|
+
|
|
|
const [current, setCurrent] = useState<string[]>(function() {
|
|
|
const firstKey = menus[0]?.children ? menus[0].children[0].key : menus[0].key;
|
|
|
|
|
@@ -20,10 +22,16 @@ export function useOpenKey(menus: ParseMenuType[]) {
|
|
|
|
|
|
function onClick(e: {key: string, keyPath: string[]}) {
|
|
|
setOpenKey(e.keyPath);
|
|
|
- setCurrent([e.key]);
|
|
|
const data = pages.find(val => val.id === e.key)!;
|
|
|
navigate(data.url);
|
|
|
}
|
|
|
|
|
|
+ useEffect(function() {
|
|
|
+ const route = pages.find(val => val.url === pathname);
|
|
|
+ if (!route) return;
|
|
|
+
|
|
|
+ if (route.id !== current[0]) setCurrent([route.id]);
|
|
|
+ }, [current, pages, pathname]);
|
|
|
+
|
|
|
return [{openKeys, current}, {onOpenChange, onClick}] as const;
|
|
|
}
|