ifixkart-admin/components/dashboard/DashboardHeaderNav.tsx

135 lines
5.1 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;
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<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">
<div>
<h1 className="text-lg font-semibold text-foreground tracking-tight">{title}</h1>
<p className="text-[13px] text-muted-foreground mt-0.5">
{subtitle ? subtitle : (
<>Welcome back, <span className="capitalize font-medium text-foreground">{userName}</span></>
)}
</p>
</div>
<div className="flex items-center gap-2 text-[13px] flex-wrap">
<div className="flex items-center gap-1.5 h-9 px-3 bg-card border border-border crm-radius-control text-muted-foreground">
<Calendar className="w-4 h-4 text-primary" />
<span>Live Store &amp; ERP Operations</span>
<ChevronDown className="w-3.5 h-3.5" />
</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>
);
}