124 lines
4.8 KiB
TypeScript
124 lines
4.8 KiB
TypeScript
'use client';
|
|
|
|
import React, { useEffect, useState, useMemo } from 'react';
|
|
import Link from 'next/link';
|
|
import { usePathname } from 'next/navigation';
|
|
import { Calendar, ChevronDown, RefreshCw, LayoutDashboard, Briefcase, Store, Wrench, Building2, Package } from 'lucide-react';
|
|
import { cn } from '@/lib/utils';
|
|
import { getAccessToken, parseJwt } from '@/services/api/client';
|
|
import { isRouteAllowed } from '@/lib/rbac';
|
|
|
|
interface DashboardHeaderNavProps {
|
|
title?: string;
|
|
refreshing?: boolean;
|
|
onRefresh?: () => void;
|
|
}
|
|
|
|
export const DASHBOARD_TABS = [
|
|
{ id: 'main', label: 'Main Overview', href: '/dashboard', icon: LayoutDashboard },
|
|
{ id: 'executive', label: 'Executive C-Suite', href: '/dashboard/executive', icon: Briefcase },
|
|
{ id: 'store_manager', label: 'Store Manager', href: '/dashboard/store-manager', icon: Store },
|
|
{ id: 'technician', label: 'Technician Workshop', href: '/dashboard/technician', icon: Wrench },
|
|
{ id: 'b2b', label: 'B2B Wholesale', href: '/dashboard/b2b', icon: Building2 },
|
|
{ id: 'products', label: 'Product Analytics', href: '/products/dashboard', icon: Package },
|
|
];
|
|
|
|
export function DashboardHeaderNav({
|
|
title = 'Multi-Dashboard Suite',
|
|
refreshing = false,
|
|
onRefresh,
|
|
}: DashboardHeaderNavProps) {
|
|
const pathname = usePathname();
|
|
const [userRole, setUserRole] = useState<string>(() => {
|
|
if (typeof window !== 'undefined') {
|
|
const token = getAccessToken();
|
|
if (token) {
|
|
const payload = parseJwt(token);
|
|
if (payload?.role) return String(payload.role);
|
|
}
|
|
}
|
|
return '';
|
|
});
|
|
|
|
useEffect(() => {
|
|
const token = getAccessToken();
|
|
if (token) {
|
|
const payload = parseJwt(token);
|
|
if (payload?.role) {
|
|
setUserRole(String(payload.role));
|
|
}
|
|
}
|
|
}, [pathname]);
|
|
|
|
const visibleTabs = useMemo(() => {
|
|
const effectiveRole = userRole || (typeof window !== 'undefined' && getAccessToken() ? String(parseJwt(getAccessToken()!)?.role || '') : '');
|
|
if (!effectiveRole) return [];
|
|
const roleLower = effectiveRole.toLowerCase().trim();
|
|
if (roleLower === 'super admin' || roleLower === 'super_admin' || roleLower === 'superadmin') {
|
|
return DASHBOARD_TABS;
|
|
}
|
|
return DASHBOARD_TABS.filter((tab) => isRouteAllowed(tab.href, effectiveRole));
|
|
}, [userRole]);
|
|
|
|
const isTabActive = (href: string) => {
|
|
if (href === '/dashboard') {
|
|
return pathname === '/dashboard';
|
|
}
|
|
return pathname === href || pathname.startsWith(href + '/');
|
|
};
|
|
|
|
return (
|
|
<div className="flex flex-col gap-3">
|
|
{/* Top Header Row */}
|
|
<div className="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3 min-w-0">
|
|
<h1 className="text-base sm:text-lg font-semibold text-foreground tracking-tight min-w-0">{title}</h1>
|
|
<div className="flex items-center gap-2 text-[13px] flex-wrap min-w-0">
|
|
<div className="flex items-center gap-1.5 h-9 max-w-full px-3 bg-card border border-border crm-radius-control text-muted-foreground min-w-0">
|
|
<Calendar className="w-4 h-4 text-primary shrink-0" />
|
|
<span className="truncate">Live Store & ERP Operations</span>
|
|
<ChevronDown className="w-3.5 h-3.5 shrink-0" />
|
|
</div>
|
|
{onRefresh && (
|
|
<button
|
|
type="button"
|
|
onClick={onRefresh}
|
|
className={cn(
|
|
'w-9 h-9 bg-card border border-border crm-radius-control text-muted-foreground hover:text-foreground hover:bg-muted cursor-pointer transition-colors flex items-center justify-center',
|
|
refreshing && 'animate-spin'
|
|
)}
|
|
aria-label="Refresh dashboard"
|
|
title="Refresh dashboard metrics"
|
|
>
|
|
<RefreshCw className="w-4 h-4" />
|
|
</button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Role Navigation Tabs - Only show tabs authorized for this user's role */}
|
|
{visibleTabs.length > 0 && (
|
|
<div className="flex items-center gap-2 border-b border-border pb-3 overflow-x-auto no-scrollbar">
|
|
{visibleTabs.map((tab) => {
|
|
const active = isTabActive(tab.href);
|
|
const Icon = tab.icon;
|
|
return (
|
|
<Link
|
|
key={tab.id}
|
|
href={tab.href}
|
|
className={cn(
|
|
'inline-flex items-center gap-2 px-3.5 py-1.5 rounded-lg text-xs font-semibold transition-all shrink-0 select-none',
|
|
active
|
|
? 'bg-primary text-white shadow-xs'
|
|
: 'bg-card border border-border text-muted-foreground hover:text-foreground hover:bg-muted'
|
|
)}
|
|
>
|
|
<Icon className={cn('w-3.5 h-3.5', active ? 'text-white' : 'text-muted-foreground')} />
|
|
<span>{tab.label}</span>
|
|
</Link>
|
|
);
|
|
})}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|