From 2b79434d456c2ad31d5b7ad3a351d1cac7341c74 Mon Sep 17 00:00:00 2001 From: GuillaumeSD <47183782+GuillaumeSD@users.noreply.github.com> Date: Mon, 12 May 2025 14:11:24 +0200 Subject: [PATCH] fix : loading game from CC and lichess --- src/lib/chessCom.ts | 10 ++++++--- src/lib/lichess.ts | 9 +++++--- src/sections/loadGame/chessComInput.tsx | 30 +++++++++++++++---------- src/sections/loadGame/lichessInput.tsx | 20 +++++++++++------ 4 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/lib/chessCom.ts b/src/lib/chessCom.ts index a7176d9..08b1124 100644 --- a/src/lib/chessCom.ts +++ b/src/lib/chessCom.ts @@ -2,7 +2,8 @@ import { ChessComGame } from "@/types/chessCom"; import { getPaddedNumber } from "./helpers"; export const getChessComUserRecentGames = async ( - username: string + username: string, + signal?: AbortSignal ): Promise => { const date = new Date(); const year = date.getUTCFullYear(); @@ -10,10 +11,13 @@ export const getChessComUserRecentGames = async ( const paddedMonth = getPaddedNumber(month); const res = await fetch( - `https://api.chess.com/pub/player/${username}/games/${year}/${paddedMonth}` + `https://api.chess.com/pub/player/${username}/games/${year}/${paddedMonth}`, + { method: "GET", signal } ); - if (res.status === 404) return []; + if (res.status >= 400) { + throw new Error("Error fetching games from Chess.com"); + } const data = await res.json(); diff --git a/src/lib/lichess.ts b/src/lib/lichess.ts index 222dd0c..07a26d1 100644 --- a/src/lib/lichess.ts +++ b/src/lib/lichess.ts @@ -56,14 +56,17 @@ export const getLichessEval = async ( }; export const getLichessUserRecentGames = async ( - username: string + username: string, + signal?: AbortSignal ): Promise => { const res = await fetch( `https://lichess.org/api/games/user/${username}?until=${Date.now()}&max=50&pgnInJson=true&sort=dateDesc&clocks=true`, - { method: "GET", headers: { accept: "application/x-ndjson" } } + { method: "GET", headers: { accept: "application/x-ndjson" }, signal } ); - if (res.status === 404) return []; + if (res.status >= 400) { + throw new Error("Error fetching games from Lichess"); + } const rawData = await res.text(); const games: LichessGame[] = rawData diff --git a/src/sections/loadGame/chessComInput.tsx b/src/sections/loadGame/chessComInput.tsx index ef37e4f..736c459 100644 --- a/src/sections/loadGame/chessComInput.tsx +++ b/src/sections/loadGame/chessComInput.tsx @@ -23,7 +23,7 @@ export default function ChessComInput({ onSelect }: Props) { "chesscom-username", "" ); - const debouncedUsername = useDebounce(chessComUsername, 200); + const debouncedUsername = useDebounce(chessComUsername, 300); const setBoardOrientation = useSetAtom(boardOrientationAtom); const { @@ -33,7 +33,9 @@ export default function ChessComInput({ onSelect }: Props) { } = useQuery({ queryKey: ["CCUserGames", debouncedUsername], enabled: !!debouncedUsername, - queryFn: () => getChessComUserRecentGames(debouncedUsername ?? ""), + queryFn: ({ signal }) => + getChessComUserRecentGames(debouncedUsername ?? "", signal), + retry: 1, }); return ( @@ -72,7 +74,7 @@ export default function ChessComInput({ onSelect }: Props) { onClick={() => { setBoardOrientation( chessComUsername.toLowerCase() !== - game.black.username.toLowerCase() + game.black?.username?.toLowerCase() ); onSelect(game.pgn); }} @@ -80,16 +82,20 @@ export default function ChessComInput({ onSelect }: Props) { key={game.uuid} > getLichessUserRecentGames(debouncedUsername ?? ""), + queryFn: ({ signal }) => + getLichessUserRecentGames(debouncedUsername ?? "", signal), + retry: 1, }); return ( @@ -72,7 +74,7 @@ export default function LichessInput({ onSelect }: Props) { onClick={() => { setBoardOrientation( lichessUsername.toLowerCase() !== - game.players?.black?.user?.name.toLowerCase() + game.players?.black?.user?.name?.toLowerCase() ); onSelect(game.pgn); }} @@ -87,11 +89,15 @@ export default function LichessInput({ onSelect }: Props) { capitalize(game.players?.black?.user?.name || "black") || "Black" } (${game.players?.black?.rating || "?"})`} - secondary={`${capitalize(game.speed)} played at ${new Date( + secondary={ game.lastMoveAt - ) - .toLocaleString() - .slice(0, -3)}`} + ? `${capitalize(game.speed || "game")} played at ${new Date( + game.lastMoveAt + ) + .toLocaleString() + .slice(0, -3)}` + : undefined + } slotProps={{ primary: { noWrap: true }, secondary: { noWrap: true },