# All files / src/hooks useGameStats.ts

91.17% Statements 31/34
66.66% Branches 10/15
100% Functions 1/1
91.17% Lines 31/34

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
    981x
    1x

    1x
    11x
    11x
    11x

    11x
    7x
    1x
    1x
    1x

    6x
    6x
    7x
    3x
    3x
    7x
    1x
    1x
    7x
    1x
    7x
    5x
    5x
    11x

    11x
    6x

    6x

    11x

    11x
    11x
     import { useState, useEffect, useCallback } from 'react';
    import api from '../services/api';

    interface Team {
      id: number;
      name: string;
      abbreviation: string | null;
      logoUrl: string | null;
    }

    interface GameTeamStats {
      id: string;
      teamId: number;
      isHome: boolean;
      quarterScores: number[] | null;
      stats: Record<string, any>;
      team: Team;
      // Season average properties (optional, used when displaying historical data)
      totalGames?: number;
      homeGames?: number;
      awayGames?: number;
      avgStats?: Record<string, any>;
    }

    interface Player {
      id: number;
      firstName: string;
      lastName: string;
      position: string | null;
      number: number | null;
    }

    interface PlayerGameStats {
      id: string;
      playerId: number;
      stats: Record<string, any>;
      started: boolean;
      minutesPlayed: string | null;
      player: Player;
      team: {
        id: number;
        name: string;
        abbreviation: string | null;
        logoUrl: string | null;
      };
    }

    interface GameStats {
      teamStats: GameTeamStats[];
      playerStats: PlayerGameStats[];
      seasonAverages?: GameTeamStats[];
    }

    interface UseGameStatsReturn {
      data: GameStats | null;
      loading: boolean;
      error: string | null;
      refetch: () => Promise<void>;
    }

    export function useGameStats(gameId: string, pollInterval?: number): UseGameStatsReturn {
      const [data, setData] = useState<GameStats | null>(null);
      const [loading, setLoading] = useState(true);
      const [error, setError] = useState<string | null>(null);

      const fetchStats = useCallback(async () => {
        if (!gameId) {
          setLoading(false);
          return;
        }

        try {
          const response = await api.get(`/stats/game/${gameId}`);
          if (response.data.success) {
            setData(response.data.data);
            setError(null);
          } else {
            setError(response.data.error || 'Failed to fetch game stats');
          }
        } catch (err: any) {
          setError(err.response?.data?.error || err.message || 'Failed to fetch game stats');
        } finally {
          setLoading(false);
        }
      }, [gameId]);

      useEffect(() => {
        fetchStats();

        if (pollInterval && pollInterval > 0) {
          const interval = setInterval(fetchStats, pollInterval);
          return () => clearInterval(interval);
        }
      }, [fetchStats, pollInterval]);

      return { data, loading, error, refetch: fetchStats };
    }