All files / components/screens/intro/components ArchetypeCardGridOverlayHtml.tsx

93.18% Statements 41/44
92.68% Branches 38/41
76.92% Functions 10/13
92.85% Lines 39/42

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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                                          3x                     35x   35x   35x 40x     35x 35x 32x 32x     35x 35x               35x   10x 10x         35x   1x 1x         35x   8x 3x   3x 3x 3x 5x 3x 3x 3x 3x 2x 2x 2x 1x                           35x 35x   35x   35x                                                                                                                                                             91x       4x                                                                     3x      
import React, { useCallback, useMemo } from "react";
import { PlayerArchetype } from "../../../../types/common";
import { FONT_FAMILY, KOREAN_COLORS } from "../../../../types/constants";
import { hexColorToCSS, hexToRgbaString } from "../../../../utils/colorUtils";
import { ArchetypeCard, ArchetypeCardData } from "./ArchetypeCardOverlayHtml";
 
export interface ArchetypeCardGridProps {
  readonly archetypes: readonly ArchetypeCardData[];
  readonly selectedArchetype: PlayerArchetype;
  readonly onArchetypeChange: (archetype: PlayerArchetype) => void;
  readonly onArchetypeConfirm?: (archetype: PlayerArchetype) => void;
  readonly onPlaySFX: (sound: string) => void;
  readonly width?: number;
  readonly height?: number;
  readonly isMobile?: boolean;
}
 
/**
 * ArchetypeCardGrid - Grid layout for displaying multiple archetype cards
 * Provides an enhanced selection interface with detailed preview cards
 */
