import { useEffect, useState, useCallback } from 'react'
import { Trophy, Medal, Award, BarChart3, Users } from 'lucide-react'
import { useRole } from '../../context'
import { api, type LeaderboardResponse, type ReporterRank, type StationRank } from '../../api'
const PERIODS = ['2026-Q1', '2026-Q2', '2026-Q3', '2026-Q4']
function RankBadge({ rank }: { rank: number }) {
if (rank === 1) return
if (rank === 2) return
if (rank === 3) return
return {rank}
}
function ReporterRow({ item, isMe }: { item: ReporterRank; isMe: boolean }) {
return (
|
{isMe && 我}
{item.name}
|
{item.station} |
{(item.totalScore ?? 0).toFixed(1)} |
)
}
function StationRow({ item, isMe }: { item: StationRank; isMe: boolean }) {
return (
|
{isMe && 本站}
{item.name}
|
{item.reporterCount} 人 |
{(item.avgQuality ?? 0).toFixed(1)} |
{(item.avgQuantity ?? 0).toFixed(1)} |
{(item.avgEfficiency ?? 0).toFixed(1)} |
{(item.avgScore ?? 0).toFixed(1)} |
)
}
export function LeaderboardPage() {
const { role } = useRole()
const [data, setData] = useState(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState('')
const [period, setPeriod] = useState('2026-Q3')
const [groupBy, setGroupBy] = useState<'reporter' | 'station'>('reporter')
const load = useCallback(async () => {
setLoading(true); setError('')
try {
const result = await api.leaderboard(role, { period, group_by: groupBy })
setData(result)
} catch (e: any) { setError(e.message) }
finally { setLoading(false) }
}, [role, period, groupBy])
useEffect(() => { load() }, [load])
const myRank = data?.myRank
const ranks = data?.ranks ?? []
return (
<>
积分排行榜
{groupBy === 'reporter'
? '按考核周期统计全国记者积分排名。'
: '按考核周期统计各记者站平均积分排名。'}
{/* 筛选栏 */}
{loading ? '加载中...' : `${ranks.length} 条记录`}
{myRank != null && role !== 'headquarters' && ` · 我的排名:第 ${myRank} 名`}
{error && (
{error}
)}
{!loading && !error && data && ranks.length === 0 && (
暂无排行榜数据,请先在「评分结果」中触发评分计算。
)}
{!loading && !error && ranks.length > 0 && (
<>
{/* 我的排名提示 */}
{data != null && myRank != null && role !== 'headquarters' && (
我的排名
第 {myRank} 名
/ 共 {data.ranks.length} 人
)}
{groupBy === 'reporter' ? (
| 排名 |
记者 |
记者站 |
综合得分 |
{(ranks as ReporterRank[]).map(item => (
))}
) : (
| 排名 |
记者站 |
人数 |
质量 |
数量 |
时效 |
综合得分 |
{(ranks as StationRank[]).map(item => (
))}
)}
>
)}
>
)
}