"use client"; import { useState, useEffect } from "react"; import type { ReactElement } from "react"; import { TaskList } from "@/components/tasks/TaskList"; import { MosaicSpinner } from "@/components/ui/MosaicSpinner"; import { fetchTasks } from "@/lib/api/tasks"; import { useWorkspaceId } from "@/lib/hooks"; import type { Task } from "@mosaic/shared"; export default function TasksPage(): ReactElement { const workspaceId = useWorkspaceId(); const [tasks, setTasks] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (!workspaceId) { setIsLoading(false); return; } let cancelled = false; setError(null); setIsLoading(true); async function loadTasks(): Promise { try { const filters = workspaceId !== null ? { workspaceId } : {}; const data = await fetchTasks(filters); if (!cancelled) { setTasks(data); } } catch (err: unknown) { console.error("[Tasks] Failed to fetch tasks:", err); if (!cancelled) { setError( err instanceof Error ? err.message : "We had trouble loading your tasks. Please try again when you're ready." ); } } finally { if (!cancelled) { setIsLoading(false); } } } void loadTasks(); return (): void => { cancelled = true; }; }, [workspaceId]); function handleRetry(): void { if (!workspaceId) return; setError(null); setIsLoading(true); fetchTasks({ workspaceId }) .then((data) => { setTasks(data); }) .catch((err: unknown) => { console.error("[Tasks] Retry failed:", err); setError( err instanceof Error ? err.message : "We had trouble loading your tasks. Please try again when you're ready." ); }) .finally(() => { setIsLoading(false); }); } return (

Tasks

Organize your work at your own pace

{isLoading ? (
) : error !== null ? (

{error}

) : tasks.length === 0 ? (

No tasks found

) : ( )}
); }