"use client"; import { useState, useTransition } from "react"; import { X, Plus } from "lucide-react"; export function PoolManager({ label, pool, onAdd, onRemove, }: { label: string; pool: string[]; onAdd: (value: string) => Promise; onRemove: (value: string) => Promise; }) { const [input, setInput] = useState(""); const [isPending, startTransition] = useTransition(); const [error, setError] = useState(null); const canAdd = input.trim().length > 0; function handleAdd() { const trimmed = input.trim(); if (!trimmed) return; if (pool.includes(trimmed)) { setError("Valore già presente"); return; } setError(null); startTransition(async () => { await onAdd(trimmed); setInput(""); }); } function handleRemove(value: string) { const ok = window.confirm( `Eliminare "${value}" da «${label}»?\n\nVerrà rimosso ovunque: sparirà dal menu e da tutte le offerte/servizi che lo usano.` ); if (!ok) return; startTransition(() => onRemove(value)); } function onKeyDown(e: React.KeyboardEvent) { if (e.key === "Enter") { e.preventDefault(); handleAdd(); } } return (

{label}

{pool.length > 0 && ( {pool.length} )}
{pool.length === 0 ? ( Nessun valore ) : ( pool.map((v) => ( {v} )) )}
{ setInput(e.target.value); setError(null); }} onKeyDown={onKeyDown} placeholder="Aggiungi valore" disabled={isPending} aria-label={`Aggiungi valore a ${label}`} className="h-8 w-full rounded-md border border-[#e5e7eb] bg-white pl-3 pr-9 text-sm text-[#1a1a1a] placeholder:text-[#a1a1aa] transition-shadow focus:border-[#1A463C]/30 focus:outline-none focus:ring-2 focus:ring-[#1A463C]/15 disabled:opacity-50" />
{error &&

{error}

}
); }