Skip to content

Commit

Permalink
refactor(projects): useMixMenuContext replace useMixMenu
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Apr 25, 2024
1 parent 19e65c1 commit 1e14293
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 51 deletions.
45 changes: 44 additions & 1 deletion src/layouts/context/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
import { computed, ref, watch } from 'vue';
import { useRoute } from 'vue-router';
import { useContext } from '@sa/hooks';
import { useMixMenu } from '../hooks';
import { useRouteStore } from '@/store/modules/route';

export const { setupStore: setupMixMenuContext, useStore: useMixMenuContext } = useContext('mix-menu', useMixMenu);

function useMixMenu() {
const route = useRoute();
const routeStore = useRouteStore();

const activeFirstLevelMenuKey = ref('');

function setActiveFirstLevelMenuKey(key: string) {
activeFirstLevelMenuKey.value = key;
}

function getActiveFirstLevelMenuKey() {
const { hideInMenu, activeMenu } = route.meta;
const name = route.name as string;

const routeName = (hideInMenu ? activeMenu : name) || name;

const [firstLevelRouteName] = routeName.split('_');

setActiveFirstLevelMenuKey(firstLevelRouteName);
}

const menus = computed(
() => routeStore.menus.find(menu => menu.key === activeFirstLevelMenuKey.value)?.children || []
);

watch(
() => route.name,
() => {
getActiveFirstLevelMenuKey();
},
{ immediate: true }
);

return {
activeFirstLevelMenuKey,
setActiveFirstLevelMenuKey,
getActiveFirstLevelMenuKey,
menus
};
}
44 changes: 0 additions & 44 deletions src/layouts/hooks/index.ts

This file was deleted.

8 changes: 2 additions & 6 deletions src/layouts/modules/global-menu/vertical-mix-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import { computed } from 'vue';
import { useBoolean } from '@sa/hooks';
import { useAppStore } from '@/store/modules/app';
import { useRouteStore } from '@/store/modules/route';
import { useThemeStore } from '@/store/modules/theme';
import { useRouterPush } from '@/hooks/common/router';
import { $t } from '@/locales';
import { useMixMenu } from '../../hooks';
import { useMixMenuContext } from '../../context';
import FirstLevelMenu from './first-level-menu.vue';
import BaseMenu from './base-menu.vue';
Expand All @@ -16,15 +15,12 @@ defineOptions({
const appStore = useAppStore();
const themeStore = useThemeStore();
const routeStore = useRouteStore();
const { routerPushByKey } = useRouterPush();
const { bool: drawerVisible, setBool: setDrawerVisible } = useBoolean();
const { activeFirstLevelMenuKey, setActiveFirstLevelMenuKey, getActiveFirstLevelMenuKey } = useMixMenu();
const { menus, activeFirstLevelMenuKey, setActiveFirstLevelMenuKey, getActiveFirstLevelMenuKey } = useMixMenuContext();
const siderInverted = computed(() => !themeStore.darkMode && themeStore.sider.inverted);
const menus = computed(() => routeStore.menus.find(menu => menu.key === activeFirstLevelMenuKey.value)?.children || []);
const showDrawer = computed(() => (drawerVisible.value && menus.value.length) || appStore.mixSiderFixed);
function handleSelectMixMenu(menu: App.Global.Menu) {
Expand Down

0 comments on commit 1e14293

Please sign in to comment.