All files / components/screens/combat/components/feedback RoundStartAnnouncement.tsx

100% Statements 34/34
76.92% Branches 10/13
100% Functions 13/13
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186                                                                                                  3x           10x 10x       10x 10x 10x       10x 2x       10x 10x 10x       10x 10x 10x         10x 10x 10x   10x 2x 2x 2x 2x         10x 10x 10x 10x 2x           10x 10x 10x       10x                                                                                                                                                
/**
 * RoundStartAnnouncement Component - Displays "Round X Begin!" announcement
 *
 * Korean: 라운드 시작 발표 (Round Start Announcement)
 *
 * Shows "Round X Begin!" for subsequent rounds (not the first round).
 * Implements Korean cyberpunk aesthetic with bilingual text support.
 *
 * @module components/combat/RoundStartAnnouncement
 * @category Combat UI
 */
 
import React, {
  useCallback,
  useEffect,
  useMemo,
  useRef,
  useState,
} from "react";
import { useAudio } from "../../../../../audio/AudioProvider";
import { FONT_FAMILY, KOREAN_COLORS } from "../../../../../types/constants";
import { hexColorToCSS } from "../../../../../utils/colorUtils";
 
/**
 * Props for the RoundStartAnnouncement component
 */
export interface RoundStartAnnouncementProps {
  /** Round number (1-based) */
  readonly roundNumber: number;
  /** Duration to display announcement in seconds */
  readonly duration?: number;
  /** Callback when announcement completes */
  readonly onComplete: () => void;
  /** Whether layout should adapt for mobile screens */
  readonly isMobile: boolean;
}
 
/**
 * RoundStartAnnouncement Component
 *
 * Displays "Round X Begin!" announcement with:
 * - Bilingual round number and "Begin!" text
 * - Flash/pulse animation for impact
 * - Auto-dismiss after configured duration
 * - Audio cue for round start
 * - Responsive sizing for mobile/tablet/desktop
 *
 * Korean: 라운드 시작 발표 컴포넌트
 */
export const RoundStartAnnouncement: React.FC<RoundStartAnnouncementProps> = ({
  roundNumber,
  duration = 2,
  onComplete,
  isMobile,
}) => {
  const audio = useAudio();
  const [isVisible, setIsVisible] = useState(false);
 
  // Use ref to stabilize onComplete callback - prevents timer reset on re-renders
  // This is critical because parent component may recreate onComplete due to state changes
  const onCompleteRef = useRef(onComplete);
  useEffect(() => {
    onCompleteRef.current = onComplete;
  }, [onComplete]);
 
  // Stable callback that reads from ref
  const handleComplete = useCallback(() => {
    onCompleteRef.current();
  }, []);
 
  // Fade in animation on mount
  useEffect(() => {
    const timer = setTimeout(() => setIsVisible(true), 50);
    return () => clearTimeout(timer);
  }, []);
 
  // Play audio cue on mount
  useEffect(() => {
    Eif (audio.isAudioReady) {
      audio.playSFX("attack_medium"); // Using placeholder - will be round_start
    }
  }, [audio]);
 
  // Auto-dismiss after duration - uses stable handleComplete to prevent timer resets
  useEffect(() => {
    let isMounted = true;
    let innerTimer: ReturnType<typeof setTimeout> | null = null;
 
    const outerTimer = setTimeout(() => {
      setIsVisible(false);
      innerTimer = setTimeout(() => {
        Eif (isMounted) {
          handleComplete();
        }
      }, 300); // Wait for fade out
    }, duration * 1000);
 
    return () => {
      isMounted = false;
      clearTimeout(outerTimer);
      if (innerTimer) {
        clearTimeout(innerTimer);
      }
    };
  }, [duration, handleComplete]);
 
  // Convert hex colors to CSS - memoized for performance
  const goldColor = useMemo(() => hexColorToCSS(KOREAN_COLORS.ACCENT_GOLD), []);
  const darkBg = useMemo(
    () => hexColorToCSS(KOREAN_COLORS.UI_BACKGROUND_DARK),
    []
  );
 
  return (
    <>
      <div
        data-testid="round-start-announcement"
        role="alert"
        aria-live="assertive"
        aria-label={`Round ${roundNumber} starting`}
        style={{
          position: "fixed",
          top: 0,
          left: 0,
          width: "100%",
          height: "100%",
          display: "flex",
          alignItems: "center",
          justifyContent: "center",
          backgroundColor: `${darkBg}88`,
          zIndex: 900,
          opacity: isVisible ? 1 : 0,
          transition: "opacity 0.3s ease-in-out",
          pointerEvents: "none",
        }}
      >
        <div
          style={{
            fontSize: isMobile ? "56px" : "96px",
            fontWeight: "bold",
            color: goldColor,
            fontFamily: FONT_FAMILY.KOREAN,
            textShadow: `0 0 40px ${goldColor}`,
            animation: "roundStartFlash 0.5s ease-out",
            textAlign: "center",
            userSelect: "none",
          }}
          data-testid="round-start-text"
        >
          라운드 {roundNumber} 시작!
          <br />
          <span
            style={{
              fontSize: isMobile ? "40px" : "64px",
            }}
          >
            Round {roundNumber} Begin!
          </span>
        </div>
      </div>
 
      {/* CSS Animation */}
      <style>
        {`
          @keyframes roundStartFlash {
            0% {
              opacity: 0;
              transform: scale(1.5);
            }
            30% {
              opacity: 1;
              transform: scale(1.2);
            }
            100% {
              opacity: 1;
              transform: scale(1);
            }
          }
        `}
      </style>
    </>
  );
};
 
export default RoundStartAnnouncement;