All files / web/src/arcade-games/know-your-world/components ContinentSelector.tsx

0% Statements 0/259
0% Branches 0/1
0% Functions 0/1
0% Lines 0/259

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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
'use client'

import { useState } from 'react'
import { css } from '@styled/css'
import { useTheme } from '@/contexts/ThemeContext'
import { WORLD_MAP } from '../maps'
import { getContinentForCountry, CONTINENTS, type ContinentId } from '../continents'
import { getRegionColor } from '../mapColors'

interface ContinentSelectorProps {
  selectedContinent: ContinentId | 'all' | null
  onSelectContinent: (continent: ContinentId | 'all') => void
}

export function ContinentSelector({
  selectedContinent,
  onSelectContinent,
}: ContinentSelectorProps) {
  const { resolvedTheme } = useTheme()
  const isDark = resolvedTheme === 'dark'
  const [hoveredContinent, setHoveredContinent] = useState<ContinentId | 'all' | null>(null)
  const [hoveredRegion, setHoveredRegion] = useState<string | null>(null)

  // Group regions by continent
  const regionsByContinent = new Map<ContinentId | 'all', typeof WORLD_MAP.regions>()
  regionsByContinent.set('all', []) // Initialize all continents

  CONTINENTS.forEach((continent) => {
    regionsByContinent.set(continent.id, [])
  })

  WORLD_MAP.regions.forEach((region) => {
    const continent = getContinentForCountry(region.id)
    if (continent) {
      regionsByContinent.get(continent)?.push(region)
    }
  })

  // Get color for a region based on its continent's state
  const getRegionColorForSelector = (
    regionId: string,
    continentId: ContinentId | 'all'
  ): string => {
    const isSelected = selectedContinent === continentId
    const isHovered = hoveredContinent === continentId || hoveredRegion === regionId

    // Use the game's color algorithm, but adjust opacity based on selection state
    const baseColor = getRegionColor(regionId, isSelected, isHovered, isDark)

    // If this continent is not selected and not hovered, make it more transparent
    if (!isSelected && !isHovered) {
      // Extract the color and add low opacity
      return baseColor.includes('#') ? `${baseColor}33` : baseColor // 20% opacity
    }

    return baseColor
  }

  const getRegionStroke = (continentId: ContinentId | 'all', regionId: string): string => {
    const isSelected = selectedContinent === continentId
    const isHovered = hoveredContinent === continentId || hoveredRegion === regionId

    if (isSelected) {
      return isDark ? '#60a5fa' : '#1d4ed8'
    }
    if (isHovered) {
      return isDark ? '#93c5fd' : '#3b82f6'
    }
    return isDark ? '#374151' : '#9ca3af'
  }

  const getRegionStrokeWidth = (continentId: ContinentId | 'all', regionId: string): number => {
    const isSelected = selectedContinent === continentId
    const isHovered = hoveredContinent === continentId || hoveredRegion === regionId

    if (isHovered) return 1.5
    if (isSelected) return 1
    return 0.3
  }

  return (
    <div data-component="continent-selector">
      <div
        className={css({
          fontSize: 'sm',
          color: isDark ? 'gray.400' : 'gray.600',
          textAlign: 'center',
          marginBottom: '2',
        })}
      >
        Click the map to focus on a continent
      </div>

      {/* Interactive Map */}
      <div
        className={css({
          width: '100%',
          padding: '4',
          bg: isDark ? 'gray.900' : 'gray.50',
          rounded: 'xl',
          border: '2px solid',
          borderColor: isDark ? 'gray.700' : 'gray.200',
        })}
      >
        <svg
          viewBox={WORLD_MAP.viewBox}
          className={css({
            width: '100%',
            height: 'auto',
            cursor: 'pointer',
          })}
        >
          {/* Background */}
          <rect x="0" y="0" width="100%" height="100%" fill={isDark ? '#111827' : '#f9fafb'} />

          {/* Render each continent as a group */}
          {CONTINENTS.map((continent) => {
            const regions = regionsByContinent.get(continent.id) || []
            if (regions.length === 0) return null

            return (
              <g key={continent.id} data-continent={continent.id}>
                {/* All regions in this continent - each individually clickable */}
                {regions.map((region) => (
                  <path
                    key={region.id}
                    d={region.path}
                    fill={getRegionColorForSelector(region.id, continent.id)}
                    stroke={getRegionStroke(continent.id, region.id)}
                    strokeWidth={getRegionStrokeWidth(continent.id, region.id)}
                    onMouseEnter={() => {
                      setHoveredContinent(continent.id)
                      setHoveredRegion(region.id)
                    }}
                    onMouseLeave={() => {
                      setHoveredContinent(null)
                      setHoveredRegion(null)
                    }}
                    onClick={(e) => {
                      e.stopPropagation()
                      onSelectContinent(continent.id)
                    }}
                    style={{
                      cursor: 'pointer',
                      transition: 'all 0.15s ease',
                      pointerEvents: 'all',
                    }}
                  />
                ))}
              </g>
            )
          })}
        </svg>
      </div>

      {/* Featured "All" option - full width */}
      <button
        data-action="select-all-continents"
        onClick={() => onSelectContinent('all')}
        onMouseEnter={() => setHoveredContinent('all')}
        onMouseLeave={() => setHoveredContinent(null)}
        className={css({
          width: '100%',
          padding: '3',
          marginTop: '3',
          rounded: 'lg',
          border: '2px solid',
          borderColor:
            selectedContinent === 'all' || selectedContinent === null ? 'blue.500' : 'transparent',
          bg:
            selectedContinent === 'all' || selectedContinent === null
              ? isDark
                ? 'blue.900'
                : 'blue.50'
              : hoveredContinent === 'all'
                ? isDark
                  ? 'gray.700'
                  : 'gray.200'
                : isDark
                  ? 'gray.800'
                  : 'gray.100',
          color: isDark ? 'gray.100' : 'gray.900',
          cursor: 'pointer',
          transition: 'all 0.2s',
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          gap: '2',
          _hover: {
            borderColor: 'blue.400',
          },
        })}
      >
        <span className={css({ fontSize: 'xl' })}>🌍</span>
        <span className={css({ fontWeight: 'bold' })}>Explore All 256 Countries</span>
      </button>

      {/* Continent buttons - smaller, for focusing */}
      <div
        className={css({
          fontSize: 'xs',
          color: isDark ? 'gray.500' : 'gray.500',
          marginTop: '3',
          marginBottom: '2',
          textAlign: 'center',
        })}
      >
        Or focus on a continent:
      </div>
      <div
        className={css({
          display: 'grid',
          gridTemplateColumns: 'repeat(6, 1fr)',
          gap: '2',
        })}
      >
        {/* Continent buttons */}
        {CONTINENTS.map((continent) => (
          <button
            key={continent.id}
            data-action={`select-${continent.id}-continent`}
            onClick={() => onSelectContinent(continent.id)}
            onMouseEnter={() => setHoveredContinent(continent.id)}
            onMouseLeave={() => setHoveredContinent(null)}
            className={css({
              padding: '2',
              rounded: 'lg',
              border: '2px solid',
              borderColor: selectedContinent === continent.id ? 'blue.500' : 'transparent',
              bg:
                selectedContinent === continent.id
                  ? isDark
                    ? 'blue.900'
                    : 'blue.50'
                  : hoveredContinent === continent.id
                    ? isDark
                      ? 'gray.700'
                      : 'gray.200'
                    : isDark
                      ? 'gray.800'
                      : 'gray.100',
              color: isDark ? 'gray.100' : 'gray.900',
              cursor: 'pointer',
              transition: 'all 0.2s',
              fontSize: 'xs',
              fontWeight: selectedContinent === continent.id ? 'bold' : 'normal',
              _hover: {
                borderColor: 'blue.400',
              },
            })}
          >
            <div className={css({ fontSize: 'lg' })}>{continent.emoji}</div>
            <div>{continent.name}</div>
          </button>
        ))}
      </div>
    </div>
  )
}