import { useCallback, useEffect, useState } from 'react'; import { Coins, Dices, Gift } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Spinner } from '@/components/ui/spinner'; import { notify } from '@/lib/notify'; import { api } from '../api/client'; import type { CheckInStatus, LotteryStatus, PointLedger } from '../api/types'; import { useAuth } from '../hooks/useAuth'; const REASON_LABEL: Record = { check_in: '签到', lottery: '抽奖', unlock_spend: '解锁内容', creator_income: '创作分成', admin_adjust: '站长调账', }; /** 个人中心:积分余额、签到、抽奖、流水 */ export default function PointsWalletPanel() { const { refresh } = useAuth(); const [loading, setLoading] = useState(true); const [points, setPoints] = useState(0); const [income, setIncome] = useState(0); const [checkIn, setCheckIn] = useState(null); const [lottery, setLottery] = useState(null); const [ledger, setLedger] = useState([]); const [busy, setBusy] = useState(false); const load = useCallback(() => { setLoading(true); api.mePoints(1) .then(d => { setPoints(d.points); setIncome(d.creator_income_total); setCheckIn(d.check_in); setLottery(d.lottery); setLedger(d.ledger ?? []); }) .catch(e => notify.error(e.message)) .finally(() => setLoading(false)); }, []); useEffect(() => { load(); }, [load]); const doCheckIn = async () => { setBusy(true); try { const r = await api.checkIn(); notify.success(`签到成功,+${r.check_in.today_points} 积分`); setPoints(r.points); setCheckIn(r.check_in); await refresh(); load(); } catch (e: unknown) { notify.error(e instanceof Error ? e.message : '签到失败'); } finally { setBusy(false); } }; const doLottery = async () => { setBusy(true); try { const r = await api.lotteryDraw(); notify.success(r.lottery.points > 0 ? `抽中 ${r.lottery.points} 积分` : '未中奖,明天再来'); setPoints(r.points); setLottery(r.lottery); await refresh(); load(); } catch (e: unknown) { notify.error(e instanceof Error ? e.message : '抽奖失败'); } finally { setBusy(false); } }; if (loading) { return (
); } return (

积分钱包

{points} 可用积分 创作收入 {income}

最近流水

{ledger.length === 0 &&

暂无流水

}
    {ledger.map(row => (
  • = 0 ? 'pos' : 'neg'}> {row.delta >= 0 ? '+' : ''}{row.delta} {REASON_LABEL[row.reason] || row.reason}
  • ))}
); }