import { useState } from "react"; import { Crown, Vote } from "lucide-react"; import { toast } from "sonner"; import { cn } from "@/lib/utils"; import { nomeCompleto, useGiocatoriSquadra } from "@/lib/giocatori-squadra"; import { useGiocatoreBase } from "@/lib/user-store"; import { usePresenzeEvento } from "@/lib/presenze"; import type { Evento } from "@/lib/eventi"; import { conteggioPartita, mioVoto, ORE_ATTESA_MVP, useVotaMvp, useVotiMvp, votoMvpAperto, type VotoMvp, } from "@/lib/mvp-voti"; /** * Pannello di votazione MVP di una partita: un voto a testa, modificabile. * * I voti sono legati all'evento CrAPP, non al referto CSI né allo scout: si vota anche * senza risultato caricato, dalle due ore dopo il fischio d'inizio. Votano e sono votabili * solo i presenti (o in ritardo) di quell'evento. */ export function VotazioneMvp({ evento }: { evento: Evento }) { const matchId = evento.id; // Solo `.id` serve qui: `useGiocatoreBase` basta, niente statistiche. const io = useGiocatoreBase(); const voti = useVotiMvp(); const vota = useVotaMvp(); const { righe: squadra } = useGiocatoriSquadra(); const { risposte } = usePresenzeEvento(evento.id); const presente = (id: string) => risposte[id] === "presente" || risposte[id] === "ritardo"; const rosa = squadra.filter((g) => g.attivo && presente(g.id)); 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; const puoVotare = !!io && presente(io.id); if (!votoMvpAperto(evento.data, evento.ora)) { return (
Apre {ORE_ATTESA_MVP} ore dopo l'inizio della partita.
{testa && !pareggio ? (
Hai votato {mio.votato_nome}
) : !puoVotare ? (Vota chi era presente a questa partita.
) : null} {aperto ? (