import { Check, CircleAlert, Copy, Hash } from "lucide-react"; import { useEffect, useRef, useState } from "react"; import { copyRoomCode } from "./room-code"; type CopyState = "idle" | "copied" | "failed"; export function RoomCode({ roomId }: { roomId: string }) { const [copyState, setCopyState] = useState("idle"); const resetTimerRef = useRef(null); const mountedRef = useRef(false); useEffect(() => { mountedRef.current = true; return () => { mountedRef.current = false; if (resetTimerRef.current !== null) { window.clearTimeout(resetTimerRef.current); } }; }, []); async function copy(): Promise { if (resetTimerRef.current !== null) { window.clearTimeout(resetTimerRef.current); resetTimerRef.current = null; } try { await copyRoomCode(roomId); if (!mountedRef.current) { return; } setCopyState("copied"); resetTimerRef.current = window.setTimeout(() => { setCopyState("idle"); resetTimerRef.current = null; }, 1_500); } catch { if (mountedRef.current) { setCopyState("failed"); resetTimerRef.current = window.setTimeout(() => { setCopyState("idle"); resetTimerRef.current = null; }, 1_500); } } } const copyLabel = copyState === "copied" ? "房间号已复制" : copyState === "failed" ? "复制失败,请手动复制房间号" : `复制房间号 ${roomId}`; return ( ); }