import { useState } from "react"; import { Crown, Vote } from "lucide-react"; import { toast } from "sonner"; import { cn } from "@/lib/utils"; import { giocatori } from "@/lib/crapp-data"; import { useGiocatoreCorrente } from "@/lib/user-store"; import { conteggioPartita, mioVoto, useVotaMvp, useVotiMvp, type VotoMvp } from "@/lib/mvp-voti"; /** Pannello di votazione MVP di una partita: un voto a testa, modificabile. */ export function VotazioneMvp({ matchId }: { matchId: string }) { const io = useGiocatoreCorrente(); const voti = useVotiMvp(); const vota = useVotaMvp(); const [aperto, setAperto] = useState(false); const tutti: VotoMvp[] = voti.data ?? []; const conteggio = conteggioPartita(tutti, matchId); const mio = io ? mioVoto(tutti, matchId, io.id) : null; const totale = conteggio.reduce((s, c) => s + c.voti, 0); const testa = conteggio[0]; const pareggio = conteggio.length > 1 && conteggio[1]!.voti === testa?.voti; async function invia(id: string, nome: string) { if (!io) return; try { await vota.mutateAsync({ match_id: matchId, votante_id: io.id, votato_id: id, votato_nome: nome, }); setAperto(false); toast.success(`Voto MVP registrato: ${nome}`); } catch { toast.error("Voto non riuscito, riprova"); } } return (

Voto MVP · {totale} {totale === 1 ? "voto" : "voti"}

{testa && !pareggio ? ( {testa.nome} ({testa.voti}) ) : ( {totale === 0 ? "Nessun voto: MVP da eleggere" : "Parità: servono altri voti"} )}

{mio ? (

Hai votato {mio.votato_nome}

) : null} {aperto ? (
{giocatori.map((g) => ( ))}
) : conteggio.length > 0 ? (
{conteggio.map((c) => ( {c.nome} · {c.voti} ))}
) : null}
); }