import { useState } from "react"; import { ClipboardCheck, Lock } from "lucide-react"; import { toast } from "sonner"; import { cn } from "@/lib/utils"; import { Card } from "@/components/crapp/ui-bits"; import { Avatar } from "@/components/crapp/Avatar"; import type { Giocatore } from "@/lib/crapp-data"; import { useGiocatoreBase } from "@/lib/user-store"; import { mieiVoti, pagellePartita, usePagelle, useVotaPagella } from "@/lib/pagelle"; const voti = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; /** Pagelle di fine partita: voto anonimo 1-10 a ciascun compagno. */ export function Pagelle({ matchId, convocati, chiuse = false, }: { matchId: string; convocati: Giocatore[]; chiuse?: boolean; }) { // Solo `.id` serve qui: `useGiocatoreBase` basta, niente statistiche. const io = useGiocatoreBase(); const { voti: tutti, isPending } = usePagelle(); const vota = useVotaPagella(); const [apertoPer, setApertoPer] = useState(null); const medie = pagellePartita(tutti, matchId); const miei = io ? mieiVoti(tutti, matchId, io.id) : {}; const daVotare = convocati.filter((g) => g.id !== io?.id); const fatti = daVotare.filter((g) => miei[g.id] !== undefined).length; async function invia(votatoId: string, voto: number) { if (!io) return; setApertoPer(null); try { await vota.mutateAsync({ match_id: matchId, votante_id: io.id, votato_id: votatoId, voto }); toast.success("Voto registrato (resta anonimo)"); } catch { toast.error("Voto non riuscito, riprova"); } } return (

Pagelle anonime

{chiuse ? "Votazioni chiuse" : `${fatti}/${daVotare.length} votati`}

Dai un voto da 1 a 10 ai compagni: nessuno vedrà chi ha votato cosa, solo la media.

{isPending ? (

Carico le pagelle…

) : (
{convocati.map((g) => { const media = medie[g.id]; const mio = miei[g.id]; const sonoIo = g.id === io?.id; const aperto = apertoPer === g.id; return (
{g.nome} {media ? `Media ${media.media} · ${media.voti} voti` : "Nessun voto"} {sonoIo ? ( Sei tu ) : chiuse ? ( ) : ( )}
{aperto && !chiuse ? (
{voti.map((v) => ( ))}
) : null}
); })}
)}
); }