export const ArchetypeCardGrid: React.FC<ArchetypeCardGridProps> = React.memo(
  ({
    archetypes,
    selectedArchetype,
    onArchetypeChange,
    onArchetypeConfirm,
    onPlaySFX,
    width = 900,
    height = 600,
    isMobile: _isMobile = false, // Kept for interface compatibility, layout uses width
  }) => {
    void _isMobile;
 
    const isSmallScreen = width < 768;
 
    const selectedIndex = useMemo(() => {
      return archetypes.findIndex((a) => a.archetype === selectedArchetype);
    }, [archetypes, selectedArchetype]);
 
    const cardWidth = useMemo(() => {
      if (isSmallScreen) return Math.min(280, width - 40);
      const isLargeContainer = width >= 1100;
      return isLargeContainer ? 340 : 380;
    }, [isSmallScreen, width]);
 
    const colors = useMemo(
      () => ({
        background: hexToRgbaString(KOREAN_COLORS.UI_BACKGROUND_DARK, 0.95),
        border: hexToRgbaString(KOREAN_COLORS.PRIMARY_CYAN, 0.7),
        headerColor: hexColorToCSS(KOREAN_COLORS.ACCENT_GOLD),
      }),
      [],
    );
 
    const handleCardSelect = useCallback(
      (archetype: PlayerArchetype) => {
        onArchetypeChange(archetype);
        onPlaySFX("menu_hover");
      },
      [onArchetypeChange, onPlaySFX],
    );
 
    const handleCardConfirm = useCallback(
      (archetype: PlayerArchetype) => {
        onArchetypeConfirm?.(archetype);
        onPlaySFX("menu_select");
      },
      [onArchetypeConfirm, onPlaySFX],
    );
 
    const handleKeyDown = useCallback(
      (event: React.KeyboardEvent<HTMLDivElement>) => {
        if (event.key === "ArrowLeft" || event.key === "ArrowUp") {
          event.preventDefault();
          const newIndex =
            selectedIndex === 0 ? archetypes.length - 1 : selectedIndex - 1;
          const newArchetype = archetypes[newIndex].archetype;
          handleCardSelect(newArchetype);
        } else if (event.key === "ArrowRight" || event.key === "ArrowDown") {
          event.preventDefault();
          const newIndex = (selectedIndex + 1) % archetypes.length;
          const newArchetype = archetypes[newIndex].archetype;
          handleCardSelect(newArchetype);
        } else Eif (event.key === "Enter") {
          event.preventDefault();
          if (onArchetypeConfirm) {
            handleCardConfirm(selectedArchetype);
          }
        }
      },
      [
        selectedIndex,
        archetypes,
        selectedArchetype,
        handleCardSelect,
        handleCardConfirm,
        onArchetypeConfirm,
      ],
    );
 
    const columnsCount = isSmallScreen ? 1 : width >= 1400 ? 3 : 2;
    const gap = isSmallScreen ? 16 : 20;
 
    const [isFocused, setIsFocused] = React.useState(false);
 
    return (
      <div
        tabIndex={0}
        onKeyDown={handleKeyDown}
        onFocus={() => setIsFocused(true)}
        onBlur={() => setIsFocused(false)}
        style={{
          width: `${width}px`,
          minHeight: `${height}px`,
          display: "flex",
          flexDirection: "column",
          alignItems: "center",
          gap: `${gap}px`,
          background: colors.background,
          borderRadius: "12px",
          border: `2px solid ${colors.border}`,
          padding: isSmallScreen ? "16px" : "24px",
          overflow: "auto",
          maxHeight: `${height}px`,
          outline: isFocused ? `3px solid ${colors.headerColor}` : "none",
          outlineOffset: "2px",
        }}
        data-testid="archetype-card-grid"
        role="region"
        aria-label="Archetype selection grid"
      >
        {/* Header */}
        <div
          style={{
            width: "100%",
            display: "flex",
            flexDirection: "column",
            alignItems: "center",
            gap: "8px",
            marginBottom: `${gap / 2}px`,
          }}
        >
          <h2
            style={{
              fontSize: isSmallScreen ? "20px" : "28px",
              fontWeight: "bold",
              fontFamily: FONT_FAMILY.KOREAN,
              color: colors.headerColor,
              margin: 0,
              textAlign: "center",
            }}
            data-testid="grid-header"
          >
            원형 선택 | Select Archetype
          </h2>
 
          <div
            style={{
              fontSize: isSmallScreen ? "12px" : "14px",
              fontFamily: FONT_FAMILY.KOREAN,
              color: hexColorToCSS(KOREAN_COLORS.TEXT_SECONDARY),
              textAlign: "center",
              fontStyle: "italic",
            }}
            data-testid="grid-hint"
          >
            {isSmallScreen
              ? "카드를 탭하여 선택"
              : "화살표 키로 탐색, 엔터로 확인 | Arrow keys to navigate, Enter to confirm"}
          </div>
        </div>
 
        {/* Card Grid */}
        <div
          style={{
            display: "grid",
            gridTemplateColumns: `repeat(${columnsCount}, 1fr)`,
            gap: `${gap}px`,
            width: "100%",
            justifyItems: "center",
          }}
          data-testid="card-grid-container"
        >
          {archetypes.map((archetype) => (
            <ArchetypeCard
              key={archetype.id}
              data={archetype}
              isSelected={archetype.archetype === selectedArchetype}
              onSelect={() => handleCardSelect(archetype.archetype)}
              onConfirm={
                onArchetypeConfirm
                  ? () => handleCardConfirm(archetype.archetype)
                  : undefined
              }
              isMobile={isSmallScreen}
              width={cardWidth}
              showSelectButton={!!onArchetypeConfirm}
            />
          ))}
        </div>
 
        {/* Footer navigation hint */}
        {!isSmallScreen && (
          <div
            style={{
              marginTop: `${gap}px`,
              fontSize: "12px",
              fontFamily: FONT_FAMILY.KOREAN,
              color: hexColorToCSS(KOREAN_COLORS.TEXT_SECONDARY),
              textAlign: "center",
              fontStyle: "italic",
            }}
            data-testid="grid-footer"
          >
            ← → 또는 ↑ ↓ 키로 원형 변경 | Use ← → or ↑ ↓ keys to change
            archetype
          </div>
        )}
      </div>
    );
  },
);
 
ArchetypeCardGrid.displayName = "ArchetypeCardGrid";
 
export default ArchetypeCardGrid;