"use client"; import { useRef, useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { Check, ChevronDown, Loader2 } from "lucide-react"; import type { ProjectStatus } from "@prisma/client"; import { Popover } from "@/components/ui/Popover"; import { STATUS_META, STATUS_ORDER, statusOf } from "@/lib/status"; import { cn } from "@/lib/utils"; interface StatusSelectProps { projectId: string; status: ProjectStatus; /** compact: nur Punkt + Kurzlabel, fuer die Karten im Board. */ variant?: "compact" | "full"; onChanged?: (status: ProjectStatus) => void; } export function StatusSelect({ projectId, status, variant = "full", onChanged, }: StatusSelectProps) { const router = useRouter(); const triggerRef = useRef(null); const [open, setOpen] = useState(false); const [current, setCurrent] = useState(status); const [pending, startTransition] = useTransition(); const [saving, setSaving] = useState(false); const meta = statusOf(current); async function select(next: ProjectStatus) { setOpen(false); if (next === current) return; const previous = current; setCurrent(next); // optimistisch setSaving(true); try { const response = await fetch(`/api/projects/${projectId}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ status: next }), }); if (!response.ok) throw new Error(await response.text()); onChanged?.(next); startTransition(() => router.refresh()); } catch { setCurrent(previous); // zurueckrollen } finally { setSaving(false); } } return ( <> setOpen(false)} role="listbox" width={264} > {STATUS_ORDER.map((value) => { const option = STATUS_META[value]; const active = value === current; return ( ); })} ); }