import React, { useEffect, useState } from 'react' import { mdiMinus, mdiPlus } from '@mdi/js' import BaseIcon from './BaseIcon' import Link from 'next/link' import { getButtonColor } from '../colors' import AsideMenuList from './AsideMenuList' import { MenuAsideItem } from '../interfaces' import { useAppSelector } from '../stores/hooks' import { useRouter } from 'next/router' type Props = { item: MenuAsideItem isDropdownList?: boolean } const AsideMenuItem = ({ item, isDropdownList = false }: Props) => { const [isLinkActive, setIsLinkActive] = useState(false) const [isDropdownActive, setIsDropdownActive] = useState(Boolean(item.isOpenByDefault)) const asideMenuItemStyle = useAppSelector((state) => state.style.asideMenuItemStyle) const asideMenuItemActiveStyle = useAppSelector((state) => state.style.asideMenuItemActiveStyle) const borders = useAppSelector((state) => state.style.borders); const activeLinkColor = useAppSelector( (state) => state.style.activeLinkColor, ); const activeClassAddon = !item.color && isLinkActive ? asideMenuItemActiveStyle : '' const { asPath, isReady } = useRouter() useEffect(() => { if (item.href && isReady) { const linkPathName = new URL(item.href, location.href).pathname + '/'; const activePathname = new URL(asPath, location.href).pathname const activeView = activePathname.split('/')[1]; const linkPathNameView = linkPathName.split('/')[1]; setIsLinkActive(linkPathNameView === activeView); } if (item.menu && isReady) { const activePathname = new URL(asPath, location.href).pathname const activeView = activePathname.split('/')[1]; const hasActiveChild = item.menu.some((menuItem) => { if (!menuItem.href) { return false; } const childPathName = new URL(menuItem.href, location.href).pathname + '/'; const childView = childPathName.split('/')[1]; return childView === activeView; }); if (hasActiveChild) { setIsDropdownActive(true); } } }, [item.href, isReady, asPath]) const asideMenuItemInnerContents = ( <> {item.icon && ( )} {item.label} {item.menu && ( )} ) const componentClass = [ 'flex min-w-0 cursor-pointer items-center gap-3 rounded-lg px-3 py-2.5 transition-colors', isDropdownList ? 'text-sm' : 'text-[15px] font-medium', item.color ? getButtonColor(item.color, false, true) : `${asideMenuItemStyle}`, isLinkActive ? `text-[#0E1A2B] ${activeLinkColor} dark:text-white dark:bg-dark-800` : '', ].join(' '); return (
  • {item.withDevider &&
    } {item.href && ( {asideMenuItemInnerContents} )} {!item.href && (
    setIsDropdownActive(!isDropdownActive)}> {asideMenuItemInnerContents}
    )} {item.menu && ( )}
  • ) } export default AsideMenuItem