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 (
{testa && !pareggio ? (
Hai votato {mio.votato_nome}
) : null} {aperto ? (