import React, { useState, useEffect } from 'react'; import { Link, useLocation, useNavigate } from 'react-router-dom'; import { useAuth } from '../context/AuthContext'; import api from '../utils/api'; import { Badge } from './ui/badge'; import { LayoutDashboard, UserCog, Users, CheckCircle, CreditCard, Calendar, Shield, Settings, ChevronLeft, ChevronRight, LogOut, Menu, Image, FileText, DollarSign, Scale, HardDrive, Repeat, Heart } from 'lucide-react'; const AdminSidebar = ({ isOpen, onToggle, isMobile }) => { const location = useLocation(); const navigate = useNavigate(); const { user, logout } = useAuth(); const [pendingCount, setPendingCount] = useState(0); const [storageUsed, setStorageUsed] = useState(0); const [storageLimit, setStorageLimit] = useState(0); const [storagePercentage, setStoragePercentage] = useState(0); // Fetch pending approvals count useEffect(() => { const fetchPendingCount = async () => { try { const response = await api.get('/admin/users'); const pending = response.data.filter(u => ['pending_email', 'pending_validation', 'pre_validated', 'payment_pending'].includes(u.status) ); setPendingCount(pending.length); } catch (error) { console.error('Failed to fetch pending count:', error); } }; fetchPendingCount(); // Refresh count every 30 seconds const interval = setInterval(fetchPendingCount, 30000); return () => clearInterval(interval); }, []); // Fetch storage usage useEffect(() => { const fetchStorageUsage = async () => { try { const response = await api.get('/admin/storage/usage'); setStorageUsed(response.data.total_bytes_used); setStorageLimit(response.data.max_bytes_allowed); setStoragePercentage(response.data.percentage); } catch (error) { console.error('Failed to fetch storage usage:', error); } }; fetchStorageUsage(); // Refresh storage usage every 60 seconds const interval = setInterval(fetchStorageUsage, 60000); return () => clearInterval(interval); }, []); const formatBytes = (bytes) => { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; }; const handleLogout = () => { logout(); navigate('/login'); }; const navItems = [ { name: 'Dashboard', icon: LayoutDashboard, path: '/admin', disabled: false }, { name: 'Staff', icon: UserCog, path: '/admin/staff', disabled: false }, { name: 'Members', icon: Users, path: '/admin/members', disabled: false }, { name: 'Validations', icon: CheckCircle, path: '/admin/validations', disabled: false, badge: pendingCount }, { name: 'Plans', icon: CreditCard, path: '/admin/plans', disabled: false }, { name: 'Subscriptions', icon: Repeat, path: '/admin/subscriptions', disabled: false }, { name: 'Donations', icon: Heart, path: '/admin/donations', disabled: false }, { name: 'Events', icon: Calendar, path: '/admin/events', disabled: false }, { name: 'Gallery', icon: Image, path: '/admin/gallery', disabled: false }, { name: 'Newsletters', icon: FileText, path: '/admin/newsletters', disabled: false }, { name: 'Financials', icon: DollarSign, path: '/admin/financials', disabled: false }, { name: 'Bylaws', icon: Scale, path: '/admin/bylaws', disabled: false }, { name: 'Permissions', icon: Shield, path: '/admin/permissions', disabled: false, superadminOnly: true } ]; // Filter nav items based on user role const filteredNavItems = navItems.filter(item => { if (item.superadminOnly && user?.role !== 'superadmin') { return false; } return true; }); const isActive = (path) => { if (path === '/admin') { return location.pathname === '/admin'; } return location.pathname.startsWith(path); }; const renderNavItem = (item) => { if (!item) return null; const Icon = item.icon; const active = isActive(item.path); return (
{ if (item.disabled) { e.preventDefault(); } }} className={` flex items-center gap-3 px-4 py-3 rounded-lg transition-all relative ${item.disabled ? 'opacity-50 cursor-not-allowed text-[#664fa3]' : active ? 'bg-[#ff9e77]/10 text-[#ff9e77]' : 'text-[#422268] hover:bg-[#DDD8EB]/20' } `} > {/* Active border */} {active && !item.disabled && (
)} {isOpen && ( <> {item.name} {item.disabled && ( Soon )} {item.badge > 0 && !item.disabled && ( {item.badge} )} )} {/* Badge when collapsed */} {!isOpen && item.badge > 0 && !item.disabled && (
{item.badge}
)} {/* Tooltip when collapsed */} {!isOpen && (
{item.name} {item.badge > 0 && ` (${item.badge})`}
)}
); }; return ( <> {/* Sidebar */} ); }; export default AdminSidebar;