All files / web/src/components/nav PlayerConfigDialog.tsx

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

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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
import { useEffect, useRef, useState } from 'react'
import { EmojiPicker } from '@/arcade-games/matching/components/EmojiPicker'
import { useGameMode } from '../../contexts/GameModeContext'
import { generateUniquePlayerName } from '../../utils/playerNames'

interface PlayerConfigDialogProps {
  playerId: string
  onClose: () => void
}

export function PlayerConfigDialog({ playerId, onClose }: PlayerConfigDialogProps) {
  // All hooks must be called before early return
  const { getPlayer, updatePlayer, players } = useGameMode()
  const [showEmojiPicker, setShowEmojiPicker] = useState(false)
  const [localName, setLocalName] = useState('')
  const [isSaving, setIsSaving] = useState(false)
  const debounceTimerRef = useRef<NodeJS.Timeout | null>(null)

  const player = getPlayer(playerId)

  // Initialize local name from player
  useEffect(() => {
    if (player) {
      setLocalName(player.name)
    }
  }, [player])

  if (!player) {
    return null
  }

  const handleNameChange = (newName: string) => {
    setLocalName(newName)

    // Debounce the update to avoid too many API calls
    if (debounceTimerRef.current) {
      clearTimeout(debounceTimerRef.current)
    }

    setIsSaving(true)
    debounceTimerRef.current = setTimeout(() => {
      updatePlayer(playerId, { name: newName })
      setIsSaving(false)
    }, 500) // Wait 500ms after user stops typing
  }

  const handleEmojiSelect = (emoji: string) => {
    updatePlayer(playerId, { emoji })
    setShowEmojiPicker(false)
  }

  const handleGenerateNewName = () => {
    const allPlayers = Array.from(players.values())
    const existingNames = allPlayers.filter((p) => p.id !== playerId).map((p) => p.name)
    const newName = generateUniquePlayerName(existingNames, player.emoji)

    setLocalName(newName)
    updatePlayer(playerId, { name: newName })
  }

  // Get player number for UI theming (first 4 players get special colors)
  const allPlayers = Array.from(players.values()).sort((a, b) => {
    const aTime =
      typeof a.createdAt === 'number'
        ? a.createdAt
        : a.createdAt instanceof Date
          ? a.createdAt.getTime()
          : 0
    const bTime =
      typeof b.createdAt === 'number'
        ? b.createdAt
        : b.createdAt instanceof Date
          ? b.createdAt.getTime()
          : 0
    return aTime - bTime
  })
  const playerIndex = allPlayers.findIndex((p) => p.id === playerId)
  const displayNumber = playerIndex + 1

  // Color based on player's actual color
  const gradientColor = player.color

  if (showEmojiPicker) {
    return (
      <EmojiPicker
        currentEmoji={player.emoji}
        onEmojiSelect={handleEmojiSelect}
        onClose={() => setShowEmojiPicker(false)}
        playerNumber={displayNumber}
      />
    )
  }

  return (
    <div
      style={{
        position: 'fixed',
        top: 0,
        left: 0,
        right: 0,
        bottom: 0,
        background: 'rgba(0, 0, 0, 0.7)',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        zIndex: 1000,
        padding: '20px',
        animation: 'fadeIn 0.2s ease',
      }}
    >
      <div
        style={{
          background: 'white',
          borderRadius: '20px',
          padding: '32px',
          maxWidth: '400px',
          width: '100%',
          boxShadow: '0 20px 40px rgba(0, 0, 0, 0.3)',
          position: 'relative',
        }}
      >
        {/* Header */}
        <div
          style={{
            display: 'flex',
            justifyContent: 'space-between',
            alignItems: 'flex-start',
            marginBottom: '24px',
          }}
        >
          <div>
            <h2
              style={{
                fontSize: '24px',
                fontWeight: 'bold',
                background: `linear-gradient(135deg, ${gradientColor}, ${gradientColor}dd)`,
                backgroundClip: 'text',
                color: 'transparent',
                margin: 0,
                marginBottom: '4px',
              }}
            >
              Player Settings
            </h2>
            <div
              style={{
                fontSize: '12px',
                color: isSaving ? '#f59e0b' : '#10b981',
                fontWeight: 500,
                opacity: 0.8,
              }}
            >
              {isSaving ? '💾 Saving...' : '✓ Changes saved automatically'}
            </div>
          </div>
          <button
            onClick={onClose}
            style={{
              background: 'none',
              border: 'none',
              fontSize: '24px',
              cursor: 'pointer',
              color: '#6b7280',
              padding: '4px',
              lineHeight: 1,
            }}
            onMouseEnter={(e) => (e.currentTarget.style.color = '#1f2937')}
            onMouseLeave={(e) => (e.currentTarget.style.color = '#6b7280')}
          >
            ✕
          </button>
        </div>

        {/* Emoji Selection */}
        <div style={{ marginBottom: '24px' }}>
          <label
            style={{
              display: 'block',
              fontSize: '14px',
              fontWeight: 600,
              color: '#374151',
              marginBottom: '8px',
            }}
          >
            Character
          </label>
          <button
            onClick={() => setShowEmojiPicker(true)}
            style={{
              width: '100%',
              padding: '16px',
              background: 'linear-gradient(135deg, #f9fafb, #f3f4f6)',
              border: '2px solid #e5e7eb',
              borderRadius: '12px',
              cursor: 'pointer',
              transition: 'all 0.2s ease',
              display: 'flex',
              alignItems: 'center',
              gap: '12px',
            }}
            onMouseEnter={(e) => {
              e.currentTarget.style.borderColor = gradientColor
              e.currentTarget.style.transform = 'translateY(-2px)'
              e.currentTarget.style.boxShadow = '0 4px 12px rgba(0,0,0,0.1)'
            }}
            onMouseLeave={(e) => {
              e.currentTarget.style.borderColor = '#e5e7eb'
              e.currentTarget.style.transform = 'translateY(0)'
              e.currentTarget.style.boxShadow = 'none'
            }}
          >
            <div
              style={{
                fontSize: '48px',
                lineHeight: 1,
              }}
            >
              {player.emoji}
            </div>
            <div
              style={{
                flex: 1,
                textAlign: 'left',
              }}
            >
              <div
                style={{
                  fontSize: '14px',
                  fontWeight: 600,
                  color: '#1f2937',
                  marginBottom: '4px',
                }}
              >
                Click to change character
              </div>
              <div
                style={{
                  fontSize: '12px',
                  color: '#6b7280',
                }}
              >
                Choose from hundreds of emojis
              </div>
            </div>
            <div
              style={{
                fontSize: '20px',
                color: '#9ca3af',
              }}
            >
              →
            </div>
          </button>
        </div>

        {/* Name Input */}
        <div>
          <label
            style={{
              display: 'block',
              fontSize: '14px',
              fontWeight: 600,
              color: '#374151',
              marginBottom: '8px',
            }}
          >
            Name
          </label>
          <div
            style={{
              display: 'flex',
              gap: '8px',
              alignItems: 'flex-start',
            }}
          >
            <div style={{ flex: 1 }}>
              <input
                type="text"
                value={localName}
                onChange={(e) => handleNameChange(e.target.value)}
                placeholder="Player Name"
                maxLength={20}
                style={{
                  width: '100%',
                  padding: '12px 16px',
                  fontSize: '16px',
                  border: '2px solid #e5e7eb',
                  borderRadius: '12px',
                  outline: 'none',
                  transition: 'all 0.2s ease',
                  fontWeight: 500,
                }}
                onFocus={(e) => {
                  e.currentTarget.style.borderColor = gradientColor
                  e.currentTarget.style.boxShadow = `0 0 0 3px ${gradientColor}20`
                }}
                onBlur={(e) => {
                  e.currentTarget.style.borderColor = '#e5e7eb'
                  e.currentTarget.style.boxShadow = 'none'
                }}
              />
              <div
                style={{
                  fontSize: '12px',
                  color: '#6b7280',
                  marginTop: '6px',
                }}
              >
                {localName.length}/20 characters
              </div>
            </div>
            <div style={{ flexShrink: 0 }}>
              <button
                type="button"
                onClick={handleGenerateNewName}
                style={{
                  padding: '12px 16px',
                  background: `linear-gradient(135deg, ${gradientColor}, ${gradientColor}dd)`,
                  border: 'none',
                  borderRadius: '12px',
                  color: 'white',
                  fontSize: '20px',
                  cursor: 'pointer',
                  transition: 'all 0.2s ease',
                  display: 'flex',
                  alignItems: 'center',
                  justifyContent: 'center',
                }}
                onMouseEnter={(e) => {
                  e.currentTarget.style.transform = 'scale(1.05)'
                  e.currentTarget.style.boxShadow = `0 4px 12px ${gradientColor}40`
                }}
                onMouseLeave={(e) => {
                  e.currentTarget.style.transform = 'scale(1)'
                  e.currentTarget.style.boxShadow = 'none'
                }}
                title="Generate random name"
              >
                🎲
              </button>
              <div
                style={{
                  fontSize: '12px',
                  color: '#6b7280',
                  marginTop: '6px',
                  textAlign: 'center',
                }}
              >
                Random name
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  )
}