|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useCallback, useEffect, useMemo, useState } from 'react'; |
| 4 | +import { useLocale, useTranslations } from 'next-intl'; |
| 5 | +import { TYPE_LIBRARY } from '../data'; |
| 6 | +import { EN_TYPE_COPY } from '../copy'; |
| 7 | + |
| 8 | +type Entry = { |
| 9 | + mode: string; |
| 10 | + totalSubmissions: number; |
| 11 | +}; |
| 12 | + |
| 13 | +type ResponseData = { |
| 14 | + entries: Entry[]; |
| 15 | + totalSubmissions: number; |
| 16 | +}; |
| 17 | + |
| 18 | +export default function SbtiTypeLeaderboard() { |
| 19 | + const locale = useLocale(); |
| 20 | + const t = useTranslations('sbtiTest.leaderboard'); |
| 21 | + const [entries, setEntries] = useState<Entry[]>([]); |
| 22 | + const [loading, setLoading] = useState(true); |
| 23 | + |
| 24 | + const fetchDistribution = useCallback(async () => { |
| 25 | + try { |
| 26 | + setLoading(true); |
| 27 | + const res = await fetch('/api/leaderboard?gameId=sbti-test&aggregate=modeCounts', { |
| 28 | + cache: 'no-store', |
| 29 | + }); |
| 30 | + |
| 31 | + if (!res.ok) { |
| 32 | + throw new Error('Failed to fetch leaderboard'); |
| 33 | + } |
| 34 | + |
| 35 | + const data = await res.json() as ResponseData; |
| 36 | + setEntries(data.entries); |
| 37 | + } catch (error) { |
| 38 | + console.error(error); |
| 39 | + setEntries([]); |
| 40 | + } finally { |
| 41 | + setLoading(false); |
| 42 | + } |
| 43 | + }, []); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + void fetchDistribution(); |
| 47 | + }, [fetchDistribution]); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + const handleUpdate = (event: Event) => { |
| 51 | + const customEvent = event as CustomEvent<{ gameId: string }>; |
| 52 | + if (customEvent.detail?.gameId === 'sbti-test') { |
| 53 | + void fetchDistribution(); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + window.addEventListener('leaderboardUpdated', handleUpdate); |
| 58 | + return () => window.removeEventListener('leaderboardUpdated', handleUpdate); |
| 59 | + }, [fetchDistribution]); |
| 60 | + |
| 61 | + const localizedName = useCallback((code: string) => { |
| 62 | + if (locale === 'en') { |
| 63 | + return EN_TYPE_COPY[code]?.name || TYPE_LIBRARY[code as keyof typeof TYPE_LIBRARY]?.cn || code; |
| 64 | + } |
| 65 | + |
| 66 | + return TYPE_LIBRARY[code as keyof typeof TYPE_LIBRARY]?.cn || code; |
| 67 | + }, [locale]); |
| 68 | + |
| 69 | + const rankedEntries = useMemo(() => entries.map((entry) => ({ |
| 70 | + ...entry, |
| 71 | + name: localizedName(entry.mode), |
| 72 | + })), [entries, localizedName]); |
| 73 | + |
| 74 | + if (loading) { |
| 75 | + return ( |
| 76 | + <div className="w-full bg-background border rounded-xl overflow-hidden shadow-sm"> |
| 77 | + <div className="flex justify-center p-8 text-muted-foreground animate-pulse"> |
| 78 | + {t('loading')} |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + if (rankedEntries.length === 0) { |
| 85 | + return ( |
| 86 | + <div className="w-full bg-background border rounded-xl overflow-hidden shadow-sm"> |
| 87 | + <div className="p-8 text-center text-muted-foreground"> |
| 88 | + {t('empty')} |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + return ( |
| 95 | + <div className="w-full bg-background border rounded-xl overflow-hidden shadow-sm"> |
| 96 | + <div className="max-h-[420px] overflow-y-auto"> |
| 97 | + <table className="w-full text-sm"> |
| 98 | + <thead className="bg-muted/50 sticky top-0 backdrop-blur-sm z-10"> |
| 99 | + <tr> |
| 100 | + <th className="text-left font-medium p-4 text-muted-foreground">{t('rank')}</th> |
| 101 | + <th className="text-left font-medium p-4 text-muted-foreground">{t('type')}</th> |
| 102 | + <th className="text-right font-medium p-4 text-muted-foreground">{t('count')}</th> |
| 103 | + </tr> |
| 104 | + </thead> |
| 105 | + <tbody className="divide-y"> |
| 106 | + {rankedEntries.map((entry, index) => ( |
| 107 | + <tr key={entry.mode} className="hover:bg-muted/50 transition-colors"> |
| 108 | + <td className="p-4"> |
| 109 | + <div className="flex items-center justify-center w-6 h-6 rounded-full font-bold bg-muted text-muted-foreground text-xs"> |
| 110 | + {index + 1} |
| 111 | + </div> |
| 112 | + </td> |
| 113 | + <td className="p-4 font-medium text-foreground"> |
| 114 | + {entry.name} |
| 115 | + </td> |
| 116 | + <td className="p-4 text-right font-mono font-bold text-foreground"> |
| 117 | + {entry.totalSubmissions} |
| 118 | + </td> |
| 119 | + </tr> |
| 120 | + ))} |
| 121 | + </tbody> |
| 122 | + </table> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + ); |
| 126 | +} |
0 commit comments