소스 검색

fix: 修复退出后tab未清除问题

xyh 2 년 전
부모
커밋
b9fb87deb8
3개의 변경된 파일18개의 추가작업 그리고 12개의 파일을 삭제
  1. 5 0
      packages/app/src/pages/home/menu/hooks.tsx
  2. 6 2
      packages/app/src/stores/tab.ts
  3. 7 10
      packages/app/src/stores/user.ts

+ 5 - 0
packages/app/src/pages/home/menu/hooks.tsx

@@ -9,6 +9,7 @@ import {useStore} from 'zustand';
 
 export function useMenu() {
   const setMenus = useStore(menuStore, state => state.setMenu);
+  const dispatch = useStore(tabStore, state => state.dispatch);
 
   const {data} = useQuery({
     queryKey: [getRoleMenu.name],
@@ -31,6 +32,10 @@ export function useMenu() {
         ];
 
         setMenus(finalMenu);
+        dispatch({
+          type: 'ADD',
+          payload: {key: '-1', url: MAIN_PATH, label: '首页'},
+        });
         return sortMenu(finalMenu);
       }
 

+ 6 - 2
packages/app/src/stores/tab.ts

@@ -19,6 +19,7 @@ type DispatchOptions =
 type Action = {
   setActiveKey: (key: string | ((prev: string) => string)) => void;
   dispatch: (value: DispatchOptions) => void;
+  clear: () => void;
 };
 
 const defaultTab: State['tabList'][0] = {
@@ -83,8 +84,8 @@ function reducer(
 
 export const tabStore = createStore<State & Action>(function (set) {
   return {
-    activeKey: '-1',
-    tabList: [defaultTab],
+    activeKey: '',
+    tabList: [],
     setActiveKey(key) {
       set(function (prev) {
         return {activeKey: typeof key === 'string' ? key : key(prev.activeKey)};
@@ -130,5 +131,8 @@ export const tabStore = createStore<State & Action>(function (set) {
         return {tabList: result, activeKey: nextActiveKey};
       });
     },
+    clear() {
+      set({activeKey: '-1', tabList: []});
+    },
   };
 });

+ 7 - 10
packages/app/src/stores/user.ts

@@ -1,8 +1,8 @@
 import {UserLoginData} from '@models';
 import {QUERY_CLIENT, USER_TOKEN_STORAGE} from '@utils';
-import {startTransition} from 'react';
 import {createStore} from 'zustand/vanilla';
 import {menuStore} from './menu';
+import {tabStore} from './tab';
 
 type UserStoreState = UserLoginData;
 
@@ -41,15 +41,12 @@ export const userStore = createStore<UserStoreState & UserStoreAction>(
         // 清除用户缓存
         sessionStorage.removeItem(USER_TOKEN_STORAGE);
         set(defaultValue());
-
-        startTransition(function () {
-          // 清除请求缓存
-          QUERY_CLIENT.clear();
-
-          // 清除菜单缓存
-          const {clear} = menuStore.getState();
-          clear();
-        });
+        // 清除请求缓存
+        QUERY_CLIENT.clear();
+        // 清除菜单缓存
+        menuStore.getState().clear();
+        // 清除tab状态
+        tabStore.getState().clear();
       },
       setMenu(menus) {
         set({menu: menus.join(',') + ','});