import { useState } from 'react'; import { Gift } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from '@/components/ui/alert-dialog'; import { notify } from '@/lib/notify'; import { api } from '../api/client'; import type { PostLotteryView } from '../api/types'; import { userPath } from '../utils/userPath'; interface Props { postId: number; lottery: PostLotteryView; isOwnerOrAdmin: boolean; onUpdate: (lottery: PostLotteryView) => void; } /** 抽奖帖信息卡 */ export default function PostLotteryCard({ postId, lottery, isOwnerOrAdmin, onUpdate }: Props) { const [drawOpen, setDrawOpen] = useState(false); const [drawing, setDrawing] = useState(false); const drawn = lottery.status === 'drawn'; const canDraw = !drawn && isOwnerOrAdmin && lottery.participant_count >= lottery.winner_count; const confirmDraw = async () => { setDrawing(true); try { const r = await api.postLotteryDraw(postId); onUpdate(r.lottery); notify.success('开奖完成'); setDrawOpen(false); } catch (e: unknown) { notify.error(e instanceof Error ? e.message : '开奖失败'); } finally { setDrawing(false); } }; return ( <> {drawn ? '已开奖' : '抽奖进行中'} 抽取 {lottery.winner_count} 人 · 当前 {lottery.participant_count} 人参与 {!drawn && ( 回帖即可参与(楼主除外),由作者或管理员手动开奖 )} {drawn && lottery.winners && lottery.winners.length > 0 && ( {lottery.winners.map(w => ( {w.nickname || w.username} ))} )} {canDraw && ( setDrawOpen(true)}>立即开奖 )} { if (!next && !drawing) setDrawOpen(false); }} > 立即开奖? 将从 {lottery.participant_count} 位参与者中抽取 {lottery.winner_count} 名中奖者。此操作不可撤销。 取消 { e.preventDefault(); void confirmDraw(); }} > {drawing ? '开奖中…' : '确认开奖'} > ); }
回帖即可参与(楼主除外),由作者或管理员手动开奖