import React, { useState, useEffect } from 'react'; import axios from 'axios'; import { motion } from 'framer-motion'; import { useNavigate, useLocation } from 'react-router-dom'; import { useAuth } from '../context/AuthContext'; import { useTranslation } from 'react-i18next'; import { LayoutDashboard, PenTool, Calendar, Trophy, Settings, LogOut, User } from 'lucide-react'; const SidebarItem = ({ icon: Icon, label, active, onClick, badge }) => (
{label}
{badge && ( {badge} )}
); let cachedProjectCount = null; const Sidebar = () => { const navigate = useNavigate(); const location = useLocation(); const { logout, user } = useAuth(); const { t } = useTranslation(); const [projectCount, setProjectCount] = useState(() => { if (cachedProjectCount !== null) return cachedProjectCount; const stored = localStorage.getItem('tf_project_count'); return stored ? parseInt(stored, 10) : 0; }); useEffect(() => { if (!user) return; const fetchProjectCount = async () => { try { const API_URL = import.meta.env.VITE_API_URL; const res = await axios.get(`${API_URL}/graphs/user/${user.id}`); if (Array.isArray(res.data)) { const count = res.data.length; setProjectCount(count); cachedProjectCount = count; localStorage.setItem('tf_project_count', count.toString()); } } catch (err) { console.error('[Sidebar] Failed to fetch project count:', err); } }; fetchProjectCount(); const interval = setInterval(fetchProjectCount, 10000); return () => clearInterval(interval); }, [user]); const handleLogout = () => { logout(); navigate('/auth'); }; const menuItems = [ { icon: LayoutDashboard, labelKey: 'projects', path: '/dashboard' }, { icon: PenTool, labelKey: 'canvas', path: '/canvas' }, { icon: Calendar, labelKey: 'habits', path: '/habits' }, { icon: User, labelKey: 'profile', path: '/profile' }, { icon: Settings, labelKey: 'settings', path: '/settings' }, ]; return ( ); }; export default Sidebar;