mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:56:28 +00:00
8478b1f28f
Add dedicated 403 pages for the app and admin area, enable Next.js forbidden handling, and improve admin loading, navigation, and error-boundary experiences.
106 lines
4.4 KiB
TypeScript
106 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;
|