import React, { useState, useEffect, useCallback, memo } from 'react'; import { NavLink, useLocation } from 'react-router-dom'; const Sidebar = ({ isCollapsed, toggleSidebar }) => { const [isMounted, setIsMounted] = useState(false); const [isLoading, setIsLoading] = useState(true); const [isHovered, setIsHovered] = useState(false); const [isAnimating, setIsAnimating] = useState(false); const [isMobile, setIsMobile] = useState(false); const [isTouchDevice, setIsTouchDevice] = useState(false); const [focusedIndex, setFocusedIndex] = useState(-1); const [isExpanded, setIsExpanded] = useState(false); const location = useLocation(); // Detect mobile devices with enhanced responsive breakpoints useEffect(() => { const checkMobile = () => { const mobile = window.innerWidth < 768; // Only consider mobile devices with screens less than 768px const tablet = window.innerWidth >= 768 && window.innerWidth < 1024; // Tablet breakpoint const touch = 'ontouchstart' in window || navigator.maxTouchPoints > 0; setIsMobile(mobile); setIsTouchDevice(touch); // Auto-collapse on mobile only, not on tablets if (mobile && !isCollapsed) { toggleSidebar(); } }; checkMobile(); window.addEventListener('resize', checkMobile); return () => window.removeEventListener('resize', checkMobile); }, [isCollapsed, toggleSidebar]); // Enhanced loading state with skeleton useEffect(() => { setIsMounted(true); const timer = setTimeout(() => setIsLoading(false), 300); return () => clearTimeout(timer); }, []); // Enhanced keyboard shortcuts useEffect(() => { const handleGlobalKeyDown = (e) => { // Toggle sidebar with Ctrl/Cmd + B if ((e.ctrlKey || e.metaKey) && e.key === 'b') { e.preventDefault(); toggleSidebar(); } // Close sidebar with Escape if (e.key === 'Escape' && isMobile && !isCollapsed) { toggleSidebar(); } }; document.addEventListener('keydown', handleGlobalKeyDown); return () => document.removeEventListener('keydown', handleGlobalKeyDown); }, [isCollapsed, isMobile, toggleSidebar]); // Enhanced hover effects for desktop useEffect(() => { if (!isMobile && !isCollapsed) { setIsExpanded(true); } else { setIsExpanded(false); } }, [isMobile, isCollapsed]); // Handle collapse/expand animations useEffect(() => { if (isCollapsed !== undefined) { setIsAnimating(true); const timer = setTimeout(() => setIsAnimating(false), 300); return () => clearTimeout(timer); } }, [isCollapsed]); // Enhanced menu items with icons and metadata using design system const menuItems = [ { path: '/dashboard', label: 'Dashboard', icon: 'dashboard', description: 'Overview and analytics', iconColor: 'text-primary-600', animationDelay: 0, ariaLabel: 'Dashboard - Overview and analytics', gradient: 'from-primary-500 to-primary-600' }, { path: '/sources', label: 'Sources', icon: 'rss_feed', description: 'Content sources management', iconColor: 'text-accent-600', animationDelay: 100, ariaLabel: 'Sources - Content sources management', gradient: 'from-accent-500 to-accent-600' }, { path: '/accounts', label: 'Accounts', icon: 'account_circle', description: 'Social media accounts', iconColor: 'text-success-600', animationDelay: 200, ariaLabel: 'Accounts - Social media accounts', gradient: 'from-success-500 to-success-600' }, { path: '/posts', label: 'Posts', icon: 'post_add', description: 'Content posts', iconColor: 'text-warning-600', animationDelay: 300, ariaLabel: 'Posts - Content posts', gradient: 'from-warning-500 to-warning-600' }, { path: '/schedule', label: 'Schedule', icon: 'schedule', description: 'Posting schedule', iconColor: 'text-info-600', animationDelay: 400, ariaLabel: 'Schedule - Posting schedule', gradient: 'from-info-500 to-info-600' } ]; // Keyboard navigation handler const handleKeyDown = (e, index) => { if (!e.currentTarget.classList.contains('nav-link')) return; switch (e.key) { case 'ArrowDown': e.preventDefault(); setFocusedIndex(prev => (prev < menuItems.length - 1 ? prev + 1 : 0)); break; case 'ArrowUp': e.preventDefault(); setFocusedIndex(prev => (prev > 0 ? prev - 1 : menuItems.length - 1)); break; case 'Enter': case ' ': e.preventDefault(); e.currentTarget.click(); break; case 'Escape': if (isMobile && !isCollapsed) { toggleSidebar(); } break; default: break; } }; // Enhanced responsive sidebar classes with mobile-first approach using design system const sidebarClasses = `sidebar transition-all duration-300 ease-in-out ${ isCollapsed ? 'collapsed' : '' } ${ isMobile ? (isCollapsed ? 'w-26' : 'w-64') : (isCollapsed ? 'w-26' : 'w-64') } ${isMounted ? 'animate-slide-in-left' : ''} ${ isMobile ? 'fixed top-16 left-0 bottom-0 z-[60]' : 'fixed top-16 left-0 z-[60] h-[calc(100vh-4rem)]' } ${isExpanded ? 'shadow-xl' : ''}`; // Enhanced toggle button with responsive design using design system const toggleClasses = `sidebar-toggle flex items-center justify-center w-8 h-8 sm:w-10 sm:h-10 rounded-lg transition-all duration-200 ease-in-out hover:bg-primary-100 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2 relative overflow-hidden touch-manipulation active:scale-95 ${ isMobile ? (isCollapsed ? 'mx-auto mt-2' : 'absolute top-3 right-1.5 sm:top-4 sm:right-2') : (isCollapsed ? 'mx-auto mt-2' : 'mx-auto mt-4') } backdrop-blur-sm bg-white/90 border border-transparent hover:border-primary-200 shadow-sm hover:shadow-md`; // Enhanced navigation with responsive spacing using design system const navClasses = `sidebar-nav h-full flex flex-col transition-all duration-300 ${ isMobile ? 'justify-start pt-2 pb-4' : (isCollapsed ? 'justify-start py-1' : 'pt-8 pb-4') }`; // Enhanced navigation list with responsive spacing using design system const navListClasses = `nav-list space-y-0 ${ isMobile ? 'px-1 py-1' : (isCollapsed ? 'px-1 py-0.5' : 'px-2 py-3') }`; // Enhanced navigation item with responsive hover effects using design system const navItemClasses = (index) => `nav-item relative transition-all duration-200 ease-in-out group ${ isMobile ? 'my-0.5 mx-0.5' : (isCollapsed ? 'my-0.5 mx-0.5' : 'my-1 mx-0.5') } ${ focusedIndex === index ? 'ring-2 ring-primary-500 ring-offset-2' : '' } hover:bg-white/20 overflow-hidden z-10`; // Enhanced navigation link with responsive design using design system const navLinkClasses = useCallback(({ isActive }) => ` nav-link group relative flex items-center px-2 sm:px-2.5 py-1.5 sm:py-2 text-xs font-medium rounded-lg transition-all duration-200 ease-in-out ${isActive ? 'bg-gradient-to-r from-primary-600 to-primary-700 text-white shadow-md transform scale-105' : 'text-secondary-700 hover:bg-accent-100 hover:shadow-sm transform hover:scale-102' } ${isMobile ? (isCollapsed ? 'justify-center px-1 sm:px-1.5 py-2 sm:py-2.5' : 'justify-start px-1 sm:px-1.5 py-1 sm:py-1.5') : (isCollapsed ? 'justify-start px-1.5' : 'justify-start px-2 py-2')} focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed relative overflow-hidden before:absolute before:inset-0 before:bg-gradient-to-r before:from-primary-500 before:to-primary-600 before:opacity-0 before:transition-opacity before:duration-200 before:rounded-lg group-hover:before:opacity-10 ${isTouchDevice ? 'touch-manipulation' : ''} ${focusedIndex >= 0 ? 'focus:ring-2 focus:ring-primary-500 focus:ring-offset-2' : ''} border border-transparent hover:border-primary-200 hover:shadow-md hover:shadow-primary-500/10 active:scale-95 active:shadow-inner min-h-[36px] sm:min-h-[40px] /* Reduced touch target size */ ${isCollapsed ? 'p-0' : ''} /* Remove padding when collapsed to ensure icons are visible */ `, [isCollapsed, isMobile, isTouchDevice, focusedIndex]); // Enhanced icon classes with responsive sizing using design system const iconClasses = ` nav-icon flex-shrink-0 w-5 h-5 transition-all duration-200 ease-in-out ${isMobile ? (isCollapsed ? 'mx-auto text-base sm:text-lg' : 'mr-2 sm:mr-3 text-base') : (isCollapsed ? 'mx-auto text-lg' : 'mr-3 text-base')} group-hover:rotate-12 group-hover:scale-110 transition-transform duration-300 ease-out shadow-sm hover:shadow-md z-20 material-icons flex items-center justify-center ${isCollapsed ? 'scale-110' : ''} /* Scale up icons when collapsed for better visibility */ `; // Enhanced label classes with responsive typography using design system const labelClasses = ` nav-label transition-all duration-200 ease-in-out ${isMobile ? (isCollapsed ? 'opacity-0 max-w-0 overflow-hidden' : 'opacity-100 max-w-full text-xs sm:text-sm font-medium') : (isCollapsed ? 'opacity-100 max-w-full text-xs font-medium' : 'opacity-100 max-w-full text-sm font-medium')} group-hover:text-primary-600 transition-colors duration-200 tracking-tight text-secondary-900 font-medium ${isCollapsed && !isMobile ? 'absolute left-12 top-1/2 transform -translate-y-1/2 bg-white px-2 py-1 rounded shadow-lg z-20 whitespace-nowrap' : ''} `; // Enhanced description classes with responsive visibility using design system const descriptionClasses = ` nav-description text-xs text-secondary-500 mt-0.5 sm:mt-1 transition-all duration-200 ease-in-out ${isMobile ? 'hidden' : (isCollapsed ? 'opacity-0 max-w-0 overflow-hidden' : 'opacity-100 max-w-full')} group-hover:text-secondary-700 transition-colors duration-200 tracking-normal font-normal leading-relaxed `; // Enhanced badge classes with responsive sizing using design system const badgeClasses = ` badge absolute top-0.5 right-0.5 px-1 py-0.5 text-xs font-medium rounded-full ${isMobile ? (isCollapsed ? 'opacity-0 scale-0' : 'opacity-100 scale-100') : (isCollapsed ? 'opacity-0 scale-0' : 'opacity-100 scale-100')} transition-all duration-200 ease-in-out animate-bounce-subtle bg-gradient-to-r from-primary-500 to-primary-600 text-white shadow-sm backdrop-blur-sm border border-white/20 font-semibold tracking-wide `; // Enhanced count indicator classes with responsive sizing using design system const countClasses = ` count-indicator absolute top-0.5 right-0.5 w-4 h-4 sm:w-5 sm:h-5 flex items-center justify-center ${isMobile ? (isCollapsed ? 'opacity-0 scale-0' : 'opacity-100 scale-100') : (isCollapsed ? 'opacity-0 scale-0' : 'opacity-100 scale-100')} transition-all duration-200 ease-in-out animate-pulse-slow bg-gradient-to-br from-secondary-500 to-secondary-600 text-white rounded-full text-xs shadow-sm backdrop-blur-sm border border-white/20 font-semibold tracking-wide `; // Enhanced skeleton loading component with responsive design using design system const SkeletonLoader = () => (