Skip to content

Commit 44fe6c3

Browse files
d3georged3george
authored andcommitted
refactor: nav refactor
1 parent e711d0a commit 44fe6c3

File tree

2 files changed

+54
-44
lines changed

2 files changed

+54
-44
lines changed

src/layouts/dashboard/nav/nav-horizontal.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { Menu, MenuProps } from 'antd';
2-
import { useMemo, useState } from 'react';
3-
import { useNavigate, useMatches } from 'react-router-dom';
4-
5-
import { useRouteToMenuFn, usePermissionRoutes, useFlattenedRoutes } from '@/router/hooks';
2+
import { useMemo } from 'react';
3+
import { useNavigate } from 'react-router-dom';
4+
5+
import {
6+
useRouteToMenuFn,
7+
usePermissionRoutes,
8+
useFlattenedRoutes,
9+
usePathname,
10+
} from '@/router/hooks';
611
import { menuFilter } from '@/router/utils';
712
import { useThemeToken } from '@/theme/hooks';
813

914
import { NAV_HORIZONTAL_HEIGHT } from '../config';
1015

1116
export default function NavHorizontal() {
1217
const navigate = useNavigate();
13-
const matches = useMatches();
18+
const pathname = usePathname();
1419
const { colorBgElevated } = useThemeToken();
1520

1621
const routeToMenuFn = useRouteToMenuFn();
@@ -22,13 +27,10 @@ export default function NavHorizontal() {
2227
return routeToMenuFn(menuRoutes);
2328
}, [routeToMenuFn, permissionRoutes]);
2429

