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

62.79% Statements 324/516
4% Branches 1/25
50% Functions 1/2
62.79% Lines 324/516

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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 5171x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                                                                                                                                             2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                                                                                                                                                           2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x       2x 2x 2x 2x   2x 2x       2x 2x 2x 2x   2x 2x       2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                                                             2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x   2x 2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x  
import { useRoomData } from '@/hooks/useRoomData'
import { useUserId } from '@/hooks/useUserId'
import { ActivePlayersList } from './ActivePlayersList'
import { AddPlayerButton } from './AddPlayerButton'
import { FullscreenPlayerSelection } from './FullscreenPlayerSelection'
import { GameModeIndicator } from './GameModeIndicator'
import { GameTitleMenu } from './GameTitleMenu'
import { NetworkPlayerIndicator } from './NetworkPlayerIndicator'
import { PendingInvitations } from './PendingInvitations'
import { RoomInfo } from './RoomInfo'
import type { PlayerBadge } from './types'
 
type GameMode = 'none' | 'single' | 'battle' | 'tournament'
 
interface Player {
  id: string
  name: string
  emoji: string
}
 
interface NetworkPlayer {
  id: string
  emoji?: string
  name?: string
  isOnline?: boolean
  userId?: string // The user ID controlling this player
}
 
interface ArcadeRoomInfo {
  roomId?: string
  roomName?: string
  gameName: string | null
  playerCount: number
  joinCode?: string
}
 
export interface RosterWarningAction {
  label: string
  onClick: () => void
  variant?: 'primary' | 'danger'
}
 
export interface RosterWarning {
  heading: string
  description: string
  actions?: RosterWarningAction[]
}
 
interface GameContextNavProps {
  navTitle: string
  navEmoji?: string
  gameName?: 'matching' | 'memory-quiz' | 'complement-race'
  gameMode: GameMode
  activePlayers: Player[]
  inactivePlayers: Player[]
  shouldEmphasize: boolean
  showFullscreenSelection: boolean
  onAddPlayer: (playerId: string) => void
  onRemovePlayer: (playerId: string) => void
  onConfigurePlayer: (playerId: string) => void
  onExitSession?: () => void
  onSetup?: () => void
  onNewGame?: () => void
  // Arcade session info
  networkPlayers?: NetworkPlayer[]
  roomInfo?: ArcadeRoomInfo
  // Game state for turn indicator
  currentPlayerId?: string
  playerScores?: Record<string, number>
  playerStreaks?: Record<string, number>
  playerBadges?: Record<string, PlayerBadge>
  // Lifted popover state from PageWithNav
  showPopover?: boolean
  setShowPopover?: (show: boolean) => void
  activeTab?: 'add' | 'invite'
  setActiveTab?: (tab: 'add' | 'invite') => void
  // Game-specific roster warnings
  rosterWarning?: RosterWarning
  // Side assignments (for 2-player games)
  whitePlayerId?: string | null
  blackPlayerId?: string | null
  onAssignWhitePlayer?: (playerId: string | null) => void
  onAssignBlackPlayer?: (playerId: string | null) => void
  // Game phase (for showing spectating vs assign)
  gamePhase?: 'setup' | 'playing' | 'results'
  // Custom mode display (overrides player-count-based mode)
  customModeLabel?: string
  customModeEmoji?: string
  customModeColor?: string
}
 
