ifixkart-admin/components/layouts/Sidebar.tsx

315 lines
10 KiB
TypeScript

'use client';
import { useEffect, useState, type ComponentType } from 'react';
import Link from 'next/link';
import { usePathname, useRouter } from 'next/navigation';
import { parseJwt, getAccessToken } from '@/services/api/client';
import {
LayoutDashboard,
Users,
Shield,
ChevronLeft,
ChevronDown,
Wrench,
Package,
Megaphone,
Warehouse,
Receipt,
} from 'lucide-react';
import { cn } from '@/lib/utils';
interface SidebarProps {
collapsed: boolean;
onToggle: () => void;
overlay?: 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 Dashboard', href: '/dashboard' },
{ name: 'Product Dashboard', href: '/products/dashboard' },
],
},
{
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: '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: 'Supplier Purchase Orders', href: '/purchases' },
],
},
{
name: 'Sales and Billings',
icon: Receipt,
children: [
{ name: 'Customer Store Orders', href: '/orders' },
{ name: 'Sales Invoices', href: '/invoices' },
{ name: 'POS Synchronization', href: '/pos-sync' },
],
},
{
name: 'Repair and Services',
icon: Wrench,
children: [
{ 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: 'User Management',
icon: Users,
children: [
{ name: 'Manage Users', href: '/users' },
{ name: 'E-Commerce Customers', href: '/customers' },
{ name: 'Roles & Permissions', href: '/roles' },
{ name: 'Staff Directory', href: '/staff-directory' },
{ name: 'User Activity Logs', href: '/activity-logs' },
],
},
{
name: 'System Infrastructure',
icon: Shield,
children: [
{ name: 'Security & Sessions', href: '/security' },
{ name: 'System Settings', href: '/settings' },
],
},
];
function pathMatches(href: string, pathname: string) {
if (pathname === 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;
}
return false;
}
function groupHasActive(group: NavGroup, pathname: string) {
return group.children.some((child) => pathMatches(child.href, pathname));
}
export function Sidebar({ collapsed, onToggle, overlay = false }: SidebarProps) {
const pathname = usePathname();
const router = useRouter();
const [userRole, setUserRole] = useState('');
const [openGroups, setOpenGroups] = useState<string[]>([]);
useEffect(() => {
const token = getAccessToken();
if (token) {
const payload = parseJwt(token);
if (payload) {
setUserRole(((payload.role as string) || '').toLowerCase());
}
}
}, []);
const isSuperAdmin = userRole === 'super admin' || userRole === 'super_admin' || userRole === 'superadmin';
const isAdmin = userRole === 'admin' || isSuperAdmin;
const groups = NAV_GROUPS.filter((group) => {
if (group.name === 'System Infrastructure') return isSuperAdmin;
if (group.name === 'User Management') return isAdmin;
return true;
}).map((group) => {
if (group.name !== 'System Infrastructure') return group;
return {
...group,
children: group.children.filter((item) => {
if (item.name === 'System Settings') return isSuperAdmin;
return isSuperAdmin;
}),
};
});
useEffect(() => {
const active = groups.find((group) => groupHasActive(group, pathname));
if (active) {
setOpenGroups((current) => (current.includes(active.name) ? current : [...current, active.name]));
}
}, [pathname, userRole]);
useEffect(() => {
groups.forEach((group) => {
if (!openGroups.includes(group.name)) return;
group.children.forEach((child) => {
router.prefetch(child.href);
});
});
}, [openGroups, userRole, router]);
useEffect(() => {
const prefetchAll = () => {
groups.forEach((group) => {
group.children.forEach((child) => router.prefetch(child.href));
});
};
const idle = window.requestIdleCallback?.(prefetchAll);
if (idle) {
return () => window.cancelIdleCallback(idle);
}
const timeout = window.setTimeout(prefetchAll, 1000);
return () => window.clearTimeout(timeout);
}, [userRole, router]);
const toggleGroup = (name: string) => {
if (collapsed) {
onToggle();
setOpenGroups((current) => (current.includes(name) ? current : [...current, name]));
return;
}
setOpenGroups((current) =>
current.includes(name) ? current.filter((item) => item !== name) : [...current, name]
);
};
return (
<aside
className={cn(
'h-screen flex flex-col bg-card border-r border-border transition-[width] duration-200 ease-out shrink-0',
overlay && 'fixed left-0 top-0 z-20',
collapsed ? 'w-[70px] min-w-[70px] max-w-[70px]' : 'w-[260px] min-w-[260px] max-w-[260px]'
)}
>
<div
className={cn(
'relative flex items-center h-14 shrink-0 border-b border-border',
collapsed ? 'justify-center px-2' : 'justify-between px-4'
)}
>
<Link href="/dashboard" className="flex items-center gap-2.5 min-w-0">
<div className="w-8 h-8 crm-radius-icon flex items-center justify-center shrink-0 bg-primary">
<Wrench className="w-4 h-4 text-white" />
</div>
{!collapsed && (
<span className="text-[15px] font-semibold text-foreground tracking-tight truncate">iFixKart</span>
)}
</Link>
<button
type="button"
onClick={onToggle}
className={cn(
'w-[22px] h-[22px] rounded-full flex items-center justify-center hover:bg-muted cursor-pointer transition-colors border border-border bg-card',
collapsed && 'absolute -right-2.5 top-1/2 -translate-y-1/2 rotate-180 z-10'
)}
aria-label="Toggle sidebar"
>
<ChevronLeft className="w-3.5 h-3.5 text-muted-foreground" />
</button>
</div>
<nav className={cn('flex-1 overflow-y-auto py-3', collapsed ? 'px-2' : 'px-3')}>
<div className="flex flex-col gap-1">
{groups.map((group) => {
const Icon = group.icon;
const open = !collapsed && openGroups.includes(group.name);
const active = groupHasActive(group, pathname);
const highlighted = open || active;
return (
<div key={group.name}>
<button
type="button"
title={collapsed ? group.name : undefined}
onClick={() => toggleGroup(group.name)}
className={cn(
'w-full flex items-center crm-radius-section text-[13px] font-medium cursor-pointer transition-colors',
collapsed ? 'justify-center p-1' : 'gap-2.5 px-2 py-1.5',
highlighted ? 'bg-primary/10 text-primary' : 'text-foreground hover:bg-muted'
)}
>
<span
className={cn(
'w-8 h-8 crm-radius-icon flex items-center justify-center shrink-0',
highlighted ? 'bg-primary text-white' : 'bg-muted text-muted-foreground'
)}
>
<Icon className="w-4 h-4" />
</span>
{!collapsed && (
<>
<span className="flex-1 text-left truncate">{group.name}</span>
<ChevronDown className={cn('w-3.5 h-3.5 shrink-0 transition-transform', open && 'rotate-180')} />
</>
)}
</button>
{open && (
<div className="relative ml-[26px] mt-1 mb-1 pl-4">
<span className="absolute left-0 top-1 bottom-2 border-l border-dashed border-border" />
<div className="flex flex-col">
{group.children.map((child) => {
const childActive = pathMatches(child.href, pathname);
return (
<Link
key={child.href}
href={child.href}
prefetch={true}
className={cn(
'relative flex items-center gap-2.5 py-[7px] text-[13px] transition-colors min-w-0',
childActive
? 'text-primary font-semibold'
: 'text-muted-foreground hover:text-foreground'
)}
>
<span
className={cn(
'w-1.5 h-1.5 rounded-full shrink-0',
childActive ? 'bg-primary' : 'bg-[#c5cad3]'
)}
/>
<span className="truncate">{child.name}</span>
</Link>
);
})}
</div>
</div>
)}
</div>
);
})}
</div>
</nav>
</aside>
);
}