Files
hats-app/app/admin/components/SidebarItem.tsx
T
Ichsan Kurniawan 751b28fab2 Add RBAC menu and role management
Adds permission, menu, and role administration with hierarchical access controls, plus admin breadcrumbs and a redirect from the legacy user-role page. Remove `credentials.md` before committing; it contains server credentials.
2026-11-05 19:01:40 +07:00

108 lines
4.4 KiB
TypeScript

'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<Props> = ({ 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 (
<>
<li style={getActiveBackgroundMenu()} className={`cursor-pointer p-2.5 rounded-md text-zinc-500 dark:text-zinc-400 font-medium text-sm hover:text-black dark:hover:text-white duration-300`}>
{submenuItems && submenuItems.length > 0 ?
<div className={`flex items-center gap-x-4 ${submenuIdActive?.includes(submenuId) && 'font-bold text-black dark:text-white'}`} title={title} onClick={() => handleClickSubMenu(submenuId!)}>
<span className='block float-left'>
{Icon}
</span>
<span className={`flex-1 duration-200 ${!expand && 'hidden'}`}>{title}</span>
{submenuItems && submenuItems.length > 0 && expand &&
<ChevronLeft size={16} className={`duration-500 ${submenuIdActive?.includes(submenuId) && '-rotate-90'}`} />
}
</div>
:
<Link href={'/admin' + path!} prefetch className={`flex items-center ${expand && 'gap-x-4'}`} title={title} onClick={() => router.push('/admin' + path)} >
<span className='block float-left'>
{Icon}
</span>
<span className={`flex-1 duration-200 ${!expand && 'hidden'}`}>{title}</span>
</Link>
}
</li>
{submenuItems && submenuItems.length > 0 && submenuIdActive?.includes(submenuId) && expand &&
<motion.ul
animate={
submenuIdActive?.includes(submenuId) ?
{
height: "fit-content",
}
:
{
height: 0,
}
}
className="flex h-0 flex-col pl-5 overflow-hidden text-white"
>
{submenuItems!.map((submenu, key) =>
<SidebarItem key={key} menu={submenu} expand={expand} onChangeExpand={onChangeExpand} />
)}
</motion.ul>
}
</>
);
}
export default SidebarItem;