'use client' import { FC, ReactNode } from 'react'; import Link from 'next/link'; import { usePathname, useRouter } from 'next/navigation'; import { ChevronLeft } from 'lucide-react' import useLocalStorage from '@/hooks/useLocalStorage'; import { useTheme } from 'next-themes'; import { motion } from "framer-motion"; export interface ISideMenu { title: string, Icon?: ReactNode, path?: string, submenuId?: number, submenuItems?: ISideMenu[] } type Props = { menu: ISideMenu, expand: boolean, onChangeExpand: (status: boolean) => any, }; const SidebarItem: FC = ({ menu, expand, onChangeExpand }) => { const [submenuIdActive, setSubmenuIdActive] = useLocalStorage('submenu', typeof window !== 'undefined' ? localStorage.getItem('submenu') || [] : []) const { title, Icon, path, submenuId, submenuItems } = menu const { systemTheme, theme } = useTheme() const pathanme = usePathname() const router = useRouter() const handleClickSubMenu = (submenuId: number) => { onChangeExpand(true) if (submenuIdActive?.includes(submenuId)) { setSubmenuIdActive(submenuIdActive?.filter((id: number) => id !== submenuId)) } else { setSubmenuIdActive([...submenuIdActive, submenuId]) } } const getActiveBackgroundMenu = () => { const currentPath = pathanme || ''; const fullPath = '/admin' + (menu.path || ''); const isActive = menu.path === '' ? currentPath === '/admin' || currentPath === '/admin/' : currentPath === fullPath || (menu.path && currentPath.startsWith(fullPath + '/')); if (isActive) { return { color: theme === 'dark' ? '#fff' : theme === 'system' && systemTheme === 'dark' ? '#fff' : '#fff', fontWeight: theme === 'light' ? 600 : theme === 'system' && systemTheme === 'light' ? 600 : undefined, background: theme === 'dark' ? '#27272a' : theme === 'system' && systemTheme === 'dark' ? '#27272a' : '#EA512C' } } else { return {} } } return ( <>
  • {submenuItems && submenuItems.length > 0 ?
    handleClickSubMenu(submenuId!)}> {Icon} {title} {submenuItems && submenuItems.length > 0 && expand && }
    : router.push('/admin' + path)} > {Icon} {title} }
  • {submenuItems && submenuItems.length > 0 && submenuIdActive?.includes(submenuId) && expand && {submenuItems!.map((submenu, key) => )} } ); } export default SidebarItem;