'use client' import { useState } from 'react' import Link from 'next/link' import Image from 'next/image' import { usePathname } from 'next/navigation' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { cn } from '@/lib/utils' import { LayoutDashboard, Users, Network, Wallet, ShoppingBag, BarChart3, CreditCard, User, Settings, LogOut, ChevronLeft, ChevronRight, Trophy, Gift, TrendingUp, Package } from 'lucide-react' import { motion, AnimatePresence } from 'framer-motion' import { useSession, signOut } from 'next-auth/react' interface SidebarItem { title: string href: string icon: React.ComponentType<{ className?: string }> badge?: string children?: SidebarItem[] roles?: string[] // Allowed roles for this item } // Role-based navigation items const getSidebarItems = (userRole: string): SidebarItem[] => { const allItems: SidebarItem[] = [ { title: 'Dashboard', href: '/dashboard', icon: LayoutDashboard, roles: ['CUSTOMER', 'MEMBER', 'WHOLESALER', 'PART_TIME', 'ADMIN'] }, { title: 'My Orders', href: '/dashboard/orders', icon: ShoppingBag, roles: ['CUSTOMER', 'MEMBER', 'WHOLESALER', 'PART_TIME', 'ADMIN'] }, { title: 'Network', href: '', icon: Network, roles: ['MEMBER', 'WHOLESALER'], children: [ { title: 'Team Genealogy', href: '/dashboard/genealogy', icon: Users, roles: ['MEMBER', 'WHOLESALER'] }, { title: 'My Referrals', href: '/dashboard/referrals', icon: Gift, roles: ['MEMBER', 'WHOLESALER'] }, { title: 'Team Overview', href: '/dashboard/team', icon: TrendingUp, roles: ['MEMBER', 'WHOLESALER'] }, ] }, { title: 'Achievements', href: '/dashboard/achievements', icon: Trophy, roles: ['MEMBER', 'WHOLESALER'] }, { title: 'Wallet & Earnings', href: '/dashboard/wallet', icon: Wallet, roles: ['MEMBER', 'WHOLESALER', 'PART_TIME'], children: [ { title: 'My Wallet', href: '/dashboard/wallet', icon: Wallet, roles: ['MEMBER', 'WHOLESALER', 'PART_TIME'] }, { title: 'Commission History', href: '/dashboard/commissions', icon: TrendingUp, roles: ['MEMBER', 'WHOLESALER'] }, { title: 'Earnings', href: '/dashboard/earnings', icon: TrendingUp, roles: ['PART_TIME'] }, { title: 'Payout Requests', href: '/dashboard/payouts', icon: CreditCard, roles: ['MEMBER', 'WHOLESALER', 'PART_TIME'] }, ] }, { title: 'Wholesale Portal', href: '/dashboard/wholesale', icon: Package, roles: ['WHOLESALER'] }, { title: 'Part-Time Jobs', href: '/dashboard/part-time', icon: Users, roles: ['PART_TIME'] }, { title: 'Reports & Analytics', href: '/dashboard/reports', icon: BarChart3, roles: ['MEMBER', 'ADMIN'] }, { title: 'Profile Settings', href: '/dashboard/profile', icon: User, roles: ['CUSTOMER', 'MEMBER', 'WHOLESALER', 'PART_TIME', 'ADMIN'] }, ] // Filter items based on user role const filterItemsByRole = (items: SidebarItem[]): SidebarItem[] => { return items .filter(item => !item.roles || item.roles.includes(userRole)) .map(item => ({ ...item, children: item.children ? filterItemsByRole(item.children) : undefined })) .filter(item => !item.children || item.children.length > 0) // Remove parent items with no visible children } return filterItemsByRole(allItems) } // Role display information const getRoleInfo = (role: string) => { const roleMap: Record = { ADMIN: { label: 'Admin', description: 'Full system access', className: 'bg-purple-100 text-purple-800 border-purple-200' }, MEMBER: { label: 'Partner', description: 'Marketing partner', className: 'bg-blue-100 text-blue-800 border-blue-200' }, WHOLESALER: { label: 'Wholesaler', description: 'Bulk distribution partner', className: 'bg-yellow-100 text-yellow-800 border-yellow-200' }, PART_TIME: { label: 'Part-time', description: 'Delivery & support team', className: 'bg-green-100 text-green-800 border-green-200' }, CUSTOMER: { label: 'Customer', description: 'Valued customer', className: 'bg-gray-100 text-gray-800 border-gray-200' } } return roleMap[role] || roleMap.CUSTOMER } interface DashboardSidebarProps { className?: string } export function DashboardSidebar({ className }: DashboardSidebarProps) { const [isCollapsed, setIsCollapsed] = useState(false) const [expandedItems, setExpandedItems] = useState([]) const pathname = usePathname() const { data: session } = useSession() // Get user role from session, default to CUSTOMER const userRole = session?.user?.role || 'CUSTOMER' const roleInfo = getRoleInfo(userRole) // Get sidebar items based on user role const sidebarItems = getSidebarItems(userRole) const toggleExpanded = (title: string) => { setExpandedItems(prev => prev.includes(title) ? prev.filter(item => item !== title) : [...prev, title] ) } const isActive = (href: string) => { if (href === '/dashboard') { return pathname === '/dashboard' } return pathname.startsWith(href) } const hasActiveChild = (children?: SidebarItem[]) => { if (!children) return false return children.some(child => isActive(child.href)) } return (
{/* Header */}
{!isCollapsed && (
Padmaaja Logo
Padmaaja Rasooi
)}
{/* User Profile */} {!isCollapsed && session && (
{session.user.image ? ( User Avatar ) : ( )}

{session.user.name || 'User'}

{roleInfo.label}

{roleInfo.description}

)}
{/* Navigation */} {/* Footer Actions */}
{!isCollapsed && ( View Site )} {session?.user?.role === 'ADMIN' && ( {!isCollapsed && ( Admin Panel )} )}
) }