mirror of
https://github.com/ichsankurnia/hats-app.git
synced 2026-12-05 10:46:29 +00:00
7f8e6637e4
Extract NextAuth configuration into a shared library, load admin sidebar items from role permissions, and update related RBAC types and UI.
154 lines
7.8 KiB
TypeScript
154 lines
7.8 KiB
TypeScript
'use client'
|
|
|
|
import dynamic from 'next/dynamic';
|
|
import { FC, FormEvent, useEffect, useRef, useState } from 'react';
|
|
import { ArrowBigLeft } from 'lucide-react'
|
|
import SIDEBAR_MENU from '@/lib/data-sidebar';
|
|
import { Input } from '@/components/ui/input';
|
|
import { Button } from '@/components/ui/button';
|
|
import { ISideMenu } from './SidebarItem';
|
|
import { ToggleTheme } from './ToggleTheme';
|
|
import Notification from './Notification';
|
|
import Link from 'next/link';
|
|
import { useRouter } from 'next/navigation';
|
|
import { UserNav } from '@/app/(dashboard)/components/UserNav';
|
|
|
|
|
|
const SidebarItem = dynamic(() => import("./SidebarItem"), {
|
|
loading: () => <p className='text-zinc-200 dark:text-zinc-800 mb-3 animate-pulse'>loading...</p>,
|
|
ssr: false
|
|
})
|
|
|
|
type Props = {
|
|
menuList: ISideMenu[]
|
|
};
|
|
|
|
const Sidebar: FC<Props> = ({ menuList }) => {
|
|
const [expand, setExpand] = useState(true)
|
|
const [sidebarOpen, setSidebarOpen] = useState(false)
|
|
const [search, setSearch] = useState('')
|
|
|
|
const trigger = useRef<any>(null);
|
|
const sidebar = useRef<any>(null);
|
|
const router = useRouter()
|
|
|
|
// const menu: ISideMenu[] = SIDEBAR_MENU
|
|
const menu: ISideMenu[] = menuList
|
|
|
|
// close on click outside
|
|
useEffect(() => {
|
|
const clickHandler = ({ target }: any) => {
|
|
if (!sidebar.current || !trigger.current) return;
|
|
if (!sidebarOpen || sidebar.current.contains(target) || trigger.current.contains(target)) return;
|
|
setSidebarOpen(false);
|
|
};
|
|
document.addEventListener('click', clickHandler);
|
|
return () => document.removeEventListener('click', clickHandler);
|
|
});
|
|
|
|
// close if the esc key is pressed
|
|
useEffect(() => {
|
|
const keyHandler = ({ keyCode }: any) => {
|
|
if (!sidebarOpen || keyCode !== 27) return;
|
|
setSidebarOpen(false);
|
|
};
|
|
document.addEventListener('keydown', keyHandler);
|
|
return () => document.removeEventListener('keydown', keyHandler);
|
|
});
|
|
|
|
const handleSearch = (e: FormEvent<HTMLFormElement>) => {
|
|
e.preventDefault()
|
|
|
|
if (search) {
|
|
setSearch('')
|
|
}
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div className='z-20'>
|
|
|
|
{/* SIDEBAR MOBILE */}
|
|
<div className={`fixed inset-0 bg-zinc-950 bg-opacity-60 z-20 lg:hidden lg:z-auto transition-opacity duration-200 backdrop-blur-sm ${sidebarOpen ? 'opacity-100' : 'opacity-0 pointer-events-none'}`} aria-hidden="true" onClick={() => setSidebarOpen(false)}></div>
|
|
|
|
<div className='lg:hidden sticky bg-white/70 dark:bg-zinc-950/70 top-0 w-full pb-3 px-4 sm:px-6 lg:px-8 z-10 mb-5 rounded-b-2xl'>
|
|
<div className="flex items-center justify-between pt-5 pb-2 backdrop-blur-xl">
|
|
<Button variant='outline' size='icon' onClick={() => setSidebarOpen(!sidebarOpen)}>
|
|
{sidebarOpen ?
|
|
<svg className="block h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true" >
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M6 18L18 6M6 6l12 12" />
|
|
</svg>
|
|
:
|
|
<svg className="block h-6 w-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M4 6h16M4 12h16M4 18h16" />
|
|
</svg>
|
|
}
|
|
</Button>
|
|
<div className="flex items-center space-x-3">
|
|
<div className="flex items-center space-x-3 border-r pr-3 dark:border-zinc-800">
|
|
<Notification />
|
|
<ToggleTheme />
|
|
</div>
|
|
<Link href='/'>
|
|
<Button variant='outline' size='icon'>
|
|
<i className="ti ti-home text-xl"></i>
|
|
</Button>
|
|
</Link>
|
|
<UserNav />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* SIDEBAR */}
|
|
<nav id="sidebar" ref={sidebar}
|
|
className={`z-30 h-screen duration-300 ${expand ? 'w-72' : 'w-[78px] top-20'} ${sidebarOpen ? 'translate-x-0' : '-translate-x-80'} absolute left-0 top-0 lg:static lg:left-auto lg:top-auto lg:translate-x-0 border-r border-zinc-200 dark:border-zinc-800 bg-white dark:bg-zinc-950`}
|
|
>
|
|
<Button variant='outline' size='icon' onClick={() => setExpand(!expand)} className={`hidden lg:flex absolute top-8 duration-300 z-10 ${!expand ? 'rotate-180 left-[60px]' : 'left-[270px]'}`}>
|
|
<ArrowBigLeft />
|
|
</Button>
|
|
|
|
<div className={`overflow-y-auto overflow-x-hidden h-full p-5 ${expand ? 'pt-6' : 'pt-0'} md:scrollbar-hide`}>
|
|
{expand ?
|
|
<Link href='/' className='flex items-center hover:opacity-70 duration-300' onClick={() => router.refresh()}>
|
|
<div className={`flex justify-center items-center text-2xl rounded cursor-pointer float-left mr-3 duration-500 h-16 max-w-[225px]`}>
|
|
<img alt='logo' loading='lazy' src='/assets/LOGO_HIS_HATs_NI.png' className='h-full w-full object-cover' />
|
|
</div>
|
|
</Link>
|
|
:
|
|
<Link href='/' className='flex items-center hover:opacity-70 duration-300 mt-6 mb-5' onClick={() => router.refresh()}>
|
|
<div className={`flex justify-center items-center text-2xl rounded cursor-pointer float-left mr-3 duration-500 h-10 w-12`}>
|
|
<img alt='logo' loading='lazy' src='/assets/LOGO_HATs.png' className='h-full w-full' />
|
|
</div>
|
|
</Link>
|
|
}
|
|
|
|
{/* SEARCH FIELD */}
|
|
<form onSubmit={handleSearch} className={`mt-4 py-2 ${!expand && 'hidden'}`}>
|
|
<Input type="search" placeholder="Search..." value={search} onChange={e => setSearch(e.target.value)} className={`w-full focus:outline-none`} />
|
|
</form>
|
|
|
|
<ul className='pt-2'>
|
|
{menu && menu.length > 0 && menu!.map((menu, index) =>
|
|
<SidebarItem key={index}
|
|
menu={menu}
|
|
expand={expand} onChangeExpand={(status) => setExpand(status)}
|
|
/>
|
|
)}
|
|
{/* <li className={`cursor-pointer p-2 rounded-xl text-zinc-500 dark:text-zinc-400 font-medium text-sm hover:text-black dark:hover:text-white duration-100`}>
|
|
<Link href={'/dssa'} className={`flex items-center ${expand && 'gap-x-3.5'}`} >
|
|
<span className='float-left flex justify-center items-center w-5 h-5 text-lg'>
|
|
<i className="ti ti-layout-dashboard flex justify-center items-center"></i>
|
|
</span>
|
|
<span className={`flex-1 duration-200 ${!expand && 'hidden'}`}>{"Test Font Awesome"}</span>
|
|
</Link>
|
|
</li> */}
|
|
</ul>
|
|
|
|
</div>
|
|
</nav>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default Sidebar; |