import { useState, useRef, useEffect } from 'react'; import { Link, useLocation, useNavigate } from 'react-router-dom'; import { Inbox, Calendar, CalendarDays, Plus, ChevronDown, ChevronRight, Hash, Settings, LogOut, User, FolderPlus, Tag, X, Check, PanelLeftClose, PanelLeftOpen } from 'lucide-react'; import { cn } from '@/lib/utils'; import { useAuthStore } from '@/stores/auth'; import { useTasksStore } from '@/stores/tasks'; const PROJECT_COLORS = [ '#3b82f6', '#ef4444', '#22c55e', '#f59e0b', '#8b5cf6', '#ec4899', '#06b6d4', '#f97316', '#6366f1', '#14b8a6', ]; const SIDEBAR_COLLAPSED_KEY = 'sidebar-collapsed'; export function Sidebar() { const location = useLocation(); const navigate = useNavigate(); const { user, logout } = useAuthStore(); const { projects, labels, createProject } = useTasksStore(); const [projectsExpanded, setProjectsExpanded] = useState(true); const [labelsExpanded, setLabelsExpanded] = useState(true); const [showNewProject, setShowNewProject] = useState(false); const [newProjectName, setNewProjectName] = useState(''); const [newProjectColor, setNewProjectColor] = useState(PROJECT_COLORS[0]); const [isCreatingProject, setIsCreatingProject] = useState(false); const [isCollapsed, setIsCollapsed] = useState(() => { try { return localStorage.getItem(SIDEBAR_COLLAPSED_KEY) === 'true'; } catch { return false; } }); const newProjectInputRef = useRef(null); useEffect(() => { if (showNewProject && newProjectInputRef.current) { newProjectInputRef.current.focus(); } }, [showNewProject]); const toggleCollapsed = () => { const next = !isCollapsed; setIsCollapsed(next); try { localStorage.setItem(SIDEBAR_COLLAPSED_KEY, String(next)); } catch {} }; const handleCreateProject = async () => { if (!newProjectName.trim() || isCreatingProject) return; setIsCreatingProject(true); try { const project = await createProject({ name: newProjectName.trim(), color: newProjectColor }); setNewProjectName(''); setNewProjectColor(PROJECT_COLORS[0]); setShowNewProject(false); navigate(`/project/${project.id}`); } catch (error) { console.error('Failed to create project:', error); } finally { setIsCreatingProject(false); } }; const handleCancelNewProject = () => { setShowNewProject(false); setNewProjectName(''); setNewProjectColor(PROJECT_COLORS[0]); }; const handleLogout = async () => { await logout(); navigate('/login'); }; const inbox = projects.find(p => p.isInbox); const regularProjects = projects.filter(p => !p.isInbox); const navItems = [ { path: '/inbox', icon: Inbox, label: 'Inbox', color: '#3b82f6' }, { path: '/today', icon: Calendar, label: 'Today', color: '#22c55e' }, { path: '/upcoming', icon: CalendarDays, label: 'Upcoming', color: '#8b5cf6' }, ]; // Collapsed sidebar if (isCollapsed) { return ( ); } return ( ); }