"use client"; import { useState, useCallback } from "react"; import { X, Lock, Unlock, UserX, Flag, Crown, Loader2, } from "lucide-react"; import { UserProfile } from "@/types"; import { getAvatar, getAvatarBg } from "@/lib/avatars"; import Modal from "@/components/Modal"; import { useToast } from "@/hooks/useToast"; interface RoomManageModalProps { open: boolean; onClose: () => void; roomId: string; userId: string; users: string[]; locked: boolean; swipeCounts: Record; totalCards: number; userProfiles: Record; } export default function RoomManageModal({ open, onClose, roomId, userId, users, locked, swipeCounts, totalCards, userProfiles, }: RoomManageModalProps) { const toast = useToast(); const [loading, setLoading] = useState(null); const [confirmKick, setConfirmKick] = useState(null); const [confirmEnd, setConfirmEnd] = useState(false); const manage = useCallback( async (action: string, targetUserId?: string) => { setLoading(action + (targetUserId ?? "")); try { const res = await fetch(`/api/room/${roomId}/manage`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ userId, action, targetUserId }), }); if (!res.ok) { const data = await res.json().catch(() => ({})); toast.show(data.error ?? "操作失败"); return; } switch (action) { case "lock": toast.show("房间已锁定,其他人无法加入"); break; case "unlock": toast.show("房间已解锁"); break; case "kick": toast.show("已将该用户移出房间"); setConfirmKick(null); break; case "end_voting": toast.show("已结束投票,正在结算结果"); setConfirmEnd(false); onClose(); break; } } catch { toast.show("操作失败,请重试"); } finally { setLoading(null); } }, [roomId, userId, toast, onClose], ); const otherUsers = users.filter((u) => u !== userId); return (

房间管理

房间号 {roomId}

房间成员({users.length})

{users.map((uid) => { const profile = userProfiles[uid]; const emoji = profile?.avatar ?? getAvatar(uid).emoji; const bg = profile ? getAvatarBg(profile.avatar) : getAvatar(uid).bg; const displayName = profile?.username ?? uid.slice(0, 8); const isCreator = uid === userId; const swiped = swipeCounts[uid] ?? 0; const finished = swiped >= totalCards; return (
{emoji}
{isCreator && ( 房主 )} {displayName}
{swiped}/{totalCards} {finished ? " 已完成" : " 进行中"}
{!isCreator && ( <> {confirmKick === uid ? (
) : ( )} )}
); })}
{confirmEnd ? (

确定要结束投票吗?将根据当前已有的投票结果直接结算。

) : ( )}
); }