"use client"; import Link from "next/link"; import { useState, useRef, useEffect, useTransition } from "react"; import { useRouter } from "next/navigation"; import { Check, ArrowRight } from "lucide-react"; import { EditableCell } from "@/components/ui/editable-cell"; import { OptionMultiSelect } from "@/components/ui/option-multi-select"; import { StatusBadge } from "@/components/ui/StatusBadge"; import { updateLeadField, addLeadTag, removeLeadTag, renameLeadTag, } from "@/app/admin/pipeline/actions"; import type { LeadWithTags, LeadFieldOptions } from "@/lib/admin-queries"; const COLUMN_HEADERS = [ "Nome", "Email", "Telefono", "Azienda", "Stato", "Prossima Azione", "Tag", "Azioni", ]; const COL_COUNT = COLUMN_HEADERS.length; function StatusCell({ lead, options, run, }: { lead: LeadWithTags; options: LeadFieldOptions; run: (fn: () => Promise) => void; }) { const [isOpen, setIsOpen] = useState(false); const containerRef = useRef(null); useEffect(() => { function handleClickOutside(e: MouseEvent) { if (containerRef.current && !containerRef.current.contains(e.target as Node)) { setIsOpen(false); } } document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, []); return (
setIsOpen((v) => !v)} className="flex items-center gap-1 px-2 py-1 rounded transition-colors duration-150 min-h-[28px] cursor-pointer hover:bg-muted" >
{isOpen && (
{options.status.map((stage) => ( ))}
)}
); } function LeadRow({ lead, options, }: { lead: LeadWithTags; options: LeadFieldOptions; }) { const router = useRouter(); const [, startTransition] = useTransition(); const [error, setError] = useState(null); function run(fn: () => Promise) { setError(null); startTransition(async () => { try { await fn(); router.refresh(); } catch (e) { setError(e instanceof Error ? e.message : "Errore nel salvataggio"); } }); } return ( <> run(() => updateLeadField(lead.id, "name", v))} /> run(() => updateLeadField(lead.id, "email", v))} /> run(() => updateLeadField(lead.id, "phone", v))} /> run(() => updateLeadField(lead.id, "company", v))} /> run(() => updateLeadField(lead.id, "next_action", v))} /> run(() => addLeadTag(lead.id, v))} onRemove={(v) => run(() => removeLeadTag(lead.id, v))} onRename={(o, n) => run(() => renameLeadTag(o, n))} /> Dettagli {error && ( {error} )} ); } export function LeadTable({ leads, options, }: { leads: LeadWithTags[]; options: LeadFieldOptions; }) { return (
{COLUMN_HEADERS.map((header) => ( ))} {leads.map((lead) => ( ))}
{header}
{leads.length === 0 && (
Nessun lead trovato
)}
); }