import type { TeamTournamentCodeView } from "@lolympus/contracts"; import { Check, Copy, LoaderCircle } from "lucide-react"; import { useEffect, useLayoutEffect, useRef, useState } from "react"; import { copyTournamentCode, getNextTournamentCodeExpiryDelay, getTournamentCodeCopyLabel, getTournamentCodeExpiryLabel, getTournamentCodePresentation, type TournamentCodeCopyFeedback, type TournamentCodeTone, } from "../../pages/match-team-progress-view"; const CODE_TONE_CLASS: Record = { pending: "border-base-content/15 bg-base-200 text-base-content", available: "border-primary/50 bg-primary/10 text-base-content", expired: "border-error/50 bg-error/10 text-base-content", used: "border-base-content/20 bg-base-200 text-base-content", }; type VisibleFeedback = TournamentCodeCopyFeedback & { gameSlotId: string }; export function TeamTournamentCodes({ assignments, seriesLabel, }: { assignments: readonly TeamTournamentCodeView[]; seriesLabel: string; }) { const [copyingSlotId, setCopyingSlotId] = useState(null); const [feedback, setFeedback] = useState(null); const [now, setNow] = useState(() => Date.now()); const clearFeedbackTimeoutRef = useRef(null); useEffect( () => () => { if (clearFeedbackTimeoutRef.current !== null) { window.clearTimeout(clearFeedbackTimeoutRef.current); } }, [], ); useEffect(() => { const delay = getNextTournamentCodeExpiryDelay(assignments, now); if (delay === null) return; const timeoutId = window.setTimeout( () => setNow(Date.now()), Math.max(1, delay), ); return () => window.clearTimeout(timeoutId); }, [assignments, now]); if (assignments.length === 0) { return (

표시할 세트 정보가 아직 없습니다.

); } async function handleCopy(assignment: TeamTournamentCodeView) { if (!assignment.code || copyingSlotId !== null) return; if (clearFeedbackTimeoutRef.current !== null) { window.clearTimeout(clearFeedbackTimeoutRef.current); clearFeedbackTimeoutRef.current = null; } const slotId = String(assignment.gameSlotId); const gameLabel = `${seriesLabel} ${assignment.ordinal}세트`; const currentTime = readCurrentTime(); setNow(currentTime); if (!getTournamentCodePresentation(assignment, currentTime).canCopy) { setFeedback({ kind: "error", gameSlotId: slotId, message: `${gameLabel} 코드는 만료되었거나 사용할 수 없어 복사하지 않았습니다.`, }); return; } setCopyingSlotId(slotId); setFeedback(null); const clipboard = navigator.clipboard; const result = await copyTournamentCode( clipboard?.writeText ? clipboard.writeText.bind(clipboard) : undefined, assignment.code, gameLabel, ); setCopyingSlotId(null); setFeedback({ ...result, gameSlotId: slotId }); if (result.kind === "success") { clearFeedbackTimeoutRef.current = window.setTimeout(() => { setFeedback(null); clearFeedbackTimeoutRef.current = null; }, 2500); } } function renderAssignment(assignment: TeamTournamentCodeView) { const slotId = String(assignment.gameSlotId); const presentation = getTournamentCodePresentation(assignment, now); const isCopying = copyingSlotId === slotId; const copied = feedback?.kind === "success" && feedback.gameSlotId === slotId; return (
  • {assignment.ordinal}세트

    대회 경기 #{assignment.displayNumber}

    {presentation.label}
    {assignment.code ? ( ) : (

    발급되면 이곳에서 복사할 수 있습니다.

    )} {assignment.code && assignment.state === "reserved" && assignment.expiresAt && (

    )}
    {assignment.code && assignment.state === "reserved" && ( )}
  • ); } return (

    토너먼트 코드

    {assignments.length}세트
      {assignments.map(renderAssignment)}
    {feedback && (

    {feedback.message}

    )}
    ); } function readCurrentTime(): number { return Date.now(); } function AutoSizingCodeField({ label, value, }: { label: string; value: string; }) { const fieldRef = useRef(null); useLayoutEffect(() => { const resizeToContent = () => { const field = fieldRef.current; if (!field) return; field.style.height = "auto"; const borderHeight = field.offsetHeight - field.clientHeight; field.style.height = `${field.scrollHeight + borderHeight}px`; }; resizeToContent(); window.addEventListener("resize", resizeToContent); return () => window.removeEventListener("resize", resizeToContent); }, [value]); return (