25-
const [currentOpenKeys, setCurrentOpenKeys] = useState<string[]>(() => {
26-
return matches.filter((match) => match.pathname !== '/').map((match) => match.pathname);
27-
});
28-
const onClick: MenuProps['onClick'] = ({ key, keyPath }) => {
29-
const nextLink = flattenedRoutes?.find((el) => el.key === key);
30-
setCurrentOpenKeys(keyPath);
30+
const selectedKeys = useMemo(() => [pathname], [pathname]);
3131

32+
const onClick: MenuProps['onClick'] = ({ key }) => {
33+
const nextLink = flattenedRoutes?.find((el) => el.key === key);
3234
// Handle special case for external links in menu items
3335
// For external links: skip internal routing, avoid adding new tab in current project,
3436
// prevent selecting current route, and open link in new browser tab
@@ -45,7 +47,7 @@ export default function NavHorizontal() {
4547
mode="horizontal"
4648
items={menuList}
4749
defaultOpenKeys={[]}
48-
defaultSelectedKeys={currentOpenKeys}
50+
selectedKeys={selectedKeys}
4951
onClick={onClick}
5052
className="!z-10 !border-none"
5153
style={{ background: colorBgElevated }}
Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { Menu, MenuProps } from 'antd';
22
import Color from 'color';
3-
import { useCallback, useMemo, useState } from 'react';
3+
import { useMemo, useState } from 'react';
44
import { useMatches, useNavigate } from 'react-router-dom';
55

66
import Scrollbar from '@/components/scrollbar';
7-
import { useRouteToMenuFn, usePermissionRoutes, useFlattenedRoutes } from '@/router/hooks';
7+
import {
8+
useRouteToMenuFn,
9+
usePermissionRoutes,
10+
useFlattenedRoutes,
11+
usePathname,
12+
} from '@/router/hooks';
813
import { menuFilter } from '@/router/utils';
914
import { useSettingActions, useSettings } from '@/store/settingStore';
1015
import { useThemeToken } from '@/theme/hooks';
@@ -22,6 +27,7 @@ export default function NavVertical(props: Props) {
2227
const navigate = useNavigate();
2328
const matches = useMatches();
2429
const { colorBgElevated, colorBorder } = useThemeToken();
30+
const pathname = usePathname();
2531

2632
const settings = useSettings();
2733
const { themeLayout } = settings;
@@ -31,27 +37,32 @@ export default function NavVertical(props: Props) {
3137
const permissionRoutes = usePermissionRoutes();
3238
const flattenedRoutes = useFlattenedRoutes();
3339

40+
const [collapsed, setCollapsed] = useState(themeLayout === ThemeLayout.Mini);
3441
const menuList = useMemo(() => {
3542
const menuRoutes = menuFilter(permissionRoutes);
3643
return routeToMenuFn(menuRoutes);
3744
}, [routeToMenuFn, permissionRoutes]);
3845

39-
const [currentOpenKeys, setCurrentOpenKeys] = useState<string[]>(() => {
40-
return matches.filter((match) => match.pathname !== '/').map((match) => match.pathname);
41-
});
46+
const selectedKeys = useMemo(() => [pathname], [pathname]);
47+
const openKeys = useMemo(() => {
48+
const keys = matches
49+
.filter((match) => match.pathname !== '/')
50+
.filter((match) => match.pathname !== pathname)
51+
.map((match) => match.pathname);
52+
return keys;
53+
}, [matches, pathname]);
4254

43-
const collapsed = useMemo(() => themeLayout === ThemeLayout.Mini, [themeLayout]);
44-
45-
const handleToggleCollapsed = useCallback(() => {
55+
const handleToggleCollapsed = () => {
4656
setSettings({
4757
...settings,
4858
themeLayout: collapsed ? ThemeLayout.Vertical : ThemeLayout.Mini,
4959
});
50-
}, [collapsed, settings, setSettings]);
60+
setCollapsed(!collapsed);
61+
};
5162

5263
const onClick: MenuProps['onClick'] = ({ key, keyPath }) => {
64+
console.log('click', key, keyPath);
5365
const nextLink = flattenedRoutes?.find((el) => el.key === key);
54-
setCurrentOpenKeys(keyPath);
5566
// Handle special case for external links in menu items
5667
// For external links: skip internal routing, avoid adding new tab in current project,
5768
// prevent selecting current route, and open link in new browser tab
@@ -65,29 +76,26 @@ export default function NavVertical(props: Props) {
6576
};
6677

6778
return (
68-
<div className="z-50 h-full flex-shrink-0">
69-
<div
70-
className="flex h-full flex-col"
71-
style={{
72-
width: collapsed ? NAV_COLLAPSED_WIDTH : NAV_WIDTH,
73-
borderRight: `1px dashed ${Color(colorBorder).alpha(0.6).toString()}`,
74-
}}
75-
>
76-
<NavLogo collapsed={collapsed} onToggle={handleToggleCollapsed} />
79+
<div
80+
style={{
81+
width: collapsed ? NAV_COLLAPSED_WIDTH : NAV_WIDTH,
82+
borderRight: `1px dashed ${Color(colorBorder).alpha(0.6).toString()}`,
83+
}}
84+
>
85+
<NavLogo collapsed={collapsed} onToggle={handleToggleCollapsed} />
7786

78-
<Scrollbar style={{ height: `calc(100vh - ${HEADER_HEIGHT}px)` }}>
79-
<Menu
80-
mode="inline"
81-
inlineCollapsed={collapsed}
82-
items={menuList}
83-
defaultOpenKeys={themeLayout === ThemeLayout.Vertical ? currentOpenKeys : []}
84-
defaultSelectedKeys={currentOpenKeys}
85-
style={{ backgroundColor: colorBgElevated }}
86-
className="h-full !border-none"
87-
onClick={onClick}
88-
/>
89-
</Scrollbar>
90-
</div>
87+
<Scrollbar style={{ height: `calc(100vh - ${HEADER_HEIGHT}px)` }}>
88+
<Menu
89+
mode="inline"
90+
inlineCollapsed={collapsed}
91+
items={menuList}
92+
{...(!collapsed && { openKeys })}
93+
selectedKeys={selectedKeys}
94+
style={{ backgroundColor: colorBgElevated }}
95+
className="h-full !border-none"
96+
onClick={onClick}
97+
/>
98+
</Scrollbar>
9199
</div>
92100
);
93101
}

0 commit comments

Comments
 (0)