import React, { useState } from "react";
import {
CanAccess,
ITreeMenu,
useIsExistAuthentication,
useLogout,
useMenu,
useRefineContext,
useRouterContext,
useTitle,
useTranslate,
} from "@pankod/refine-core";
import {
ActionIcon,
Box,
Drawer,
Navbar,
NavLink,
NavLinkStylesNames,
NavLinkStylesParams,
ScrollArea,
MediaQuery,
Button,
Tooltip,
TooltipProps,
Styles,
} from "@mantine/core";
import {
IconList,
IconMenu2,
IconChevronRight,
IconChevronLeft,
IconLogout,
IconDashboard,
} from "@tabler/icons";
import { RefineLayoutSiderProps } from "../types";
import { RefineTitle as DefaultTitle } from "@components";
const defaultNavIcon = ;
export const Sider: React.FC = ({ render }) => {
const [collapsed, setCollapsed] = useState(false);
const [opened, setOpened] = useState(false);
const { Link } = useRouterContext();
const { defaultOpenKeys, menuItems, selectedKey } = useMenu();
const Title = useTitle();
const isExistAuthentication = useIsExistAuthentication();
const t = useTranslate();
const { hasDashboard } = useRefineContext();
const { mutate: mutateLogout } = useLogout();
const RenderToTitle = Title ?? DefaultTitle;
const drawerWidth = () => {
if (collapsed) return 80;
return 200;
};
const commonNavLinkStyles: Styles =
{
root: {
display: "flex",
color: "white",
fontWeight: 500,
"&:hover": {
backgroundColor: "unset",
},
"&[data-active]": {
backgroundColor: "#ffffff1a",
color: "white",
fontWeight: 700,
"&:hover": {
backgroundColor: "#ffffff1a",
},
},
justifyContent: collapsed && !opened ? "center" : "flex-start",
},
icon: {
marginRight: collapsed && !opened ? 0 : 12,
},
body: {
display: collapsed && !opened ? "none" : "flex",
},
};
const commonTooltipProps: Partial = {
disabled: !collapsed || opened,
position: "right",
withinPortal: true,
withArrow: true,
arrowSize: 8,
arrowOffset: 12,
offset: 4,
};
const renderTreeView = (tree: ITreeMenu[], selectedKey: string) => {
return tree.map((item) => {
const { icon, label, route, name, children } = item;
const isSelected = route === selectedKey;
const isParent = children.length > 0;
const additionalLinkProps = isParent
? {}
: { component: Link, to: route };
return (
{isParent && renderTreeView(children, selectedKey)}
);
});
};
const items = renderTreeView(menuItems, selectedKey);
const dashboard = hasDashboard ? (
}
component={Link}
to="/"
active={selectedKey === "/"}
styles={commonNavLinkStyles}
/>
) : null;
const logout = isExistAuthentication && (
}
onClick={() => mutateLogout()}
styles={commonNavLinkStyles}
/>
);
const renderSider = () => {
if (render) {
return render({
dashboard,
logout,
items,
collapsed,
});
}
return (
<>
{dashboard}
{items}
{logout}
>
);
};
return (
<>
setOpened((prev) => !prev)}
>
setOpened(false)}
size={200}
zIndex={1200}
withCloseButton={false}
styles={{
drawer: {
overflow: "hidden",
backgroundColor: "#2A132E",
},
}}
>
{renderSider()}
{renderSider()}
>
);
};