# All files / src/pages GameDetail.tsx

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

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

     import React, { useState, useEffect } from 'react';
    import { useParams, useNavigate, Link } from 'react-router-dom';
    import GameStatsPanel from '../components/stats/GameStatsPanel';
    import { formatTime } from '../utils/format';

    interface Game {
      id: string;
      sportKey: string;
      sportName: string;
      awayTeamId: string;
      homeTeamId: string;
      awayTeamName: string;
      homeTeamName: string;
      commenceTime: string;
      status: string;
      completed: boolean;
      venue?: string;
      homeScore?: number;
      awayScore?: number;
    }

    export default function GameDetail() {
      const { gameId } = useParams<{ gameId: string }>();
      const navigate = useNavigate();
      const [game, setGame] = useState<Game | null>(null);
      const [loading, setLoading] = useState(true);
      const [error, setError] = useState<string | null>(null);

      useEffect(() => {
        async function fetchGame() {
          if (!gameId) return;

          try {
            const response = await fetch(`/api/games/${gameId}`);
            if (response.ok) {
              const result = await response.json();
              const gameData = result.data || result;

              // Transform the game data to flatten sport fields
              const transformedGame: Game = {
                id: gameData.id,
                sportKey: gameData.sport?.key || '',
                sportName: gameData.sport?.name || '',
                awayTeamId: gameData.awayTeamId,
                homeTeamId: gameData.homeTeamId,
                awayTeamName: gameData.awayTeamName,
                homeTeamName: gameData.homeTeamName,
                commenceTime: gameData.commenceTime,
                status: gameData.status,
                completed: gameData.status === 'final',
                venue: gameData.venue,
                homeScore: gameData.homeScore,
                awayScore: gameData.awayScore
              };

              setGame(transformedGame);
            } else {
              setError('Game not found');
            }
          } catch (error) {
            console.error('Error fetching game:', error);
            setError('Failed to load game details');
          } finally {
            setLoading(false);
          }
        }

        fetchGame();
      }, [gameId]);

      if (!gameId) {
        return (
          <div className="container mx-auto px-4 py-8">
            <div className="bg-red-900/20 border border-red-500 rounded-lg p-6">
              <h2 className="text-red-400 font-bold text-xl mb-2">Invalid Game</h2>
              <p className="text-red-300">Game ID not provided</p>
            </div>
          </div>
        );
      }

      const isLive = game?.status === 'in_progress';
      const isCompleted = game?.status === 'final';
      const isScheduled = game?.status === 'scheduled';

      return (
        <div className="container mx-auto px-4 py-8">
          {/* Header */}
          <div className="flex items-center justify-between mb-6">
            <button
              onClick={() => navigate(-1)}
              className="flex items-center gap-2 text-blue-400 hover:text-blue-300 transition-colors"
            >
              <svg
                className="w-5 h-5"
                fill="none"
                stroke="currentColor"
                viewBox="0 0 24 24"
              >
                <path
                  strokeLinecap="round"
                  strokeLinejoin="round"
                  strokeWidth={2}
                  d="M15 19l-7-7 7-7"
                />
              </svg>
              Back
            </button>

            {isLive && (
              <div className="flex items-center gap-2 px-4 py-2 bg-red-600 rounded-full">
                <div className="w-2 h-2 bg-white rounded-full animate-pulse" />
                <span className="text-white font-bold text-sm">LIVE</span>
              </div>
            )}

            {isCompleted && (
              <div className="px-4 py-2 bg-gray-700 rounded-full">
                <span className="text-gray-300 font-bold text-sm">FINAL</span>
              </div>
            )}
          </div>

          {/* Loading State */}
          {loading && (
            <div className="flex justify-center items-center py-12">
              <div className="text-center">
                <div className="inline-block animate-spin mb-4">
                  <div className="w-8 h-8 border-4 border-blue-400 border-t-transparent rounded-full" />
                </div>
                <p className="text-gray-400">Loading game details...</p>
              </div>
            </div>
          )}

          {/* Error State */}
          {!loading && error && (
            <div className="bg-red-900/20 border border-red-500 rounded-lg p-6 mb-6">
              <h2 className="text-red-400 font-bold text-xl mb-2">Error</h2>
              <p className="text-red-300 mb-4">{error}</p>
              <button
                onClick={() => navigate(-1)}
                className="px-4 py-2 bg-red-600 hover:bg-red-700 text-white rounded transition-colors"
              >
                Go Back
              </button>
            </div>
          )}

          {/* Game Matchup Card */}
          {!loading && game && (
            <div className="bg-gray-800 rounded-lg p-6 mb-6">
              <div className="grid grid-cols-3 gap-8 items-center">
                {/* Away Team */}
                <Link
                  to={`/teams/${encodeURIComponent(game.sportKey)}/${encodeURIComponent(game.awayTeamName)}`}
                  className="text-center hover:opacity-80 transition-opacity"
                >
                  <div className="text-4xl mb-2">🏀</div>
                  <h2 className="text-2xl font-bold text-white mb-2">{game.awayTeamName}</h2>
                  <p className="text-gray-400 text-sm">Away</p>
                  {(isLive || isCompleted) && game.awayScore !== undefined && (
                    <p className="text-5xl font-bold text-blue-400 mt-4">{game.awayScore}</p>
                  )}
                </Link>

                {/* Game Info */}
                <div className="text-center">
                  {isScheduled && (
                    <>
                      <div className="text-gray-400 text-sm mb-2">Game Time</div>
                      <div className="text-white font-bold text-lg">{formatTime(game.commenceTime)}</div>
                      <div className="text-gray-500 text-sm mt-2">
                        {new Date(game.commenceTime).toLocaleDateString('en-US', {
                          weekday: 'long',
                          month: 'long',
                          day: 'numeric'
                        })}
                      </div>
                    </>
                  )}
                  {(isLive || isCompleted) && (
                    <div className="text-6xl font-bold text-gray-600">VS</div>
                  )}
                  {game.venue && (
                    <div className="text-gray-500 text-sm mt-4">📍 {game.venue}</div>
                  )}
                </div>

                {/* Home Team */}
                <Link
                  to={`/teams/${encodeURIComponent(game.sportKey)}/${encodeURIComponent(game.homeTeamName)}`}
                  className="text-center hover:opacity-80 transition-opacity"
                >
                  <div className="text-4xl mb-2">🏀</div>
                  <h2 className="text-2xl font-bold text-white mb-2">{game.homeTeamName}</h2>
                  <p className="text-gray-400 text-sm">Home</p>
                  {(isLive || isCompleted) && game.homeScore !== undefined && (
                    <p className="text-5xl font-bold text-red-400 mt-4">{game.homeScore}</p>
                  )}
                </Link>
              </div>
            </div>
          )}

          {/* Game Stats Panel */}
          <GameStatsPanel gameId={gameId} isLive={isLive} />
        </div>
      );
    }