'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; subtitle?: string; userName?: 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', subtitle, userName = 'Admin', refreshing = false, onRefresh, }: DashboardHeaderNavProps) { const pathname = usePathname(); const [userRole, setUserRole] = useState(() => { 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 (
{/* Top Header Row */}

{title}

{subtitle ? subtitle : ( <>Welcome back, {userName} )}

Live Store & ERP Operations
{onRefresh && ( )}
{/* Role Navigation Tabs - Only show tabs authorized for this user's role */} {visibleTabs.length > 0 && (
{visibleTabs.map((tab) => { const active = isTabActive(tab.href); const Icon = tab.icon; return ( {tab.label} ); })}
)}
); }