From 9ba708ecb7b45327c253ccac7e9e6a039b2cf22c Mon Sep 17 00:00:00 2001 From: weeihan Date: Thu, 1 Oct 2026 09:31:12 +0800 Subject: [PATCH] feat(design): add AppShell, MobileShell and the notifications drawer Co-Authored-By: Claude Haiku 4.5 --- app/(protected)/layout.tsx | 15 +- components/layout/app-shell.tsx | 135 +++++++++++++ components/layout/mobile-shell.tsx | 53 +++++ components/layout/nav-items.ts | 59 ++++++ components/layout/sidebar.tsx | 202 ------------------- components/notifications/bell.tsx | 143 +++++++------ tests/components/layout/app-shell.test.tsx | 77 +++++++ tests/components/layout/nav-items.test.ts | 24 +++ tests/components/layout/sidebar.test.tsx | 41 ---- tests/components/notifications/bell.test.tsx | 44 ++++ 10 files changed, 482 insertions(+), 311 deletions(-) create mode 100644 components/layout/app-shell.tsx create mode 100644 components/layout/mobile-shell.tsx create mode 100644 components/layout/nav-items.ts delete mode 100644 components/layout/sidebar.tsx create mode 100644 tests/components/layout/app-shell.test.tsx create mode 100644 tests/components/layout/nav-items.test.ts delete mode 100644 tests/components/layout/sidebar.test.tsx create mode 100644 tests/components/notifications/bell.test.tsx diff --git a/app/(protected)/layout.tsx b/app/(protected)/layout.tsx index 1978f5b..9550c54 100644 --- a/app/(protected)/layout.tsx +++ b/app/(protected)/layout.tsx @@ -2,7 +2,7 @@ export const dynamic = 'force-dynamic' import { redirect } from 'next/navigation' import { getSession } from '@/lib/auth/get-session' -import { Sidebar } from '@/components/layout/sidebar' +import { AppShell } from '@/components/layout/app-shell' export default async function ProtectedLayout({ children, @@ -13,15 +13,8 @@ export default async function ProtectedLayout({ if (!session) redirect('/login') return ( -
- -
- {children} -
-
+ + {children} + ) } diff --git a/components/layout/app-shell.tsx b/components/layout/app-shell.tsx new file mode 100644 index 0000000..8586c55 --- /dev/null +++ b/components/layout/app-shell.tsx @@ -0,0 +1,135 @@ +'use client' + +import { useEffect, useRef } from 'react' +import Link from 'next/link' +import { usePathname, useRouter } from 'next/navigation' +import { LogOut, Search, ShieldCheck, User } from 'lucide-react' +import { NotificationBell } from '@/components/notifications/bell' +import { Avatar } from '@/components/ui/avatar' +import { cx } from '@/components/ui/cx' +import { MobileShell } from './mobile-shell' +import { isActivePath, navItemsFor, searchHrefFor } from './nav-items' + +const ITEM = 'flex items-center gap-2.5 h-8 px-2.5 rounded-md text-[13px] w-full text-left' +const ITEM_ACTIVE = 'bg-surface text-ink font-semibold shadow-chip' +const ITEM_IDLE = 'text-ink-3 hover:bg-rule-2' + +export function AppShell({ + role, + userName, + children, +}: { + role: string + userName: string + children: React.ReactNode +}) { + const pathname = usePathname() + const router = useRouter() + const items = navItemsFor(role) + const searchHref = searchHrefFor(role) + const searchRef = useRef(null) + const displayName = userName || 'User' + const roleLabel = role.replace(/_/g, ' ').replace(/\b\w/g, c => c.toUpperCase()) + + useEffect(() => { + const onKey = (e: KeyboardEvent) => { + if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') { + e.preventDefault() + searchRef.current?.focus() + } + } + document.addEventListener('keydown', onKey) + return () => document.removeEventListener('keydown', onKey) + }, []) + + const logout = async () => { + await fetch('/api/auth/logout', { method: 'POST' }) + router.push('/login') + router.refresh() + } + + const accountActive = pathname === '/account' + + return ( +
+
+
+ + + + IMS + Setia HSE +
+ + {searchHref ? ( +
+ +
+ ) : ( +
+ )} + +
+ +
+ + {displayName} +
+
+
+ + + +
{children}
+ + +
+ ) +} diff --git a/components/layout/mobile-shell.tsx b/components/layout/mobile-shell.tsx new file mode 100644 index 0000000..4dcaa7c --- /dev/null +++ b/components/layout/mobile-shell.tsx @@ -0,0 +1,53 @@ +'use client' + +import Link from 'next/link' +import { LogOut } from 'lucide-react' +import { isActivePath, type NavItem } from './nav-items' +import { cx } from '@/components/ui/cx' + +export function MobileShell({ + items, + pathname, + onLogout, +}: { + items: NavItem[] + pathname: string + onLogout: () => void +}) { + const tabs = items.slice(0, 4) + return ( + + ) +} diff --git a/components/layout/nav-items.ts b/components/layout/nav-items.ts new file mode 100644 index 0000000..d16a419 --- /dev/null +++ b/components/layout/nav-items.ts @@ -0,0 +1,59 @@ +import { + AlignLeft, + CircleCheck, + ClipboardList, + LayoutDashboard, + Plus, + Settings, + Users, + type LucideIcon, +} from 'lucide-react' + +export interface NavItem { + label: string + href: string + icon: LucideIcon +} + +const NAV_ITEMS: Record = { + reporter: [ + { label: 'My Reports', href: '/reporter', icon: ClipboardList }, + { label: 'New Incident', href: '/report', icon: Plus }, + ], + supervisor: [ + { label: 'Overview', href: '/supervisor', icon: AlignLeft }, + { label: 'Incidents', href: '/supervisor/incidents', icon: ClipboardList }, + ], + hse: [ + { label: 'Dashboard', href: '/hse/dashboard', icon: LayoutDashboard }, + { label: 'Incidents', href: '/hse/incidents', icon: ClipboardList }, + { label: 'CAPA', href: '/hse/capa', icon: CircleCheck }, + { label: 'Settings', href: '/hse/settings', icon: Settings }, + ], + management: [{ label: 'Dashboard', href: '/management', icon: LayoutDashboard }], + capa_owner: [{ label: 'My CAPAs', href: '/capa-owner', icon: CircleCheck }], + admin: [ + { label: 'Admin', href: '/admin', icon: Users }, + { label: 'Dashboard', href: '/hse/dashboard', icon: LayoutDashboard }, + { label: 'Incidents', href: '/hse/incidents', icon: ClipboardList }, + { label: 'CAPA', href: '/hse/capa', icon: CircleCheck }, + { label: 'Management', href: '/management', icon: AlignLeft }, + { label: 'Settings', href: '/hse/settings', icon: Settings }, + ], +} + +const EXACT_ROOTS = ['/reporter', '/supervisor', '/management', '/capa-owner', '/admin', '/report', '/account'] + +export function navItemsFor(role: string): NavItem[] { + return NAV_ITEMS[role] ?? NAV_ITEMS.reporter +} + +export function isActivePath(href: string, pathname: string): boolean { + return EXACT_ROOTS.includes(href) ? pathname === href : pathname.startsWith(href) +} + +export function searchHrefFor(role: string): string | null { + if (role === 'hse' || role === 'admin') return '/hse/incidents' + if (role === 'supervisor') return '/supervisor/incidents' + return null +} diff --git a/components/layout/sidebar.tsx b/components/layout/sidebar.tsx deleted file mode 100644 index e1d8a72..0000000 --- a/components/layout/sidebar.tsx +++ /dev/null @@ -1,202 +0,0 @@ -'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 = () => -const IconList = () => -const IconCheck = () => -const IconChart = () => -const IconSettings = () => -const IconUsers = () => -const IconPlus = () => -const IconLogout = () => -const IconUser = () => - -const NAV_ITEMS: Record = { - reporter: [ - { label: 'My Reports', href: '/reporter', icon: }, - { label: 'New Incident', href: '/report', icon: }, - ], - supervisor: [ - { label: 'Overview', href: '/supervisor', icon: }, - { label: 'Incidents', href: '/supervisor/incidents', icon: }, - ], - hse: [ - { label: 'Dashboard', href: '/hse/dashboard', icon: }, - { label: 'Incidents', href: '/hse/incidents', icon: }, - { label: 'CAPA', href: '/hse/capa', icon: }, - { label: 'Settings', href: '/hse/settings', icon: }, - ], - management: [ - { label: 'Dashboard', href: '/management', icon: }, - ], - capa_owner: [ - { label: 'My CAPAs', href: '/capa-owner', icon: }, - ], - admin: [ - { label: 'Admin', href: '/admin', icon: }, - { label: 'Dashboard', href: '/hse/dashboard', icon: }, - { label: 'Incidents', href: '/hse/incidents', icon: }, - { label: 'CAPA', href: '/hse/capa', icon: }, - { label: 'Management', href: '/management', icon: }, - { label: 'Settings', href: '/hse/settings', icon: }, - ], -} - -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 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 ( - - {item.icon} - {item.label} - - ) -} - -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 */} - - - {/* Mobile bottom tab bar */} - - - ) -} diff --git a/components/notifications/bell.tsx b/components/notifications/bell.tsx index 2ba018a..62d3dc2 100644 --- a/components/notifications/bell.tsx +++ b/components/notifications/bell.tsx @@ -1,8 +1,10 @@ 'use client' -import { useCallback, useEffect, useRef, useState } from 'react' +import { useCallback, useEffect, useState } from 'react' import Link from 'next/link' import { Bell } from 'lucide-react' +import { Drawer } from '@/components/ui/drawer' +import { cx } from '@/components/ui/cx' type Notification = { id: string @@ -14,11 +16,15 @@ type Notification = { const POLL_MS = 60_000 +function isToday(iso: string): boolean { + return new Date(iso).toDateString() === new Date().toDateString() +} + export function NotificationBell() { const [notifications, setNotifications] = useState([]) const [unread, setUnread] = useState(0) const [open, setOpen] = useState(false) - const panelRef = useRef(null) + const [tab, setTab] = useState<'all' | 'unread'>('all') const load = useCallback(() => { fetch('/api/notifications') @@ -36,15 +42,6 @@ export function NotificationBell() { return () => clearInterval(id) }, [load]) - useEffect(() => { - if (!open) return - const onClick = (e: MouseEvent) => { - if (panelRef.current && !panelRef.current.contains(e.target as Node)) setOpen(false) - } - document.addEventListener('mousedown', onClick) - return () => document.removeEventListener('mousedown', onClick) - }, [open]) - const markAllRead = () => { fetch('/api/notifications', { method: 'POST', @@ -58,62 +55,94 @@ export function NotificationBell() { .catch(() => {}) } + const shown = tab === 'unread' ? notifications.filter(n => !n.read_at) : notifications + const groups: Array<{ label: string; items: Notification[] }> = [ + { label: 'Today', items: shown.filter(n => isToday(n.sent_at)) }, + { label: 'Earlier', items: shown.filter(n => !isToday(n.sent_at)) }, + ].filter(g => g.items.length > 0) + return ( -
+ <> - {open && ( -
-
- Notifications - {unread > 0 && ( - - )} -
- {notifications.length === 0 ? ( -

No notifications

- ) : ( -
    - {notifications.map(n => { - const inner = ( - <> -

    - {n.title ?? 'Notification'} -

    -

    - {new Date(n.sent_at).toLocaleString()} -

    - - ) - return ( -
  • - {n.link ? ( - setOpen(false)}> - {inner} - - ) : ( -
    {inner}
    - )} -
  • - ) - })} -
- )} + setOpen(false)} + title="Notifications" + actions={ + unread > 0 ? ( + + ) : undefined + } + > +
+ {(['all', 'unread'] as const).map(t => ( + + ))}
- )} -
+ + {groups.length === 0 ? ( +

No notifications

+ ) : ( + groups.map(g => ( +
+

{g.label}

+
    + {g.items.map(n => { + const body = ( + <> +

    + {n.title ?? 'Notification'} +

    +

    {new Date(n.sent_at).toLocaleString()}

    + + ) + return ( +
  • + {n.link ? ( + setOpen(false)}> + {body} + + ) : ( +
    {body}
    + )} +
  • + ) + })} +
+
+ )) + )} + + ) } diff --git a/tests/components/layout/app-shell.test.tsx b/tests/components/layout/app-shell.test.tsx new file mode 100644 index 0000000..833c81a --- /dev/null +++ b/tests/components/layout/app-shell.test.tsx @@ -0,0 +1,77 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest' +import { render, screen, fireEvent } from '@testing-library/react' + +const push = vi.fn() +const refresh = vi.fn() +vi.mock('next/navigation', () => ({ + usePathname: () => '/hse/capa', + useRouter: () => ({ push, refresh }), +})) +vi.mock('@/components/notifications/bell', () => ({ NotificationBell: () =>