import { useEffect, useState } from 'react'; import { useParams, useNavigate, useLocation } from 'react-router-dom'; import { LayoutList, LayoutGrid, Plus } from 'lucide-react'; import { cn } from '@/lib/utils'; import { useTasksStore } from '@/stores/tasks'; import { TaskItem } from '@/components/TaskItem'; import { AddTask } from '@/components/AddTask'; import { CompletedSection } from '@/components/CompletedSection'; import { BoardView } from '@/pages/Board'; import { api } from '@/lib/api'; import type { Project as ProjectType, Section } from '@/types'; export function ProjectPage() { const { id } = useParams<{ id: string }>(); const location = useLocation(); const navigate = useNavigate(); const { tasks, completedTasks, isLoading, fetchTasks, fetchCompletedTasks, setSelectedTask } = useTasksStore(); const [project, setProject] = useState(null); const [sections, setSections] = useState([]); const isBoardView = location.pathname.endsWith('/board'); useEffect(() => { if (!id) return; fetchTasks({ projectId: id, completed: false }); fetchCompletedTasks({ projectId: id }); api.getProject(id).then((p) => { setProject(p); setSections(p.sections || []); }).catch(console.error); }, [id]); if (!project) { return (
Loading project...
); } const toggleView = () => { if (isBoardView) { navigate(`/project/${id}`); } else { navigate(`/project/${id}/board`); } }; // Group tasks by section const unsectionedTasks = tasks.filter((t) => !t.sectionId); const tasksBySection = sections.map((section) => ({ section, tasks: tasks.filter((t) => t.sectionId === section.id), })); return (
{/* Header */}

{project.name}

{isBoardView ? ( ) : ( /* List view */
{isLoading ? (
Loading tasks...
) : ( <> {/* Unsectioned tasks */} {unsectionedTasks.length > 0 && (
{unsectionedTasks.map((task) => ( setSelectedTask(task)} /> ))}
)} {/* Add task for unsectioned */} {/* Sections */} {tasksBySection.map(({ section, tasks: sectionTasks }) => (

{section.name}

{sectionTasks.map((task) => ( setSelectedTask(task)} /> ))}
))} {/* Completed tasks */} )}
)}
); }