Skip to content

Commit

Permalink
fix(projects): fix home when is dynamic mode router
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Mar 11, 2024
1 parent 5ecf384 commit 3c2296e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/router/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { CustomRoute, ElegantConstRoute, ElegantRoute } from '@elegant-router/types';
import { generatedRoutes } from '../elegant/routes';
import { layouts, views } from '../elegant/imports';
import { transformElegantRoutesToVueRoutes } from '../elegant/transform';
import { getRoutePath, transformElegantRoutesToVueRoutes } from '../elegant/transform';

export const ROOT_ROUTE: CustomRoute = {
name: 'root',
path: '/',
redirect: '/home',
redirect: getRoutePath(import.meta.env.VITE_ROUTE_HOME) || '/home',
meta: {
title: 'root',
constant: true
Expand Down
8 changes: 5 additions & 3 deletions src/store/modules/tab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import type { RouteKey } from '@elegant-router/types';
import { SetupStoreId } from '@/enum';
import { useRouterPush } from '@/hooks/common/router';
import { localStg } from '@/utils/storage';
import { useRouteStore } from '@/store/modules/route';
import { useThemeStore } from '../theme';
import {
filterTabsByAllRoutes,
extractTabsByAllRoutes,
filterTabsById,
filterTabsByIds,
findTabByRouteName,
Expand All @@ -23,6 +24,7 @@ import {

export const useTabStore = defineStore(SetupStoreId.Tab, () => {
const router = useRouter();
const routeStore = useRouteStore();
const themeStore = useThemeStore();
const { routerPush } = useRouterPush(false);

Expand All @@ -38,7 +40,7 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
* @param router Router instance
*/
function initHomeTab() {
homeTab.value = getDefaultHomeTab(router);
homeTab.value = getDefaultHomeTab(router, routeStore.routeHome);
}

/** Get all tabs */
Expand All @@ -65,7 +67,7 @@ export const useTabStore = defineStore(SetupStoreId.Tab, () => {
const storageTabs = localStg.get('globalTabs');

if (themeStore.tab.cache && storageTabs) {
const filteredTabs = filterTabsByAllRoutes(router, storageTabs);
const filteredTabs = extractTabsByAllRoutes(router, storageTabs);
tabs.value = updateTabsByI18nKey(filteredTabs);
}

Expand Down
16 changes: 10 additions & 6 deletions src/store/modules/tab/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ export function getAllTabs(tabs: App.Global.Tab[], homeTab?: App.Global.Tab) {
return [];
}

const fixedTabs = tabs.filter(tab => tab.fixedIndex !== undefined).sort((a, b) => a.fixedIndex! - b.fixedIndex!);
const filterHomeTabs = tabs.filter(tab => tab.id !== homeTab.id);

const remainTabs = tabs.filter(tab => tab.fixedIndex === undefined);
const fixedTabs = filterHomeTabs
.filter(tab => tab.fixedIndex !== undefined)
.sort((a, b) => a.fixedIndex! - b.fixedIndex!);

const remainTabs = filterHomeTabs.filter(tab => tab.fixedIndex === undefined);

const allTabs = [homeTab, ...fixedTabs, ...remainTabs];

Expand Down Expand Up @@ -99,9 +103,9 @@ export function getRouteIcons(route: App.Global.TabRoute) {
* Get default home tab
*
* @param router
* @param homeRouteName routeHome in useRouteStore
*/
export function getDefaultHomeTab(router: Router) {
const homeRouteName = import.meta.env.VITE_ROUTE_HOME;
export function getDefaultHomeTab(router: Router, homeRouteName: LastLevelRouteKey) {
const homeRoutePath = getRoutePath(homeRouteName);
const i18nLabel = $t(`route.${homeRouteName}`);

Expand Down Expand Up @@ -153,12 +157,12 @@ export function filterTabsByIds(tabIds: string[], tabs: App.Global.Tab[]) {
}

/**
* filter tabs by all routes
* extract tabs by all routes
*
* @param router
* @param tabs
*/
export function filterTabsByAllRoutes(router: Router, tabs: App.Global.Tab[]) {
export function extractTabsByAllRoutes(router: Router, tabs: App.Global.Tab[]) {
const routes = router.getRoutes();

const routeNames = routes.map(route => route.name);
Expand Down

0 comments on commit 3c2296e

Please sign in to comment.