-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(game-details): tanstack query instead of custom hook for data re…
…trieval
- Loading branch information
1 parent
34b4b62
commit e00855b
Showing
6 changed files
with
165 additions
and
84 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,17 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
|
||
interface GameData { | ||
commonTitle: string; | ||
cover: string | null; | ||
description: string; | ||
developer: string; | ||
discs: number; | ||
genre: string; | ||
id: string | string[]; | ||
languages: string[]; | ||
officialTitle: string; | ||
publisher: string; | ||
region: string; | ||
releaseDate: string; | ||
title: string; | ||
} | ||
import { fetchGameData, gameDataKeys } from "@/lib/query"; | ||
|
||
export function useGameData(platform: string, region: string, gameId: string) { | ||
const [gameData, setGameData] = useState<GameData | null>(null); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [error, setError] = useState<string | null>(null); | ||
|
||
const fetchGameData = useCallback(async () => { | ||
if (!platform || !region || !gameId) { | ||
setGameData(null); | ||
setError(null); | ||
return; | ||
} | ||
|
||
setIsLoading(true); | ||
setError(null); | ||
|
||
try { | ||
const apiRegion = mapRegionToApi(region); | ||
const response = await fetch( | ||
`https://psxdata.barrenechea.cl/${apiRegion}/${gameId}.json` | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed to fetch game data"); | ||
} | ||
|
||
const data = (await response.json()) as GameData; | ||
setGameData({ | ||
...data, | ||
cover: data.cover | ||
? `https://psxdata.barrenechea.cl/${apiRegion}/covers/${gameId}.${data.cover.split(".").pop()}` | ||
: null, | ||
}); | ||
} catch (err) { | ||
setError((err as Error).message); | ||
setGameData(null); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}, [platform, region, gameId]); | ||
|
||
useEffect(() => { | ||
void fetchGameData(); | ||
}, [fetchGameData]); | ||
|
||
return { gameData, isLoading, error }; | ||
} | ||
|
||
function mapRegionToApi(region: string): string { | ||
switch (region.toLowerCase()) { | ||
case "america": | ||
case "usa": | ||
case "ntsc-u": | ||
return "America"; | ||
case "europe": | ||
case "pal": | ||
case "ntsc-pal": | ||
return "Europe"; | ||
case "japan": | ||
case "ntsc-j": | ||
return "Japan"; | ||
default: | ||
return "America"; // Default to America if unknown | ||
} | ||
const query = useQuery({ | ||
queryKey: gameDataKeys.details(region, gameId), | ||
queryFn: () => fetchGameData(region, gameId), | ||
enabled: Boolean(platform && region && gameId), | ||
}); | ||
|
||
return { | ||
gameData: query.data ?? null, | ||
isLoading: query.isLoading, | ||
error: query.error ? query.error.message : null, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { QueryClient } from "@tanstack/react-query"; | ||
|
||
export const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
staleTime: 1000 * 60 * 5, // Data is fresh for 5 minutes | ||
gcTime: 1000 * 60 * 60, // Unused data is garbage collected after 1 hour | ||
retry: 1, | ||
}, | ||
}, | ||
}); | ||
|
||
export const gameDataKeys = { | ||
all: ["game-data"] as const, | ||
details: (region: string, gameId: string) => | ||
[...gameDataKeys.all, "details", region, gameId] as const, | ||
}; | ||
|
||
export interface GameData { | ||
commonTitle: string; | ||
cover: string | null; | ||
description: string; | ||
developer: string; | ||
discs: number; | ||
genre: string; | ||
id: string | string[]; | ||
languages: string[]; | ||
officialTitle: string; | ||
publisher: string; | ||
region: string; | ||
releaseDate: string; | ||
title: string; | ||
} | ||
|
||
function mapRegionToApi(region: string): string { | ||
switch (region.toLowerCase()) { | ||
case "america": | ||
case "usa": | ||
case "ntsc-u": | ||
return "America"; | ||
case "europe": | ||
case "pal": | ||
case "ntsc-pal": | ||
return "Europe"; | ||
case "japan": | ||
case "ntsc-j": | ||
return "Japan"; | ||
default: | ||
return "America"; | ||
} | ||
} | ||
|
||
export async function fetchGameData( | ||
region: string, | ||
gameId: string | ||
): Promise<GameData> { | ||
const apiRegion = mapRegionToApi(region); | ||
const response = await fetch( | ||
`https://psxdata.barrenechea.cl/${apiRegion}/${gameId}.json` | ||
); | ||
|
||
if (!response.ok) { | ||
throw new Error("Failed to fetch game data"); | ||
} | ||
|
||
const data = (await response.json()) as GameData; | ||
return { | ||
...data, | ||
cover: data.cover | ||
? `https://psxdata.barrenechea.cl/${apiRegion}/covers/${gameId}.${data.cover.split(".").pop()}` | ||
: null, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters