import { useMatchRoute } from '../use-match-route'; // import { router } from '@/router'; import { useSessionStorageState } from 'ahooks'; import { omit } from 'lodash'; import React, { useCallback, useEffect, useState, useRef } from 'react'; import { useNavigate } from "react-router-dom"; export interface KeepAliveTab { title: string; routePath: string; key: string; pathname: string; // searchParmStr:string; icon?: string; children: React.ReactNode; } function getKey() { return new Date().getTime().toString(); } export function useTabs() { // 存放页面记录 const [keepAliveTabs, setKeepAliveTabs] = useSessionStorageState('keepAliveTabs', { defaultValue: [], serializer: value => { // 把 children 剔除掉,不然会报错 return JSON.stringify(value.map(item => omit(item, ['children']))) }, }); // 当前激活的tab const [activeTabRoutePath, setActiveTabRoutePath] = useState(''); const tabsRef = useRef(keepAliveTabs || []); const matchRoute = useMatchRoute(); let navigate = useNavigate(); useEffect(() => { tabsRef.current = keepAliveTabs || []; }, [keepAliveTabs]); // 关闭tab const closeTab = useCallback( (routePath: string = activeTabRoutePath || '') => { const currentTabs = tabsRef.current; if (!currentTabs?.length) { return; } const index = (currentTabs || []).findIndex(o => o.routePath === routePath); if (index === -1) { return; } if (currentTabs[index].routePath === activeTabRoutePath && currentTabs.length > 1) { if (index > 0) { navigate(currentTabs[index - 1].pathname); } else { navigate(currentTabs[index + 1].pathname); } } currentTabs.splice(index, 1); setKeepAliveTabs([...currentTabs]); }, [activeTabRoutePath], ); // 关闭除了自己其它tab const closeOtherTab = useCallback((routePath: string = activeTabRoutePath || '') => { if (!keepAliveTabs?.length) { return; } const tab = keepAliveTabs.find(o => o.routePath === routePath); setKeepAliveTabs(keepAliveTabs.filter(o => o.routePath === routePath)); //router.navigate(tab?.pathname || routePath); }, [activeTabRoutePath]); // 刷新tab const refreshTab = useCallback((routePath: string = activeTabRoutePath || '') => { // setKeepAliveTabs(prev => { // const index = (prev || []).findIndex(tab => tab.pathname === routePath); // // if (index >= 0 && prev) { // // 这个是react的特性,key变了,组件会卸载重新渲染 // prev[index].key = getKey(); // } // return [...prev || []]; // }); }, [activeTabRoutePath]); useEffect(() => { if (!matchRoute) return; const existKeepAliveTab = (keepAliveTabs || []).find(o => o.routePath === matchRoute?.routePath); // 如果不存在则需要插入 if (!existKeepAliveTab) { setKeepAliveTabs(prev => ([...(prev || []), { title: matchRoute.title, key: matchRoute.routePath, routePath: matchRoute.routePath, pathname: matchRoute.pathname, children: matchRoute.children, icon: matchRoute.icon // searchParmStr:matchRoute.searchParmStr||"" }])); } else if (existKeepAliveTab.pathname !== matchRoute.pathname) { // 如果是同一个路由,但是参数不同,我们只需要刷新当前页签并且把pathname设置为新的pathname, children设置为新的children setKeepAliveTabs(prev => { const index = (prev || []).findIndex(tab => tab.routePath === matchRoute.routePath); if (index >= 0 && prev) { prev[index].key = matchRoute.routePath; prev[index].pathname = matchRoute.pathname; prev[index].children = matchRoute.children; // prev[index].searchParmStr=matchRoute.searchParmStr ||"" } return [...(prev || [])]; }); } else if (!existKeepAliveTab.children) { // 如果pathname相同,但是children为空,说明重缓存中加载的数据,我们只需要刷新当前页签并且把children设置为新的children setKeepAliveTabs(prev => { const index = (prev || []).findIndex(tab => tab.routePath === matchRoute.routePath); if (index >= 0 && prev) { prev[index].key = matchRoute.routePath; prev[index].children = matchRoute.children; } return [...(prev || [])]; }); } setActiveTabRoutePath(matchRoute.routePath); }, [matchRoute]) return { tabs: keepAliveTabs, activeTabRoutePath, closeTab, closeOtherTab, refreshTab, setTabs: setKeepAliveTabs, } }