'use client'; import { useEffect, useState, useMemo, type ComponentType } from 'react'; import Link from 'next/link'; import { usePathname, useRouter } from 'next/navigation'; import { parseJwt, getAccessToken } from '@/services/api/client'; import { isRouteAllowed, getDefaultDashboardForRole } from '@/lib/rbac'; import { LayoutDashboard, Users, Shield, ChevronLeft, ChevronDown, Wrench, Package, Megaphone, Warehouse, Receipt, BarChart3, FileSpreadsheet, Layers, } from 'lucide-react'; import { cn } from '@/lib/utils'; interface SidebarProps { collapsed: boolean; onToggle: () => void; overlay?: boolean; variant?: 'vertical' | 'horizontal'; detached?: boolean; } type NavChild = { name: string; href: string }; type NavGroup = { name: string; icon: ComponentType<{ className?: string }>; children: NavChild[]; }; const NAV_GROUPS: NavGroup[] = [ { name: 'Dashboard', icon: LayoutDashboard, children: [ { name: 'Main Overview', href: '/dashboard' }, { name: 'Executive C-Suite', href: '/dashboard/executive' }, { name: 'Store Manager', href: '/dashboard/store-manager' }, { name: 'Technician Workshop', href: '/dashboard/technician' }, { name: 'B2B Wholesale', href: '/dashboard/b2b' }, { name: 'Product Analytics', href: '/products/dashboard' }, ], }, { name: 'CRM & Sales Pipeline', icon: Users, children: [ { name: 'Leads', href: '/crm-leads' }, { name: 'Deals', href: '/crm-deals' }, { name: 'Quotations', href: '/crm-quotations' }, { name: 'Tasks', href: '/crm-todos' }, { name: 'Calendar', href: '/crm-calendar' }, { name: 'WhatsApp', href: '/whatsapp-web' }, { name: 'Customers', href: '/customers' }, { name: 'Repair customers', href: '/customers/service' }, ], }, { name: 'Catalog Management', icon: Package, children: [ { name: 'Product Categories', href: '/categories' }, { name: 'Brands', href: '/brands' }, { name: 'Device Series', href: '/series' }, { name: 'Device Models', href: '/models' }, { name: 'Products', href: '/products' }, { name: 'Master Attributes', href: '/attributes' }, { name: 'Bulk Product Import', href: '/migration' }, ], }, { name: 'Marketing', icon: Megaphone, children: [ { name: 'Google Merchant Center', href: '/merchant-center' }, { name: 'Social Media Publisher', href: '/social-media' }, { name: 'Google Analytics GA4', href: '/analytics/google' }, { name: 'Homepage Layout', href: '/storefront-sections' }, { name: 'Storefront CMS', href: '/storefront-cms' }, { name: 'Customer Reviews', href: '/reviews-moderation' }, ], }, { name: 'Inventory and Purchase', icon: Warehouse, children: [ { name: 'Stock Ledger', href: '/inventory' }, { name: 'Multi-Warehouse Routing', href: '/warehouses' }, { name: 'Supplier Purchase Orders', href: '/purchases' }, ], }, { name: 'Sales and Billings', icon: Receipt, children: [ { name: 'Customer Store Orders', href: '/orders' }, { name: 'Razorpay Dashboard', href: '/razorpay-dashboard' }, { name: 'Shiprocket Logistics', href: '/logistics' }, { name: 'Warranties', href: '/warranties' }, { name: 'Refunds & Returns', href: '/refunds' }, { name: 'Sales Invoices', href: '/invoices' }, { name: 'B2B Bulk Quotations', href: '/bulk-quotations' }, { name: 'POS Sessions & Cash', href: '/pos-sessions' }, { name: 'User POS Analytics', href: '/user-pos-analytics' }, ], }, { name: 'Repair and Services', icon: Wrench, children: [ { name: '5-Stage Repair Queue', href: '/repair-tickets' }, { name: 'Service Catalog Manager', href: '/service-catalog' }, { name: 'Service Jobs Queue', href: '/services' }, { name: 'Walk-In Device Queue', href: '/services/intake' }, { name: 'Technician Workspace', href: '/technician' }, { name: 'Service Quotations', href: '/service-quotes' }, { name: 'Service Invoices', href: '/service-invoices' }, ], }, { name: 'Reports & Intelligence', icon: FileSpreadsheet, children: [ { name: 'AI Prediction Lab (Swarm)', href: '/simulation' }, { name: 'Reporting Hub (Export)', href: '/reports' }, ], }, { name: 'User Management', icon: Users, children: [ { name: 'Manage Users', href: '/users' }, { name: 'Roles & Permissions', href: '/roles' }, { name: 'Staff Directory', href: '/staff-directory' }, { name: 'User Activity Logs', href: '/activity-logs' }, ], }, { name: 'Integrations Hub', icon: Layers, children: [ { name: 'All Integrations', href: '/integrations' }, { name: 'WhatsApp Hub (OpenWA & WATI)', href: '/integrations/whatsapp' }, { name: 'Transactional Email', href: '/integrations/email' }, { name: 'Google Analytics GA4', href: '/integrations/google-analytics' }, { name: 'Meta Ads & Graph API', href: '/integrations/meta-ads' }, { name: 'Knowlarity Telephony', href: '/integrations/call' }, { name: 'Shiprocket Logistics', href: '/integrations/shipping' }, ], }, { name: 'System Infrastructure', icon: Shield, children: [ { name: 'Store Configuration', href: '/store-config' }, { name: 'WhatsApp & Notification Gateway', href: '/notifications-gateway' }, { name: 'Notification Templates', href: '/notification-templates' }, { name: 'Security & Sessions', href: '/security' }, { name: 'System Settings', href: '/settings' }, ], }, ]; function pathMatches(href: string, pathname: string) { if (pathname === href) return true; if (href === '/integrations' && pathname === '/integrations') return true; if (href.startsWith('/integrations/') && pathname.startsWith(href)) return true; if (href === '/products' && pathname.startsWith('/products/') && pathname !== '/products/dashboard') { return true; } if (href === '/services' && pathname.startsWith('/services/') && !pathname.startsWith('/services/intake')) { return true; } if (href === '/crm-leads' && pathname.startsWith('/crm-leads')) return true; return false; } function groupHasActive(group: NavGroup, pathname: string) { return group.children.some((child) => pathMatches(child.href, pathname)); } export function Sidebar({ collapsed, onToggle, overlay = false, variant = 'vertical', detached = false }: SidebarProps) { const pathname = usePathname(); const router = useRouter(); const [mounted, setMounted] = useState(false); const [userRole, setUserRole] = useState(''); const [openGroups, setOpenGroups] = useState([]); useEffect(() => { setMounted(true); const token = getAccessToken(); if (token) { const payload = parseJwt(token); if (payload && typeof payload.role === 'string') { setUserRole(payload.role.toLowerCase()); } } else { setUserRole(''); } }, [pathname]); const groups = useMemo(() => { if (!mounted || !userRole) { return NAV_GROUPS; } return NAV_GROUPS.map((group) => { const allowedChildren = group.children.filter((child) => isRouteAllowed(child.href, userRole)); return { ...group, children: allowedChildren, }; }).filter((group) => group.children.length > 0); }, [userRole, mounted]); useEffect(() => { if (!mounted) return; const active = groups.find((group) => groupHasActive(group, pathname)); if (active) { setOpenGroups((current) => (current.includes(active.name) ? current : [...current, active.name])); } }, [pathname, userRole, mounted, groups]); const prefetchHref = (href: string) => { if (href !== pathname) router.prefetch(href); }; const toggleGroup = (name: string) => { if (variant === 'horizontal') { setOpenGroups((current) => (current.includes(name) ? [] : [name])); return; } if (collapsed) { onToggle(); setOpenGroups((current) => (current.includes(name) ? current : [...current, name])); return; } setOpenGroups((current) => current.includes(name) ? current.filter((item) => item !== name) : [...current, name] ); }; if (variant === 'horizontal') { return ( ); } return ( ); }