export function GameContextNav({
  navTitle,
  navEmoji,
  gameName,
  gameMode,
  activePlayers,
  inactivePlayers,
  shouldEmphasize,
  showFullscreenSelection,
  onAddPlayer,
  onRemovePlayer,
  onConfigurePlayer,
  onExitSession,
  onSetup,
  onNewGame,
  networkPlayers = [],
  roomInfo,
  currentPlayerId,
  playerScores,
  playerStreaks,
  playerBadges,
  showPopover,
  setShowPopover,
  activeTab,
  setActiveTab,
  rosterWarning,
  whitePlayerId,
  blackPlayerId,
  onAssignWhitePlayer,
  onAssignBlackPlayer,
  gamePhase,
  customModeLabel,
  customModeEmoji,
  customModeColor,
}: GameContextNavProps) {
  // Get current user info for moderation
  const { data: currentUserId } = useUserId()
  const { roomData, refetch: refetchRoomData } = useRoomData()
 
  // Check if current user is the host
  const currentMember = roomData?.members.find((m) => m.userId === currentUserId)
  const isCurrentUserHost = currentMember?.isCreator ?? false
 
  // 2x2 grid layout for normal mode, column for fullscreen
  if (showFullscreenSelection) {
    return (
      <div
        style={{
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'center',
          gap: '16px',
          width: '100%',
        }}
      >
        <h1
          style={{
            fontSize: '32px',
            fontWeight: 'bold',
            background: 'linear-gradient(135deg, #60a5fa, #a78bfa, #f472b6)',
            backgroundClip: 'text',
            color: 'transparent',
            margin: 0,
            transition: 'font-size 0.4s cubic-bezier(0.4, 0, 0.2, 1)',
          }}
        >
          {navEmoji && `${navEmoji} `}
          {navTitle}
        </h1>

        <GameModeIndicator
          gameMode={gameMode}
          shouldEmphasize={shouldEmphasize}
          showFullscreenSelection={showFullscreenSelection}
          customModeLabel={customModeLabel}
          customModeEmoji={customModeEmoji}
          customModeColor={customModeColor}
        />

        <FullscreenPlayerSelection
          inactivePlayers={inactivePlayers}
          onSelectPlayer={onAddPlayer}
          onConfigurePlayer={onConfigurePlayer}
          isVisible={showFullscreenSelection}
        />

        <style
          dangerouslySetInnerHTML={{
            __html: `
          @keyframes fadeIn {
            from {
              opacity: 0;
              transform: translateY(-10px);
            }
            to {
              opacity: 1;
              transform: translateY(0);
            }
          }
          @keyframes expandIn {
            from {
              opacity: 0;
              transform: scaleY(0.8);
            }
            to {
              opacity: 1;
              transform: scaleY(1);
            }
          }
        `,
          }}
        />
      </div>
    )
  }
 
  // Completely flat layout: ALL components always mounted, shown/hidden via display
  // This ensures AddPlayerButton never unmounts, preserving its state
  const showPlayers = activePlayers.length > 0 || (shouldEmphasize && inactivePlayers.length > 0)
 
  return (
    <>
      {/* Pending Invitations Banner - Shows above nav when user has invitations */}
      <PendingInvitations
        currentRoomId={roomInfo?.roomId}
        onInvitationChange={() => refetchRoomData()}
      />
 
      {/* Roster Warning Banner - Game-specific warnings (e.g., too many players) */}
      {rosterWarning && (
        <div
          style={{
            width: '100%',
            padding: '12px 16px',
            background:
              'linear-gradient(135deg, rgba(251, 191, 36, 0.15), rgba(245, 158, 11, 0.1))',
            borderLeft: '4px solid #f59e0b',
            borderRadius: '8px',
            marginBottom: '12px',
            display: 'flex',
            flexDirection: 'column',
            gap: '8px',
          }}
        >
          <div>
            <h4
              style={{
                margin: 0,
                fontSize: '14px',
                fontWeight: 600,
                color: '#92400e',
              }}
            >
              {rosterWarning.heading}
            </h4>
            <p
              style={{
                margin: '4px 0 0 0',
                fontSize: '13px',
                color: '#78350f',
                lineHeight: '1.4',
              }}
            >
              {rosterWarning.description}
            </p>
          </div>
          {rosterWarning.actions && rosterWarning.actions.length > 0 && (
            <div style={{ display: 'flex', flexWrap: 'wrap', gap: '6px' }}>
              {rosterWarning.actions.map((action, index) => (
                <button
                  key={index}
                  onClick={action.onClick}
                  style={{
                    padding: '6px 12px',
                    fontSize: '12px',
                    fontWeight: 600,
                    borderRadius: '6px',
                    border: 'none',
                    cursor: 'pointer',
                    background: action.variant === 'danger' ? '#dc2626' : '#f59e0b',
                    color: 'white',
                    transition: 'all 0.2s ease',
                  }}
                  onMouseOver={(e) => {
                    e.currentTarget.style.background =
                      action.variant === 'danger' ? '#b91c1c' : '#d97706'
                  }}
                  onMouseOut={(e) => {
                    e.currentTarget.style.background =
                      action.variant === 'danger' ? '#dc2626' : '#f59e0b'
                  }}
                  onFocus={(e) => {
                    e.currentTarget.style.background =
                      action.variant === 'danger' ? '#b91c1c' : '#d97706'
                  }}
                  onBlur={(e) => {
                    e.currentTarget.style.background =
                      action.variant === 'danger' ? '#dc2626' : '#f59e0b'
                  }}
                  type="button"
                >
                  {action.label}
                </button>
              ))}
            </div>
          )}
        </div>
      )}
 
      <div
        style={{
          display: 'flex',
          gap: '20px',
          alignItems: 'center',
          width: 'auto',
          whiteSpace: 'nowrap',
          overflow: 'visible',
        }}
      >
        {/* Game Title Section - Always mounted, hidden when in room */}
        <div
          style={{
            display: roomInfo ? 'none' : 'flex',
            alignItems: 'center',
            gap: '12px',
            flex: 1,
          }}
        >
          <GameTitleMenu
            navTitle={navTitle}
            navEmoji={navEmoji}
            onSetup={onSetup}
            onNewGame={onNewGame}
            onQuit={onExitSession}
            showMenu={true}
          />
          <div style={{ marginLeft: 'auto' }}>
            <GameModeIndicator
              gameMode={gameMode}
              shouldEmphasize={shouldEmphasize}
              showFullscreenSelection={false}
              customModeLabel={customModeLabel}
              customModeEmoji={customModeEmoji}
              customModeColor={customModeColor}
            />
          </div>
        </div>
 
        {/* Room Info Section - Always mounted, hidden when not in room */}
        <div
          style={{
            display: roomInfo ? 'flex' : 'none',
            alignItems: 'flex-end',
            gap: '12px',
            padding: '6px 12px 12px 12px',
            background:
              'linear-gradient(135deg, rgba(255, 255, 255, 0.10), rgba(255, 255, 255, 0.05))',
            borderRadius: '12px',
            border: '2px solid rgba(255, 255, 255, 0.15)',
            boxShadow: '0 4px 12px rgba(0, 0, 0, 0.1)',
            transition: 'all 0.3s ease',
          }}
        >
          <RoomInfo
            roomId={roomInfo?.roomId}
            roomName={roomInfo?.roomName}
            gameName={roomInfo?.gameName ?? ''}
            playerCount={roomInfo?.playerCount ?? 0}
            joinCode={roomInfo?.joinCode}
            shouldEmphasize={shouldEmphasize}
            gameMode={gameMode}
            modeColor={
              customModeColor ??
              (gameMode === 'battle'
                ? '#8b5cf6'
                : gameMode === 'single'
                  ? '#3b82f6'
                  : gameMode === 'tournament'
                    ? '#f59e0b'
                    : '#6b7280')
            }
            modeEmoji={
              customModeEmoji ??
              (gameMode === 'battle'
                ? '⚔️'
                : gameMode === 'single'
                  ? '🎯'
                  : gameMode === 'tournament'
                    ? '🏆'
                    : '👥')
            }
            modeLabel={
              customModeLabel ??
              (gameMode === 'battle'
                ? 'Battle'
                : gameMode === 'single'
                  ? 'Solo'
                  : gameMode === 'tournament'
                    ? 'Tournament'
                    : 'Select Players')
            }
            navTitle={navTitle}
            navEmoji={navEmoji}
            onSetup={onSetup}
            onNewGame={onNewGame}
            onQuit={onExitSession}
          />
 
          {/* Network Players - inside same pane as room info */}
          {networkPlayers.length > 0 && (
            <>
              <div
                style={{
                  width: '1px',
                  height: '48px',
                  background: 'rgba(255, 255, 255, 0.2)',
                  margin: '0 4px',
                }}
              />
              {networkPlayers.map((player) => (
                <NetworkPlayerIndicator
                  key={player.id}
                  player={player}
                  shouldEmphasize={shouldEmphasize}
                  currentPlayerId={currentPlayerId}
                  playerScores={playerScores}
                  playerStreaks={playerStreaks}
                  playerBadges={playerBadges}
                  roomId={roomInfo?.roomId}
                  currentUserId={currentUserId ?? undefined}
                  isCurrentUserHost={isCurrentUserHost}
                  whitePlayerId={whitePlayerId}
                  blackPlayerId={blackPlayerId}
                  onAssignWhitePlayer={onAssignWhitePlayer}
                  onAssignBlackPlayer={onAssignBlackPlayer}
                  isInRoom={!!roomInfo}
                  gamePhase={gamePhase}
                />
              ))}
            </>
          )}
        </div>
 
        {/* Player Section - Always mounted, hidden when no players */}
        <div
          style={{
            display: showPlayers ? 'flex' : 'none',
            alignItems: 'flex-end',
            gap: shouldEmphasize ? '12px' : '8px',
            padding: shouldEmphasize ? '12px 20px 16px 20px' : '6px 12px 12px 12px',
            background: shouldEmphasize
              ? 'linear-gradient(135deg, rgba(255, 255, 255, 0.18), rgba(255, 255, 255, 0.10))'
              : 'linear-gradient(135deg, rgba(255, 255, 255, 0.10), rgba(255, 255, 255, 0.05))',
            borderRadius: shouldEmphasize ? '16px' : '12px',
            border: shouldEmphasize
              ? '3px solid rgba(255, 255, 255, 0.3)'
              : '2px solid rgba(255, 255, 255, 0.15)',
            boxShadow: shouldEmphasize
              ? '0 8px 24px rgba(0, 0, 0, 0.2), inset 0 1px 0 rgba(255,255,255,0.3)'
              : '0 4px 12px rgba(0, 0, 0, 0.1)',
            transition: 'all 0.4s cubic-bezier(0.4, 0, 0.2, 1)',
            transform: shouldEmphasize ? 'scale(1.05)' : 'scale(1)',
          }}
        >
          <ActivePlayersList
            activePlayers={activePlayers}
            shouldEmphasize={shouldEmphasize}
            onRemovePlayer={onRemovePlayer}
            onConfigurePlayer={onConfigurePlayer}
            currentPlayerId={currentPlayerId}
            playerScores={playerScores}
            playerStreaks={playerStreaks}
            playerBadges={playerBadges}
            whitePlayerId={whitePlayerId}
            blackPlayerId={blackPlayerId}
            onAssignWhitePlayer={onAssignWhitePlayer}
            onAssignBlackPlayer={onAssignBlackPlayer}
            isInRoom={!!roomInfo}
            isCurrentUserHost={isCurrentUserHost}
            gamePhase={gamePhase}
          />
 
          <AddPlayerButton
            inactivePlayers={inactivePlayers}
            shouldEmphasize={shouldEmphasize}
            onAddPlayer={onAddPlayer}
            showPopover={showPopover}
            setShowPopover={setShowPopover}
            activeTab={activeTab}
            setActiveTab={setActiveTab}
            isInRoom={!!roomInfo}
            gameName={gameName || 'matching'}
          />
        </div>
 
        <style
          dangerouslySetInnerHTML={{
            __html: `
        @keyframes fadeIn {
          from {
            opacity: 0;
            transform: translateY(-10px);
          }
          to {
            opacity: 1;
            transform: translateY(0);
          }
        }
        @keyframes expandIn {
          from {
            opacity: 0;
            transform: scaleY(0.8);
          }
          to {
            opacity: 1;
            transform: scaleY(1);
          }
        }
      `,
          }}
        />
      </div>
    </>
  )
}