"use client"; import { useState } from "react"; interface TaskLink { id: string; url: string; label: string; createdByAnonId: string; } interface Task { id: string; title: string; description: string; acceptanceCriteria: string; effort: string; status: string; claimedByAnonId: string | null; links: TaskLink[]; } const statusColors: Record = { open: "bg-gray-100 text-gray-700", in_progress: "bg-blue-100 text-blue-700", done: "bg-green-100 text-green-700", }; const statusLabels: Record = { open: "Open", in_progress: "In Progress", done: "Done", }; const effortColors: Record = { S: "bg-emerald-50 text-emerald-700", M: "bg-yellow-50 text-yellow-700", L: "bg-orange-50 text-orange-700", XL: "bg-red-50 text-red-700", }; export function TaskList({ tasks, onUpdate, }: { tasks: Task[]; onUpdate: (tasks: Task[]) => void; }) { const [expandedId, setExpandedId] = useState(null); const [linkUrl, setLinkUrl] = useState(""); const [linkLabel, setLinkLabel] = useState(""); const [actionLoading, setActionLoading] = useState(null); async function claimTask(taskId: string) { setActionLoading(taskId); const res = await fetch(`/api/tasks/${taskId}/claim`, { method: "POST" }); if (res.ok) { const d = await res.json(); onUpdate(tasks.map((t) => (t.id === taskId ? { ...t, ...d.data } : t))); } setActionLoading(null); } async function unclaimTask(taskId: string) { setActionLoading(taskId); const res = await fetch(`/api/tasks/${taskId}/claim`, { method: "DELETE", }); if (res.ok) { const d = await res.json(); onUpdate(tasks.map((t) => (t.id === taskId ? { ...t, ...d.data } : t))); } setActionLoading(null); } async function updateStatus(taskId: string, status: string) { setActionLoading(taskId); const res = await fetch(`/api/tasks/${taskId}/status`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ status }), }); if (res.ok) { const d = await res.json(); onUpdate(tasks.map((t) => (t.id === taskId ? { ...t, ...d.data } : t))); } setActionLoading(null); } async function addLink(taskId: string) { if (!linkUrl.trim()) return; const res = await fetch(`/api/tasks/${taskId}/links`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ url: linkUrl, label: linkLabel || undefined }), }); if (res.ok) { const d = await res.json(); onUpdate( tasks.map((t) => t.id === taskId ? { ...t, links: [...t.links, d.data] } : t ) ); setLinkUrl(""); setLinkLabel(""); } } async function deleteLink(taskId: string, linkId: string) { const res = await fetch(`/api/task-links/${linkId}`, { method: "DELETE", }); if (res.ok) { onUpdate( tasks.map((t) => t.id === taskId ? { ...t, links: t.links.filter((l) => l.id !== linkId) } : t ) ); } } return (
{tasks.map((task) => { const isExpanded = expandedId === task.id; const isLoading = actionLoading === task.id; return (
{isExpanded && (
{task.description && (

{task.description}

)} {task.acceptanceCriteria && (

Acceptance Criteria

{task.acceptanceCriteria}

)} {/* Actions */}
{task.status === "open" && ( )} {task.claimedByAnonId && task.status !== "done" && ( <> {task.status === "in_progress" && ( )} )} {task.status === "done" && ( )}
{/* Links */} {task.links.length > 0 && (

Links

{task.links.map((link) => (
{link.label || link.url}
))}
)} {/* Add Link */} {task.claimedByAnonId && (
setLinkUrl(e.target.value)} placeholder="https://github.com/..." className="flex-1 px-2 py-1.5 border rounded text-sm" /> setLinkLabel(e.target.value)} placeholder="Label" className="w-24 px-2 py-1.5 border rounded text-sm" />
)}
)}
); })}
); }