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 | 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 9x 9x 9x 6x 6x 6x 6x 1x 1x 4x 4x 4x 4x 6x 6x 9x 9x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 3x 3x 1x 1x 2x 2x 2x 3x 6x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 3x 3x 1x 1x 2x 2x 2x 3x 6x 6x 6x 6x 6x | 'use client'
import { useQuery } from '@tanstack/react-query'
import type {
GetAllPlayerStatsResponse,
GetPlayerStatsResponse,
PlayerStatsData,
} from '@/lib/arcade/stats/types'
import { api } from '@/lib/queryClient'
/**
* Hook to fetch stats for a specific player or all user's players
*
* Usage:
* ```tsx
* // Fetch all players' stats
* const { data, isLoading } = usePlayerStats()
* // data is PlayerStatsData[]
*
* // Fetch specific player's stats
* const { data, isLoading } = usePlayerStats('player-id')
* // data is PlayerStatsData
* ```
*/
export function usePlayerStats(playerId?: string) {
return useQuery<PlayerStatsData | PlayerStatsData[]>({
queryKey: playerId ? ['player-stats', playerId] : ['player-stats'],
queryFn: async () => {
const url = playerId ? `player-stats/${playerId}` : 'player-stats'
const res = await api(url)
if (!res.ok) {
throw new Error('Failed to fetch player stats')
}
const data: GetPlayerStatsResponse | GetAllPlayerStatsResponse = await res.json()
// Return single player stats or array of all stats
return 'stats' in data ? data.stats : data.playerStats
},
})
}
/**
* Hook to fetch stats for all user's players (typed as array)
*
* Convenience wrapper around usePlayerStats() with better typing.
*/
export function useAllPlayerStats() {
const query = useQuery<PlayerStatsData[]>({
queryKey: ['player-stats'],
queryFn: async () => {
const res = await api('player-stats')
if (!res.ok) {
throw new Error('Failed to fetch player stats')
}
const data: GetAllPlayerStatsResponse = await res.json()
return data.playerStats
},
})
return query
}
/**
* Hook to fetch stats for a specific player (typed as single object)
*
* Convenience wrapper around usePlayerStats() with better typing.
*/
export function useSinglePlayerStats(playerId: string) {
const query = useQuery<PlayerStatsData>({
queryKey: ['player-stats', playerId],
queryFn: async () => {
const res = await api(`player-stats/${playerId}`)
if (!res.ok) {
throw new Error('Failed to fetch player stats')
}
const data: GetPlayerStatsResponse = await res.json()
return data.stats
},
enabled: !!playerId, // Only run if playerId is provided
})
return query
}
|