|
@@ -1,12 +1,11 @@
|
|
|
import {getRoleMenu} from '@apis';
|
|
|
import {useContextSection} from '@hooks';
|
|
|
import {HOME_PATH} from '@routes';
|
|
|
-import {menuStore} from '@stores';
|
|
|
+import {menuStore, tabStore} from '@stores';
|
|
|
import {useQuery} from '@tanstack/react-query';
|
|
|
-import {MENU_COLLAPSED_STORAGE, ParseMenuType, sortMenu} from '@utils';
|
|
|
+import {MENU_COLLAPSED_STORAGE, sortMenu} from '@utils';
|
|
|
import {useBoolean, useLocalStorageState} from 'ahooks';
|
|
|
import {useEffect, useState} from 'react';
|
|
|
-import {useLocation, useNavigate} from 'react-router-dom';
|
|
|
import {useStore} from 'zustand';
|
|
|
import {context} from '../context';
|
|
|
|
|
@@ -47,78 +46,44 @@ export function useMenu() {
|
|
|
return data!;
|
|
|
}
|
|
|
|
|
|
-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;
|
|
|
-
|
|
|
- return [firstKey];
|
|
|
- });
|
|
|
- const [openKeys, setOpenKey] = useState<string[]>(current);
|
|
|
+export function useMenuState() {
|
|
|
+ const [openKeys, setOpenKey] = useState<string[]>(['-1']);
|
|
|
|
|
|
function onOpenChange(keys: string[]) {
|
|
|
setOpenKey([keys[keys.length - 1]]);
|
|
|
}
|
|
|
|
|
|
+ const pages = useStore(menuStore, state => state.menus);
|
|
|
+ const activeKey = useStore(tabStore, state => state.key);
|
|
|
+ const dispatch = useContextSection(context, state => state[1]);
|
|
|
function onClick(e: {key: string; keyPath: string[]}) {
|
|
|
setOpenKey(e.keyPath);
|
|
|
const data = pages.find(val => val.id === e.key);
|
|
|
if (!data) return;
|
|
|
|
|
|
- navigate(data.url);
|
|
|
+ dispatch({
|
|
|
+ type: 'ADD',
|
|
|
+ payload: {key: data.id, url: data.url, label: data.name},
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
+ const [selectKey, setSelectKey] = useState(['-1']);
|
|
|
useEffect(
|
|
|
function () {
|
|
|
- const route = pages.find(val => val.url === pathname);
|
|
|
- if (!route) return;
|
|
|
-
|
|
|
- if (route.id !== current[0]) setCurrent([route.id]);
|
|
|
- },
|
|
|
- [current, pages, pathname],
|
|
|
- );
|
|
|
+ const menu = pages.find(val => val.id === activeKey);
|
|
|
+ menu && menu.pId !== '0' ? setOpenKey([menu.pId]) : setOpenKey([]);
|
|
|
|
|
|
- useEffect(
|
|
|
- function () {
|
|
|
- pathname === HOME_PATH && setOpenKey([]);
|
|
|
+ setSelectKey([activeKey]);
|
|
|
},
|
|
|
- [pathname],
|
|
|
+ [activeKey, pages],
|
|
|
);
|
|
|
|
|
|
return [
|
|
|
- {openKeys, current},
|
|
|
+ {openKeys, selectKey},
|
|
|
{onOpenChange, onClick},
|
|
|
] as const;
|
|
|
}
|
|
|
|
|
|
-export function useMenuState() {
|
|
|
- const [openKeys, setOpenKey] = useState<string[]>(['-1']);
|
|
|
-
|
|
|
- function onOpenChange(keys: string[]) {
|
|
|
- setOpenKey([keys[keys.length - 1]]);
|
|
|
- }
|
|
|
-
|
|
|
- const pages = useStore(menuStore, state => state.menus);
|
|
|
- const dispatch = useContextSection(context, state => state[1]);
|
|
|
- function onClick(e: {key: string; keyPath: string[]}) {
|
|
|
- setOpenKey(e.keyPath);
|
|
|
- const data = pages.find(val => val.id === e.key);
|
|
|
- if (!data) return;
|
|
|
-
|
|
|
- dispatch({
|
|
|
- type: 'ADD',
|
|
|
- payload: {key: data.id, url: data.url, label: data.name},
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- return [{openKeys}, {onOpenChange, onClick}] as const;
|
|
|
-}
|
|
|
-
|
|
|
export function useCollapsedMenu() {
|
|
|
const [storage, setStorage] = useLocalStorageState<'0' | '1' | undefined>(
|
|
|
MENU_COLLAPSED_STORAGE,
|