import { useState } from 'react'; import { api, type LeaderboardPeriod } from '@/api'; import { useAppData } from '@/state/AppData'; import { useAsync } from '@/lib/useAsync'; import { EmptyState, ErrorState, LoadingState, Screen, ScreenHeader } from '@/components/ui'; import { toFa } from '@/lib/format'; const MEDAL: Record = { 1: { bg: '#FFD23F', fg: '#8a6d00', h: 92 }, 2: { bg: '#D9DEE8', fg: '#5a6270', h: 72 }, 3: { bg: '#E8B27A', fg: '#7a4a1a', h: 56 }, }; /** جدولِ امتیازها؛ هر بار که باز شود تازه گرفته می‌شود. */ export function LeaderboardScreen() { const { profile } = useAppData(); const [period, setPeriod] = useState('weekly'); const board = useAsync(() => api.leaderboard(period), [period]); const entries = board.data ?? []; const podium = [entries[1], entries[0], entries[2]].filter(Boolean); const isMe = (userId: string) => userId === profile.userId; return (
{([['weekly', 'این هفته'], ['all_time', 'همه‌ی زمان‌ها']] as const).map(([p, label]) => ( ))}
{board.loading ? ( ) : board.error ? ( ) : entries.length === 0 ? ( ) : ( <>
{podium.map((e) => { const m = MEDAL[e.rank] ?? MEDAL[3]; return (
{toFa(e.rank)} {isMe(e.userId) ? 'تو' : e.name || 'بی‌نام'} {toFa(e.xpInPeriod)} امتیاز
); })}
    {entries.slice(3).map((e) => (
  • {toFa(e.rank)} {e.name?.[0] ?? '؟'} {isMe(e.userId) ? 'تو' : e.name || 'بی‌نام'} {toFa(e.xpInPeriod)} امتیاز
  • ))}
)}
); }