Files
ims/components/layout/sidebar.tsx
T

203 lines
6.9 KiB
TypeScript

'use client'
import Link from 'next/link'
import { usePathname, useRouter } from 'next/navigation'
import {
AlignLeft,
CircleCheck,
ClipboardList,
LayoutDashboard,
LogOut,
Plus,
Settings,
User,
Users,
} from 'lucide-react'
import { NotificationBell } from '@/components/notifications/bell'
interface NavItem {
label: string
href: string
icon: React.ReactNode
}
interface SidebarProps {
role: string
userName: string
userEmail: string
}
const IconGrid = () => <AlignLeft size={20} aria-hidden />
const IconList = () => <ClipboardList size={20} aria-hidden />
const IconCheck = () => <CircleCheck size={20} aria-hidden />
const IconChart = () => <LayoutDashboard size={20} aria-hidden />
const IconSettings = () => <Settings size={20} aria-hidden />
const IconUsers = () => <Users size={20} aria-hidden />
const IconPlus = () => <Plus size={20} aria-hidden />
const IconLogout = () => <LogOut size={16} aria-hidden />
const IconUser = () => <User size={16} aria-hidden />
const NAV_ITEMS: Record<string, NavItem[]> = {
reporter: [
{ label: 'My Reports', href: '/reporter', icon: <IconList /> },
{ label: 'New Incident', href: '/report', icon: <IconPlus /> },
],
supervisor: [
{ label: 'Overview', href: '/supervisor', icon: <IconGrid /> },
{ label: 'Incidents', href: '/supervisor/incidents', icon: <IconList /> },
],
hse: [
{ label: 'Dashboard', href: '/hse/dashboard', icon: <IconChart /> },
{ label: 'Incidents', href: '/hse/incidents', icon: <IconList /> },
{ label: 'CAPA', href: '/hse/capa', icon: <IconCheck /> },
{ label: 'Settings', href: '/hse/settings', icon: <IconSettings /> },
],
management: [
{ label: 'Dashboard', href: '/management', icon: <IconChart /> },
],
capa_owner: [
{ label: 'My CAPAs', href: '/capa-owner', icon: <IconCheck /> },
],
admin: [
{ label: 'Admin', href: '/admin', icon: <IconUsers /> },
{ label: 'Dashboard', href: '/hse/dashboard', icon: <IconChart /> },
{ label: 'Incidents', href: '/hse/incidents', icon: <IconList /> },
{ label: 'CAPA', href: '/hse/capa', icon: <IconCheck /> },
{ label: 'Management', href: '/management', icon: <IconGrid /> },
{ label: 'Settings', href: '/hse/settings', icon: <IconSettings /> },
],
}
const EXACT_ROOTS = ['/reporter', '/supervisor', '/management', '/capa-owner', '/admin', '/report', '/account']
function isActivePath(href: string, pathname: string): boolean {
return EXACT_ROOTS.includes(href) ? pathname === href : pathname.startsWith(href)
}
function initials(name: string): string {
return name
.split(' ')
.map(w => w[0] ?? '')
.join('')
.toUpperCase()
.slice(0, 2)
}
function useNavItems(role: string): NavItem[] {
return NAV_ITEMS[role] ?? NAV_ITEMS.reporter
}
const LINK_BASE = 'flex items-center gap-3 px-3 py-2 text-sm border-l-2 transition-colors'
const LINK_ACTIVE = 'border-accent text-accent-700 font-extrabold bg-accent-100/50'
const LINK_IDLE = 'border-transparent text-ink/70 font-medium hover:bg-ink/7 hover:text-ink'
function NavLink({ item, pathname }: { item: NavItem; pathname: string }) {
const active = isActivePath(item.href, pathname)
return (
<Link
href={item.href}
aria-current={active ? 'page' : undefined}
className={`${LINK_BASE} ${active ? LINK_ACTIVE : LINK_IDLE}`}
>
<span className={active ? 'text-accent-700' : 'text-ink/50'}>{item.icon}</span>
{item.label}
</Link>
)
}
export function Sidebar({ role, userName, userEmail }: SidebarProps) {
const pathname = usePathname()
const router = useRouter()
const items = useNavItems(role)
const mobileItems = items.slice(0, 4)
const logout = async () => {
await fetch('/api/auth/logout', { method: 'POST' })
router.push('/login')
router.refresh()
}
const displayName = userName || userEmail || 'User'
const roleLabel = role.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase())
const accountActive = pathname === '/account'
return (
<>
{/* Desktop sidebar */}
<aside className="hidden sm:flex fixed left-0 top-0 h-full w-60 bg-ground border-r-2 border-divider flex-col z-40">
<div className="flex items-center justify-between px-4 py-4 border-b-2 border-divider">
<span className="text-lg font-extrabold tracking-tight text-ink">IMS</span>
<NotificationBell />
</div>
<nav className="flex-1 overflow-y-auto py-3">
{items.map(item => (
<NavLink key={item.href} item={item} pathname={pathname} />
))}
</nav>
<div className="border-t-2 border-divider py-3">
<div className="flex items-center gap-2 px-4 py-2 mb-1">
<div className="w-8 h-8 rounded-full bg-accent flex items-center justify-center text-ground text-xs font-extrabold flex-shrink-0">
{initials(displayName)}
</div>
<div className="min-w-0">
<p className="text-sm font-extrabold text-ink truncate">{displayName}</p>
<p className="text-xs text-ink/60 truncate">{roleLabel}</p>
</div>
</div>
<Link
href="/account"
aria-current={accountActive ? 'page' : undefined}
className={`${LINK_BASE} ${accountActive ? LINK_ACTIVE : LINK_IDLE}`}
>
<span className={accountActive ? 'text-accent-700' : 'text-ink/50'}>
<IconUser />
</span>
Account
</Link>
<button
onClick={logout}
className={`${LINK_BASE} ${LINK_IDLE} w-full text-left cursor-pointer`}
>
<span className="text-ink/50"><IconLogout /></span>
Logout
</button>
<div className="mt-3 pt-3 border-t border-divider px-4">
<p className="text-xs text-ink/50">v1.2.2</p>
</div>
</div>
</aside>
{/* Mobile bottom tab bar */}
<nav className="sm:hidden fixed bottom-0 left-0 right-0 bg-ground border-t-2 border-divider flex z-40 safe-area-pb">
{mobileItems.map(item => {
const active = isActivePath(item.href, pathname)
return (
<Link
key={item.href}
href={item.href}
aria-current={active ? 'page' : undefined}
className={`flex-1 flex flex-col items-center gap-1 py-2 text-[10px] font-extrabold transition-colors ${
active ? 'text-accent-700' : 'text-ink/55'
}`}
>
{item.icon}
<span className="leading-none">{item.label}</span>
</Link>
)
})}
{items.length <= 4 && (
<button
onClick={logout}
className="flex-1 flex flex-col items-center gap-1 py-2 text-[10px] font-extrabold text-ink/55 cursor-pointer"
>
<IconLogout />
<span className="leading-none">Logout</span>
</button>
)}
</nav>
</>
)
}