'use client'; import { cn } from '@/lib/utils'; // import { Timer } from '../Timer'; import type { ScoreEvent } from '@/lib/leaderboard'; import type { SWRSubscription } from 'swr/subscription'; import useSWRSubscription from 'swr/subscription'; import Podium from './podium/Podium'; import { Timer } from '../Timer'; const SCORE_COLORS = ['text-yellow-400', 'text-gray-400', 'text-orange-400']; export default function EventLeaderboard({ id }: { token: string; id: number }) { const subscription: SWRSubscription = (key, { next }) => { const socket = new WebSocket(key); socket.addEventListener('message', (event) => { next(null, JSON.parse(event.data)); }); socket.addEventListener('error', (event) => { console.error(event); }); return () => socket.close(); }; const { data } = useSWRSubscription( `wss://${process.env.NEXT_PUBLIC_API_URL?.split('//')[1]}/rleaderboard/${id}`, subscription ); const scores = [data?.groups] .flat() .sort((a, b) => a!.rank - b!.rank) .map((group, place) => ({ ...group, place })); return (
{data && } {data && data.end_date && }
    {data?.groups.map((group, key) => (
  • {group.rank}
    {group.name} {group.players && group.players.length > 1 ? group.players .map((player) => player.pseudo || 'Anonyme') .sort((a, b) => a.localeCompare(b)) .join(', ') : group.players[0].pseudo}
    {/*
    Puzzles {group.players.reduce((a, b) => a + b.completions, 0)}
    */}
    Score {group.players.reduce((a, b) => a + b.score, 0)}
  • ))}
); }