"use client"; import { useEffect, useState } from "react"; import { Plus, Trash2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog"; import { toast } from "sonner"; import Link from "next/link"; import { CreateItemDialog } from "@/components/create-item-dialog"; import { useCreateDialogStore } from "@/lib/stores/use-create-dialog-store"; interface Project { id: string; name: string; domain: string; status?: string; } export default function ProjectsPage() { const [projects, setProjects] = useState([]); const [domainMap, setDomainMap] = useState>(new Map()); const [loading, setLoading] = useState(true); const [deleteId, setDeleteId] = useState(null); const [deleting, setDeleting] = useState(false); const [refreshKey, setRefreshKey] = useState(0); const { open, openCreate, closeCreate } = useCreateDialogStore(); useEffect(() => { fetchProjects(); fetchDomains(); }, [refreshKey]); async function fetchProjects() { try { const res = await fetch("/api/projects?sort=-created"); const data = await res.json(); setProjects(data.items || []); } catch { toast.error("Unable to load projects"); } finally { setLoading(false); } } async function fetchDomains() { try { const res = await fetch("/api/domains?sort=sort_order"); const data = await res.json(); const map = new Map(); for (const d of data.items || []) map.set(d.id, d.name); setDomainMap(map); } catch {} } async function handleDelete(id: string) { setDeleting(true); try { const res = await fetch(`/api/projects/${id}`, { method: "DELETE" }); if (!res.ok) throw new Error(); toast.success("Project deleted"); setProjects((p) => p.filter((x) => x.id !== id)); } catch { toast.error("Unable to delete project"); } finally { setDeleting(false); setDeleteId(null); } } if (loading) return

Loading projects...

; return (

Projects

Plan and track your work.

{projects.length === 0 ?

No projects yet.

: (
{projects.map((p) => (
{p.name}
{domainMap.get(p.domain) || p.domain} {p.status && {p.status}}
))}
)} !o && setDeleteId(null)}> Delete project? This cannot be undone. Cancel deleteId && handleDelete(deleteId)} disabled={deleting}>{deleting ? "Deleting..." : "Delete"} (o ? openCreate("project") : closeCreate())} onCreated={() => setRefreshKey((k) => k + 1)} />
); }