Skip to content

Commit

Permalink
refactor: nav refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
d3george authored and d3george committed Nov 7, 2024
1 parent 44fe6c3 commit 12c1fca
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/layouts/dashboard/nav/nav-vertical.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Menu, MenuProps } from 'antd';
import Color from 'color';
import { useMemo, useState } from 'react';
import { useEffect, useMemo, useState } from 'react';
import { useMatches, useNavigate } from 'react-router-dom';

import Scrollbar from '@/components/scrollbar';
Expand Down Expand Up @@ -37,27 +37,32 @@ export default function NavVertical(props: Props) {
const permissionRoutes = usePermissionRoutes();
const flattenedRoutes = useFlattenedRoutes();

const [collapsed, setCollapsed] = useState(themeLayout === ThemeLayout.Mini);
const collapsed = useMemo(() => themeLayout === ThemeLayout.Mini, [themeLayout]);

const menuList = useMemo(() => {
const menuRoutes = menuFilter(permissionRoutes);
return routeToMenuFn(menuRoutes);
}, [routeToMenuFn, permissionRoutes]);

const selectedKeys = useMemo(() => [pathname], [pathname]);
const openKeys = useMemo(() => {
const keys = matches
.filter((match) => match.pathname !== '/')
.filter((match) => match.pathname !== pathname)
.map((match) => match.pathname);
return keys;
}, [matches, pathname]);

const [openKeys, setOpenKeys] = useState<string[]>([]);

useEffect(() => {
if (!collapsed) {
const keys = matches
.filter((match) => match.pathname !== '/')
.filter((match) => match.pathname !== pathname)
.map((match) => match.pathname);
setOpenKeys(keys);
}
}, [matches, pathname, collapsed]);

const handleToggleCollapsed = () => {
setSettings({
...settings,
themeLayout: collapsed ? ThemeLayout.Vertical : ThemeLayout.Mini,
});
setCollapsed(!collapsed);
};

const onClick: MenuProps['onClick'] = ({ key, keyPath }) => {
Expand All @@ -75,6 +80,12 @@ export default function NavVertical(props: Props) {
props?.closeSideBarDrawer?.();
};

const handleOpenChange: MenuProps['onOpenChange'] = (keys) => {
if (!collapsed) {
setOpenKeys(keys);
}
};

return (
<div
style={{
Expand All @@ -94,6 +105,7 @@ export default function NavVertical(props: Props) {
style={{ backgroundColor: colorBgElevated }}
className="h-full !border-none"
onClick={onClick}
onOpenChange={handleOpenChange}
/>
</Scrollbar>
</div>
Expand Down

0 comments on commit 12c1fca

Please sign in to comment.