import React from 'react'; import { Box, Drawer, DrawerBody, DrawerCloseButton, DrawerContent, DrawerOverlay, Flex, useColorModeValue, Text, Spacer, useBreakpoint, VStack, } from '@chakra-ui/react'; import { useTranslation } from 'react-i18next'; import { useLocation } from 'react-router-dom'; import { v4 as uuid } from 'uuid'; import { NavLinkButton } from './NavLinkButton'; import { useAuth } from 'contexts/AuthProvider'; import { Route } from 'models/Routes'; const variantChange = '0.2s linear'; export type SidebarProps = { routes: Route[]; isOpen: boolean; toggle: () => void; logo: React.ReactNode; version: string; children?: React.ReactNode; topNav?: (isRouteActive: (str: string, str2: string) => boolean, toggleSidebar: () => void) => React.ReactNode; }; export const Sidebar = ({ routes, isOpen, toggle, logo, version, topNav, children }: SidebarProps) => { const { t } = useTranslation(); const { user } = useAuth(); const location = useLocation(); const navbarShadow = useColorModeValue('0px 7px 23px rgba(0, 0, 0, 0.05)', 'none'); const breakpoint = useBreakpoint(); const isRouteActive = (routeName: string, otherRoute?: string) => { if (otherRoute) return ( location.pathname.split('/')[1] === routeName.split('/')[1] || location.pathname.split('/')[1] === otherRoute.split('/')[1] ); return location.pathname === routeName.replace(':id', '0'); }; const isCompact = breakpoint === 'base' || breakpoint === 'sm' || breakpoint === 'md'; const brand = ( {logo} ); const sidebarContent = React.useMemo( () => ( <> {topNav ? topNav(isRouteActive, toggle) : null} {routes .filter(({ hidden, authorized }) => !hidden && authorized.includes(user?.userRole ?? '')) .map((route) => ( ))} {children} {t('footer.version')} {version} ), [user?.userRole, location], ); return ( <> {brand} {sidebarContent} ); };