2026-08-06 08:36:47 +02:00
|
|
|
import { useState } from "react";
|
|
|
|
|
import { Crown, Vote } from "lucide-react";
|
|
|
|
|
import { toast } from "sonner";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2026-09-03 10:17:03 +02:00
|
|
|
import { nomeCompleto, useGiocatoriSquadra } from "@/lib/giocatori-squadra";
|
2026-09-08 10:00:52 +02:00
|
|
|
import { useGiocatoreBase } from "@/lib/user-store";
|
2026-09-06 22:11:26 +02:00
|
|
|
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";
|
2026-08-06 08:36:47 +02:00
|
|
|
|
2026-09-06 22:11:26 +02:00
|
|
|
/**
|
|
|
|
|
* 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;
|
2026-09-08 10:00:52 +02:00
|
|
|
// Solo `.id` serve qui: `useGiocatoreBase` basta, niente statistiche.
|
|
|
|
|
const io = useGiocatoreBase();
|
2026-08-06 08:36:47 +02:00
|
|
|
const voti = useVotiMvp();
|
|
|
|
|
const vota = useVotaMvp();
|
2026-09-03 10:17:03 +02:00
|
|
|
const { righe: squadra } = useGiocatoriSquadra();
|
2026-09-06 22:11:26 +02:00
|
|
|
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));
|
2026-08-06 08:36:47 +02:00
|
|
|
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;
|
2026-09-06 22:11:26 +02:00
|
|
|
const puoVotare = !!io && presente(io.id);
|
|
|
|
|
|
|
|
|
|
if (!votoMvpAperto(evento.data, evento.ora)) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="mt-3 rounded-2xl bg-secondary/60 p-3">
|
|
|
|
|
<p className="inline-flex items-center gap-1.5 text-xs font-bold uppercase tracking-wide text-muted-foreground">
|
|
|
|
|
<Vote className="h-3.5 w-3.5" /> Voto MVP
|
|
|
|
|
</p>
|
|
|
|
|
<p className="mt-1 text-xs text-muted-foreground">
|
|
|
|
|
Apre {ORE_ATTESA_MVP} ore dopo l'inizio della partita.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-08-06 08:36:47 +02:00
|
|
|
|
|
|
|
|
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 (
|
|
|
|
|
<div className="mt-3 rounded-2xl bg-secondary/60 p-3">
|
|
|
|
|
<div className="flex items-center justify-between gap-2">
|
2026-09-05 12:42:24 +02:00
|
|
|
<p className="inline-flex items-center gap-1.5 text-xs font-bold uppercase tracking-wide text-muted-foreground">
|
2026-08-06 08:36:47 +02:00
|
|
|
<Vote className="h-3.5 w-3.5" /> Voto MVP · {totale} {totale === 1 ? "voto" : "voti"}
|
|
|
|
|
</p>
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
onClick={() => setAperto((v) => !v)}
|
2026-09-06 22:11:26 +02:00
|
|
|
disabled={!puoVotare}
|
2026-09-05 12:42:24 +02:00
|
|
|
className="rounded-full bg-accent px-3 py-1 text-xs font-bold uppercase text-accent-foreground disabled:opacity-50"
|
2026-08-06 08:36:47 +02:00
|
|
|
>
|
|
|
|
|
{mio ? "Cambia voto" : "Vota"}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<p className="mt-2 text-xs">
|
|
|
|
|
{testa && !pareggio ? (
|
|
|
|
|
<span className="inline-flex items-center gap-1 font-bold">
|
|
|
|
|
<Crown className="h-3.5 w-3.5 text-warning" /> {testa.nome} ({testa.voti})
|
|
|
|
|
</span>
|
|
|
|
|
) : (
|
|
|
|
|
<span className="text-muted-foreground">
|
|
|
|
|
{totale === 0 ? "Nessun voto: MVP da eleggere" : "Parità: servono altri voti"}
|
|
|
|
|
</span>
|
|
|
|
|
)}
|
|
|
|
|
</p>
|
|
|
|
|
{mio ? (
|
2026-09-05 12:42:24 +02:00
|
|
|
<p className="mt-1 text-xs text-muted-foreground">Hai votato {mio.votato_nome}</p>
|
2026-09-06 22:11:26 +02:00
|
|
|
) : !puoVotare ? (
|
|
|
|
|
<p className="mt-1 text-xs text-muted-foreground">
|
|
|
|
|
Vota chi era presente a questa partita.
|
|
|
|
|
</p>
|
2026-08-06 08:36:47 +02:00
|
|
|
) : null}
|
|
|
|
|
|
|
|
|
|
{aperto ? (
|
|
|
|
|
<div className="mt-3 grid max-h-60 grid-cols-2 gap-1.5 overflow-y-auto">
|
2026-09-06 19:30:49 +02:00
|
|
|
{/* Sé stessi fuori dall'elenco, come nei badge social: l'auto-voto è vietato
|
|
|
|
|
anche dal vincolo `mvp_no_autovoto` (M12). */}
|
|
|
|
|
{rosa
|
|
|
|
|
.filter((g) => g.id !== io?.id)
|
|
|
|
|
.map((g) => (
|
|
|
|
|
<button
|
|
|
|
|
key={g.id}
|
|
|
|
|
type="button"
|
|
|
|
|
disabled={vota.isPending}
|
|
|
|
|
onClick={() => invia(g.id, nomeCompleto(g))}
|
|
|
|
|
className={cn(
|
|
|
|
|
"truncate rounded-xl px-2.5 py-2 text-left text-xs font-semibold transition-colors",
|
|
|
|
|
mio?.votato_id === g.id
|
|
|
|
|
? "bg-accent text-accent-foreground"
|
|
|
|
|
: "bg-card text-foreground",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{nomeCompleto(g)}
|
|
|
|
|
</button>
|
|
|
|
|
))}
|
2026-08-06 08:36:47 +02:00
|
|
|
</div>
|
|
|
|
|
) : conteggio.length > 0 ? (
|
|
|
|
|
<div className="mt-2 flex flex-wrap gap-1.5">
|
|
|
|
|
{conteggio.map((c) => (
|
2026-09-05 12:42:24 +02:00
|
|
|
<span key={c.id} className="rounded-lg bg-card px-2 py-1 text-xs font-semibold">
|
2026-08-06 08:36:47 +02:00
|
|
|
{c.nome} · {c.voti}
|
|
|
|
|
</span>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2026-09-01 13:38:33 +02:00
|
|
|
}
|