"use client"; import { useEffect, useState } from "react"; import { PublicProfileScreen } from "./PublicProfileScreen"; type LeaderboardRow = { id: string; name: string; totalGames: number; wins: number; soloGames: number; soloWins: number; multiGames: number; multiWins: number; avgClicks: number | null; bestTime: number | null; }; type Mode = "all" | "solo" | "multi"; function fmt(s: number): string { const m = Math.floor(s / 60); const sec = Math.floor(s % 60); return `${m}:${String(sec).padStart(2, "0")}`; } const MEDALS = ["🥇", "🥈", "🥉"]; export function LeaderboardScreen({ onBack }: { onBack: () => void }) { const [rows, setRows] = useState([]); const [loading, setLoading] = useState(true); const [mode, setMode] = useState("all"); const [viewingUserId, setViewingUserId] = useState(null); useEffect(() => { fetch("/api/leaderboard") .then((r) => r.json()) .then((data) => { setRows(data); setLoading(false); }); }, []); if (viewingUserId) { return ( setViewingUserId(null)} /> ); } const sorted = [...rows] .filter((r) => mode === "solo" ? r.soloGames > 0 : mode === "multi" ? r.multiGames > 0 : true, ) .sort((a, b) => { if (mode === "solo") return b.soloWins - a.soloWins || b.soloGames - a.soloGames; if (mode === "multi") return b.multiWins - a.multiWins || b.multiGames - a.multiGames; return b.wins - a.wins || b.totalGames - a.totalGames; }); return (
{/* Header */}

Classement

{/* Tabs */}
{(["all", "solo", "multi"] as Mode[]).map((m) => ( ))}
{loading ? (
) : sorted.length === 0 ? (

Aucune partie jouée pour le moment.

) : (
{sorted.map((row, i) => { const games = mode === "solo" ? row.soloGames : mode === "multi" ? row.multiGames : row.totalGames; const wins = mode === "solo" ? row.soloWins : mode === "multi" ? row.multiWins : row.wins; return (
setViewingUserId(row.id)} className={`flex items-center gap-2.5 px-3.5 py-3 rounded-xl border cursor-pointer hover:brightness-110 transition-all ${i < 3 ? "border-[#7c3aed] bg-[#7c3aed]/8" : "border-[#2e2e2e] bg-[#1a1a1a]"}`} > {MEDALS[i] ?? `#${i + 1}`}
{row.name}
{wins}{" "} victoires {games}{" "} parties {row.avgClicks != null && ( {row.avgClicks} {" "} clics moy. )} {row.bestTime != null && ( {fmt(row.bestTime)} {" "} meilleur )}
); })}
)}
); }