import { useCallback, useEffect, useState } from 'react'; import { useNavigate, Link } from 'react-router-dom'; import type { PartnerLeaderboardPeriod, PartnerLeaderboardResponse } from '@dukang/shared-types'; import PageHeader from '@dukang/shared-ui/PageHeader'; import PullToRefresh from '@dukang/shared-ui/PullToRefresh'; import { fetchPartnerLeaderboard } from '../lib/leaderboard'; import { usePartnerSession } from '../contexts/PartnerSessionContext'; import { isPrimaryAccount } from '../lib/partnerAccess'; import { usePartnerPageView } from '../lib/usePageView'; const PERIOD_TABS: { key: PartnerLeaderboardPeriod; label: string }[] = [ { key: 'month', label: '本月' }, { key: 'lastMonth', label: '上月' }, { key: 'total', label: '累计' }, ]; function periodStatLabel(period: PartnerLeaderboardPeriod): string { if (period === 'month') return '本月拓店'; if (period === 'lastMonth') return '上月拓店'; return '累计拓店'; } function periodSubLabel(period: PartnerLeaderboardPeriod): string { if (period === 'total') return '累计'; if (period === 'month') return '本月新增'; return '上月新增'; } export default function LeaderboardPage() { usePartnerPageView('partner_leaderboard_view'); const navigate = useNavigate(); const { account } = usePartnerSession(); const showStaffFab = isPrimaryAccount(account); const [period, setPeriod] = useState('month'); const [data, setData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(''); const loadLeaderboard = useCallback(() => { setLoading(true); setError(''); return fetchPartnerLeaderboard(period) .then(setData) .catch((e) => { setData(null); setError(e instanceof Error ? e.message : '加载失败'); }) .finally(() => setLoading(false)); }, [period]); useEffect(() => { void loadLeaderboard(); }, [loadLeaderboard]); return ( navigate('/')} />
{PERIOD_TABS.map((tab) => ( ))}

按拓店数排行,数据实时更新

{loading &&
加载中...
} {!loading && error &&
{error}
} {!loading && !error && data && (
{data.self && (
第 {data.self.rank} 名

{data.self.name} {data.self.isSelf ? ' (我)' : ''}

{data.self.roleLabel}

{periodStatLabel(period)}: {data.self.periodStores} 间 {data.self.beatPercent != null && ( 击败 {data.self.beatPercent}% 合伙人 )}
)}

实时排行

{data.list.length === 0 ? (
暂无排行数据
) : ( data.list.map((entry) => (
{entry.rank}

{entry.name} ({entry.roleLabel})

累计拓店 {entry.totalStores} 间

{entry.periodStores} 间

{periodSubLabel(period)}

)) )}
)} {showStaffFab && ( add )}
); }