190 lines
6.5 KiB
TypeScript
190 lines
6.5 KiB
TypeScript
import React, { useState } from 'react'
|
|
import { createFileRoute, Outlet, useNavigate, useLocation, Navigate } from '@tanstack/react-router'
|
|
import {
|
|
LayoutDashboard,
|
|
Package,
|
|
DollarSign,
|
|
Group,
|
|
Images,
|
|
CalendarClock,
|
|
TriangleAlert,
|
|
ShoppingBag,
|
|
TicketPercent,
|
|
Code2,
|
|
Store,
|
|
Users,
|
|
Megaphone,
|
|
LogOut,
|
|
Menu,
|
|
ArrowLeft,
|
|
RefreshCw,
|
|
UserRound,
|
|
} from 'lucide-react'
|
|
import { p as MyText } from 'web-components'
|
|
import { useStaffAuth } from '../components/context/staff-auth-context'
|
|
import { EventBus, REFRESH_EVENT } from '../lib/event-bus'
|
|
|
|
export const Route = createFileRoute('/dashboard')({
|
|
component: DashboardShell,
|
|
})
|
|
|
|
const SCREEN_TITLES: Record<string, string> = {
|
|
products: 'Products',
|
|
prices: 'Prices Overview',
|
|
'product-groupings': 'Product Groupings',
|
|
banners: 'Dashboard Banners',
|
|
slots: 'Slots',
|
|
complaints: 'Complaints',
|
|
orders: 'Manage Orders',
|
|
coupons: 'Coupons',
|
|
'vendor-snippets': 'Vendor Snippets',
|
|
stores: 'Stores',
|
|
users: 'User Management',
|
|
notifications: 'Send Notifications',
|
|
'product-tags': 'Product Tags',
|
|
rebalance: 'Rebalance Orders',
|
|
'customize-app': 'Customize App',
|
|
}
|
|
|
|
interface SidebarEntry {
|
|
label: string
|
|
to: string
|
|
icon: React.ReactNode
|
|
action?: 'logout'
|
|
}
|
|
|
|
const SIDEBAR_ENTRIES: SidebarEntry[] = [
|
|
{ label: 'Dashboard', to: '/dashboard', icon: <LayoutDashboard size={22} /> },
|
|
{ label: 'Products', to: '/dashboard/products', icon: <Package size={22} /> },
|
|
{ label: 'Prices Overview', to: '/dashboard/prices', icon: <DollarSign size={22} /> },
|
|
{ label: 'Product Groupings', to: '/dashboard/product-groupings', icon: <Group size={22} /> },
|
|
{ label: 'Dashboard Banners', to: '/dashboard/banners', icon: <Images size={22} /> },
|
|
{ label: 'Slots', to: '/dashboard/slots', icon: <CalendarClock size={22} /> },
|
|
{ label: 'Complaints', to: '/dashboard/complaints', icon: <TriangleAlert size={22} /> },
|
|
{ label: 'Manage Orders', to: '/dashboard/orders', icon: <ShoppingBag size={22} /> },
|
|
{ label: 'Coupons', to: '/dashboard/coupons', icon: <TicketPercent size={22} /> },
|
|
{ label: 'Vendor Snippets', to: '/dashboard/vendor-snippets', icon: <Code2 size={22} /> },
|
|
{ label: 'Stores', to: '/dashboard/stores', icon: <Store size={22} /> },
|
|
{ label: 'User Management', to: '/dashboard/users', icon: <Users size={22} /> },
|
|
{ label: 'Send Notifications', to: '/dashboard/notifications', icon: <Megaphone size={22} /> },
|
|
{ label: 'Logout', to: '/login', icon: <LogOut size={22} />, action: 'logout' },
|
|
]
|
|
|
|
// Top-level feature roots show the menu toggle; deeper screens show back.
|
|
const FEATURE_ROOTS = new Set([
|
|
'/dashboard',
|
|
...SIDEBAR_ENTRIES.filter((e) => !e.action).map((e) => e.to),
|
|
])
|
|
|
|
function DashboardShell() {
|
|
const { isLoggedIn, isLoading, staff, logout } = useStaffAuth()
|
|
const navigate = useNavigate()
|
|
const location = useLocation()
|
|
const [sidebarOpen, setSidebarOpen] = useState(false)
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="flex min-h-screen items-center justify-center bg-white">
|
|
<div className="h-8 w-8 animate-spin rounded-full border-2 border-gray-300 border-t-blue-500" />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
if (!isLoggedIn) {
|
|
return <Navigate to="/login" />
|
|
}
|
|
|
|
const pathname = location.pathname
|
|
const segment = pathname.split('/')[2] ?? ''
|
|
const title = segment ? (SCREEN_TITLES[segment] ?? 'Dashboard') : 'Dashboard'
|
|
const canGoBack = !FEATURE_ROOTS.has(pathname)
|
|
|
|
const goEntry = (entry: SidebarEntry) => {
|
|
if (entry.action === 'logout') {
|
|
logout()
|
|
} else if (entry.to === '/dashboard') {
|
|
navigate({ to: '/dashboard' })
|
|
} else {
|
|
navigate({ to: entry.to } as any)
|
|
}
|
|
setSidebarOpen(false)
|
|
}
|
|
|
|
const isActive = (to: string) =>
|
|
to === '/dashboard' ? pathname === '/dashboard' : pathname.startsWith(to)
|
|
|
|
const sidebar = (
|
|
<div className="flex h-full flex-col overflow-y-auto bg-white">
|
|
{staff && (
|
|
<div className="border-b border-gray-200 p-4">
|
|
<UserRound size={40} color="#3B82F6" />
|
|
<div className="mt-2">
|
|
<MyText className="text-lg font-semibold text-gray-800">{staff.name}</MyText>
|
|
<MyText className="text-sm text-gray-600">Staff Member</MyText>
|
|
</div>
|
|
</div>
|
|
)}
|
|
<nav className="flex-1 py-2">
|
|
{SIDEBAR_ENTRIES.map((entry) => (
|
|
<button
|
|
key={entry.label}
|
|
onClick={() => goEntry(entry)}
|
|
className={`flex w-full items-center gap-4 px-4 py-3 text-left text-[15px] font-medium transition-colors ${
|
|
!entry.action && isActive(entry.to)
|
|
? 'bg-blue-50 text-blue-600'
|
|
: 'text-gray-700 hover:bg-gray-100'
|
|
}`}
|
|
>
|
|
<span className={!entry.action && isActive(entry.to) ? 'text-blue-600' : 'text-gray-500'}>
|
|
{entry.icon}
|
|
</span>
|
|
{entry.label}
|
|
</button>
|
|
))}
|
|
</nav>
|
|
</div>
|
|
)
|
|
|
|
return (
|
|
<div className="min-h-screen bg-white md:grid md:grid-cols-[260px_1fr]">
|
|
{/* Desktop sidebar */}
|
|
<aside className="hidden border-r border-gray-200 md:block">{sidebar}</aside>
|
|
|
|
{/* Mobile drawer */}
|
|
{sidebarOpen && (
|
|
<div className="fixed inset-0 z-50 md:hidden">
|
|
<div className="absolute inset-0 bg-black/40" onClick={() => setSidebarOpen(false)} />
|
|
<aside className="absolute inset-y-0 left-0 w-[280px] bg-white shadow-xl">{sidebar}</aside>
|
|
</div>
|
|
)}
|
|
|
|
<div className="flex min-w-0 flex-col">
|
|
{/* Header — same chrome as admin-ui drawer header */}
|
|
<header
|
|
className="sticky top-0 z-40 flex items-center justify-between px-4 py-3"
|
|
style={{ backgroundColor: '#fdfdfd' }}
|
|
>
|
|
<button
|
|
onClick={() => (canGoBack ? window.history.back() : setSidebarOpen(true))}
|
|
className="flex h-10 w-10 items-center justify-center rounded-full bg-[#f2f2f2]"
|
|
aria-label={canGoBack ? 'Go back' : 'Open menu'}
|
|
>
|
|
{canGoBack ? <ArrowLeft size={24} color="black" /> : <Menu size={24} color="black" />}
|
|
</button>
|
|
<MyText className="text-lg font-semibold text-gray-900">{title}</MyText>
|
|
<button
|
|
onClick={() => EventBus.emit(REFRESH_EVENT)}
|
|
className="flex h-10 w-10 items-center justify-center rounded-full bg-[#f2f2f2]"
|
|
aria-label="Refresh"
|
|
>
|
|
<RefreshCw size={24} color="black" />
|
|
</button>
|
|
</header>
|
|
|
|
<main className="min-w-0 flex-1">
|
|
<Outlet />
|
|
</main>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|