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

100% Statements 24/24
95.65% Branches 22/23
100% Functions 11/11
100% Lines 21/21

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                                                  2x                     40x         40x     40x 64x   133x                             40x 25x 25x 29x         40x 15x 15x       40x   4x 2x 2x             40x 25x       40x                                                                                                                   2x      
import React, { useCallback, useMemo, useState } from "react";
import { PlayerArchetype } from "../../../../types/common";
import { BaseButtonOverlayHtml } from "../../../shared/base/BaseButtonOverlayHtml";
import { ArchetypeCardGrid } from "./ArchetypeCardGridOverlayHtml";
import {
  ArchetypeDataShape,
  ArchetypeDisplayOverlayHtml,
} from "./ArchetypeDisplayOverlayHtml";
 
export interface EnhancedArchetypeDisplayProps {
  readonly archetypes: readonly ArchetypeDataShape[];
  readonly selectedIndex: number;
  readonly onArchetypeChange: (index: number) => void;
  readonly onPlaySFX: (sound: string) => void;
  readonly width?: number;
  readonly height?: number;
  readonly isMobile?: boolean; // For controls/haptics only, use width for layout sizing
  readonly allowDetailedView?: boolean; // Allow switching to card grid view
}
 
/**
 * EnhancedArchetypeDisplay - Provides both compact and detailed card view modes
 * Can switch between ArchetypeDisplayOverlayHtml (compact) and ArchetypeCardGrid (detailed)
 */
export const EnhancedArchetypeDisplay: React.FC<EnhancedArchetypeDisplayProps> =
  React.memo(
    ({
      archetypes,
      selectedIndex,
      onArchetypeChange,
      onPlaySFX,
      width = 800,
      height = 300,
      isMobile = false,
      allowDetailedView = true,
    }) => {
      const [viewMode, setViewMode] = useState<"compact" | "detailed">(
        "compact",
      );
 
      // Use width for layout sizing decisions
      const isSmallScreen = width < 768;
 
      // Convert archetype data to card data format
      const cardData = useMemo(() => {
        return archetypes.map((archetype) => ({
          archetype: Object.values(PlayerArchetype).find(
            (key) => key === archetype.id,
          ) as PlayerArchetype,
          id: archetype.id,
          korean: archetype.korean,
          english: archetype.english,
          description: archetype.description,
          color: archetype.color,
          textureKey: archetype.textureKey,
          stats: archetype.stats,
          philosophy: archetype.philosophy,
          specialAbilities: archetype.specialAbilities ?? [], // Use actual data or empty array
        }));
      }, [archetypes]);
 
      // Get current archetype
      const currentArchetype = useMemo(() => {
        const archetype = archetypes[selectedIndex];
        return Object.values(PlayerArchetype).find(
          (key) => key === archetype.id,
        ) as PlayerArchetype;
      }, [archetypes, selectedIndex]);
 
      // Toggle view mode
      const handleToggleView = useCallback(() => {
        setViewMode((prev) => (prev === "compact" ? "detailed" : "compact"));
        onPlaySFX("menu_hover");
      }, [onPlaySFX]);
 
      // Handle archetype change from card grid
      const handleArchetypeChangeFromGrid = useCallback(
        (archetype: PlayerArchetype) => {
          const index = archetypes.findIndex((a) => a.id === archetype);
          Eif (index !== -1) {
            onArchetypeChange(index);
          }
        },
        [archetypes, onArchetypeChange],
      );
 
      // Memoize height calculation for detailed view (minimum 600px or 2x compact height)
      const detailedHeight = useMemo(
        () => Math.max(height * 2, 600),
        [height],
      );
 
      return (
        <div
          style={{
            width: `${width}px`,
            display: "flex",
            flexDirection: "column",
            gap: isSmallScreen ? "10px" : "16px",
          }}
          data-testid="enhanced-archetype-display"
        >
          {/* View toggle button (only show if allowed and not small screen) */}
          {allowDetailedView && !isSmallScreen && (
            <div
              style={{
                display: "flex",
                justifyContent: "flex-end",
                width: "100%",
              }}
            >
              <BaseButtonOverlayHtml
                korean={viewMode === "compact" ? "상세 보기" : "간단 보기"}
                english={viewMode === "compact" ? "Detailed View" : "Compact View"}
                onClick={handleToggleView}
                variant="secondary"
                size="sm"
                testId="view-toggle-button"
                ariaLabel={`Toggle ${viewMode === "compact" ? "detailed" : "compact"} view`}
              />
            </div>
          )}
 
          {/* Render appropriate view */}
          {viewMode === "compact" ? (
            <ArchetypeDisplayOverlayHtml
              archetypes={archetypes}
              selectedIndex={selectedIndex}
              onArchetypeChange={onArchetypeChange}
              onPlaySFX={onPlaySFX}
              width={width}
              height={height}
              isMobile={isMobile}
            />
          ) : (
            <ArchetypeCardGrid
              archetypes={cardData}
              selectedArchetype={currentArchetype}
              onArchetypeChange={handleArchetypeChangeFromGrid}
              onPlaySFX={onPlaySFX}
              width={width}
              height={detailedHeight}
              isMobile={isMobile}
            />
          )}
        </div>
      );
    },
  );
 
EnhancedArchetypeDisplay.displayName = "EnhancedArchetypeDisplay";
 
export default EnhancedArchetypeDisplay;