import { useState } from "react"; import { Crown, Vote } from "lucide-react"; import { toast } from "sonner"; import { cn } from "@/lib/utils"; import { inRosa, nomeCompleto, useGiocatoriSquadra } from "@/lib/giocatori-squadra"; import { useGiocatoreInCampo } from "@/lib/user-store"; import { usePresenzeEvento } from "@/lib/presenze"; import type { Evento } from "@/lib/eventi"; import { conteggioPartita, mioVoto, mvpPartita, ORE_ATTESA_MVP, useMieiVotiMvp, useVotaMvp, useVotiMvp, VOTI_MINIMI_MVP, votoMvpAperto, votoMvpChiuso, DURATA_VOTO_MVP_ORE, type VotoMvpPubblico, } from "@/lib/mvp-voti"; /** * Pannello di votazione MVP di una partita: un voto a testa, definitivo. Con una parità in * testa gli MVP sono più d'uno. * * 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, niente statistiche; `null` per l'allenatore, che non gioca (DD-034). const io = useGiocatoreInCampo(); const voti = useVotiMvp(); const miei = useMieiVotiMvp(io?.id); 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) => inRosa(g) && presente(g.id)); const [aperto, setAperto] = useState(false); const [scelto, setScelto] = useState<{ id: string; nome: string } | null>(null); const tutti: VotoMvpPubblico[] = voti.data ?? []; const conteggio = conteggioPartita(tutti, matchId); const mio = io ? mioVoto(miei.data ?? [], matchId, io.id) : null; const chiuso = votoMvpChiuso(evento.data, evento.ora); const totale = conteggio.reduce((s, c) => s + c.voti, 0); const mvp = mvpPartita(tutti, matchId); const puoVotare = !!io && !mio && !chiuso && presente(io.id); if (!votoMvpAperto(evento.data, evento.ora)) { return (
Apre {ORE_ATTESA_MVP} ore dopo l'inizio della partita.
{mvp.length > 0 ? (
{mvp.map((c) => (
Hai votato {mio.votato_nome}: il voto non si può cambiare.
) : chiuso ? (Votazione chiusa.
) : !puoVotare ? (Vota chi era presente a questa partita.
) : (Il voto è segreto e resta aperto {DURATA_VOTO_MVP_ORE} ore.
)} {aperto && !mio ? ( <>Il voto è definitivo: non si può cambiare.
> ) : conteggio.length > 0 ? (