feat: persistent sidebar nav with role-scoped links, logout, and mobile bottom tab bar

This commit is contained in:
2026-07-12 16:40:57 +08:00
parent ba2cf69a8b
commit 180b0dc0a3
2 changed files with 233 additions and 6 deletions
+17 -6
View File
@@ -1,9 +1,8 @@
// app/(protected)/layout.tsx
export const dynamic = 'force-dynamic' export const dynamic = 'force-dynamic'
import { createClient } from '@/lib/supabase/server' import { createClient } from '@/lib/supabase/server'
import { redirect } from 'next/navigation' import { redirect } from 'next/navigation'
import { NotificationBell } from '@/components/notifications/bell' import { Sidebar } from '@/components/layout/sidebar'
export default async function ProtectedLayout({ export default async function ProtectedLayout({
children, children,
@@ -17,10 +16,22 @@ export default async function ProtectedLayout({
if (!user) redirect('/login') if (!user) redirect('/login')
const { data: profile } = await supabase
.from('users')
.select('role, name, email')
.eq('id', user.id)
.single()
return ( return (
<> <div className="min-h-screen bg-gray-50">
<NotificationBell /> <Sidebar
{children} role={profile?.role ?? 'reporter'}
</> userName={profile?.name ?? user.email ?? ''}
userEmail={profile?.email ?? user.email ?? ''}
/>
<div className="md:ml-60 pb-16 md:pb-0">
{children}
</div>
</div>
) )
} }
+216
View File
@@ -0,0 +1,216 @@
'use client'
import Link from 'next/link'
import { usePathname, useRouter } from 'next/navigation'
import { createBrowserClient } from '@supabase/ssr'
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 = () => (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 6h16M4 12h16M4 18h7" />
</svg>
)
const IconList = () => (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2" />
</svg>
)
const IconCheck = () => (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
)
const IconChart = () => (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z" />
</svg>
)
const IconSettings = () => (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
)
const IconUsers = () => (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4.354a4 4 0 110 5.292M15 21H3v-1a6 6 0 0112 0v1zm0 0h6v-1a6 6 0 00-9-5.197M13 7a4 4 0 11-8 0 4 4 0 018 0z" />
</svg>
)
const IconPlus = () => (
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 4v16m8-8H4" />
</svg>
)
const IconLogout = () => (
<svg className="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1" />
</svg>
)
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 /> },
],
}
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
}
function NavLink({ item, pathname }: { item: NavItem; pathname: string }) {
// Exact match for root-level single-page roles, prefix match otherwise
const exactRoots = ['/reporter', '/supervisor', '/management', '/capa-owner', '/admin', '/report']
const isActive = exactRoots.includes(item.href)
? pathname === item.href
: pathname.startsWith(item.href)
return (
<Link
href={item.href}
className={`flex items-center gap-3 px-3 py-2 rounded-lg text-sm font-medium transition-colors ${
isActive
? 'bg-blue-50 text-blue-700'
: 'text-gray-600 hover:bg-gray-100 hover:text-gray-900'
}`}
>
<span className={isActive ? 'text-blue-600' : 'text-gray-400'}>{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 () => {
const supabase = createBrowserClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
)
await supabase.auth.signOut()
router.push('/login')
}
const displayName = userName || userEmail || 'User'
const roleLabel = role.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase())
return (
<>
{/* Desktop sidebar */}
<aside className="hidden md:flex fixed left-0 top-0 h-full w-60 bg-white border-r border-gray-200 flex-col z-40">
{/* Header */}
<div className="flex items-center justify-between px-4 py-4 border-b border-gray-100">
<span className="text-base font-bold text-gray-900 tracking-tight">IMS</span>
<NotificationBell />
</div>
{/* Nav */}
<nav className="flex-1 overflow-y-auto px-3 py-3 space-y-1">
{items.map(item => (
<NavLink key={item.href} item={item} pathname={pathname} />
))}
</nav>
{/* User + logout */}
<div className="border-t border-gray-100 px-3 py-3">
<div className="flex items-center gap-2 px-2 py-2 mb-1">
<div className="w-8 h-8 rounded-full bg-blue-600 flex items-center justify-center text-white text-xs font-bold flex-shrink-0">
{initials(displayName)}
</div>
<div className="min-w-0">
<p className="text-sm font-medium text-gray-900 truncate">{displayName}</p>
<p className="text-xs text-gray-500 truncate">{roleLabel}</p>
</div>
</div>
<button
onClick={logout}
className="flex items-center gap-2 w-full px-3 py-2 text-sm text-gray-600 hover:bg-gray-100 hover:text-gray-900 rounded-lg transition-colors"
>
<IconLogout />
Logout
</button>
</div>
</aside>
{/* Mobile bottom tab bar */}
<nav className="md:hidden fixed bottom-0 left-0 right-0 bg-white border-t border-gray-200 flex z-40 safe-area-pb">
{mobileItems.map(item => {
const exactRoots = ['/reporter', '/supervisor', '/management', '/capa-owner', '/admin', '/report']
const isActive = exactRoots.includes(item.href)
? pathname === item.href
: pathname.startsWith(item.href)
return (
<Link
key={item.href}
href={item.href}
className={`flex-1 flex flex-col items-center gap-1 py-2 text-[10px] font-medium transition-colors ${
isActive ? 'text-blue-600' : 'text-gray-400'
}`}
>
{item.icon}
<span className="leading-none">{item.label}</span>
</Link>
)
})}
{/* Mobile logout in "More" slot if room */}
{items.length <= 4 && (
<button
onClick={logout}
className="flex-1 flex flex-col items-center gap-1 py-2 text-[10px] font-medium text-gray-400"
>
<IconLogout />
<span className="leading-none">Logout</span>
</button>
)}
</nav>
</>
)
}