From 0e5939675b3c7c39fa4ba31a25d2257f6a5bbd36 Mon Sep 17 00:00:00 2001 From: GuillaumeSD Date: Sat, 17 May 2025 04:57:47 +0200 Subject: [PATCH] fix : loading game error display --- src/sections/loadGame/chessComInput.tsx | 12 +++---- src/sections/loadGame/lichessInput.tsx | 12 +++---- src/sections/loadGame/loadGameDialog.tsx | 42 ++++++++++++++++-------- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/sections/loadGame/chessComInput.tsx b/src/sections/loadGame/chessComInput.tsx index 736c459..09234bc 100644 --- a/src/sections/loadGame/chessComInput.tsx +++ b/src/sections/loadGame/chessComInput.tsx @@ -9,13 +9,11 @@ import { ListItemText, TextField, } from "@mui/material"; -import { useSetAtom } from "jotai"; -import { boardOrientationAtom } from "../analysis/states"; import { useDebounce } from "@/hooks/useDebounce"; import { useQuery } from "@tanstack/react-query"; interface Props { - onSelect: (pgn: string) => void; + onSelect: (pgn: string, boardOrientation?: boolean) => void; } export default function ChessComInput({ onSelect }: Props) { @@ -24,7 +22,6 @@ export default function ChessComInput({ onSelect }: Props) { "" ); const debouncedUsername = useDebounce(chessComUsername, 300); - const setBoardOrientation = useSetAtom(boardOrientationAtom); const { data: games, @@ -72,11 +69,10 @@ export default function ChessComInput({ onSelect }: Props) { games.map((game) => ( { - setBoardOrientation( + const boardOrientation = chessComUsername.toLowerCase() !== - game.black?.username?.toLowerCase() - ); - onSelect(game.pgn); + game.black?.username?.toLowerCase(); + onSelect(game.pgn, boardOrientation); }} style={{ width: 350, maxWidth: 350 }} key={game.uuid} diff --git a/src/sections/loadGame/lichessInput.tsx b/src/sections/loadGame/lichessInput.tsx index 3a28262..8710915 100644 --- a/src/sections/loadGame/lichessInput.tsx +++ b/src/sections/loadGame/lichessInput.tsx @@ -9,13 +9,11 @@ import { ListItemText, TextField, } from "@mui/material"; -import { useSetAtom } from "jotai"; -import { boardOrientationAtom } from "../analysis/states"; import { useDebounce } from "@/hooks/useDebounce"; import { useQuery } from "@tanstack/react-query"; interface Props { - onSelect: (pgn: string) => void; + onSelect: (pgn: string, boardOrientation?: boolean) => void; } export default function LichessInput({ onSelect }: Props) { @@ -24,7 +22,6 @@ export default function LichessInput({ onSelect }: Props) { "" ); const debouncedUsername = useDebounce(lichessUsername, 500); - const setBoardOrientation = useSetAtom(boardOrientationAtom); const { data: games, @@ -72,11 +69,10 @@ export default function LichessInput({ onSelect }: Props) { games.map((game) => ( { - setBoardOrientation( + const boardOrientation = lichessUsername.toLowerCase() !== - game.players?.black?.user?.name?.toLowerCase() - ); - onSelect(game.pgn); + game.players?.black?.user?.name?.toLowerCase(); + onSelect(game.pgn, boardOrientation); }} style={{ width: 350, maxWidth: 350 }} key={game.id} diff --git a/src/sections/loadGame/loadGameDialog.tsx b/src/sections/loadGame/loadGameDialog.tsx index eaec68f..a1d3192 100644 --- a/src/sections/loadGame/loadGameDialog.tsx +++ b/src/sections/loadGame/loadGameDialog.tsx @@ -12,12 +12,13 @@ import { InputLabel, OutlinedInput, DialogActions, - Typography, Grid2 as Grid, + Snackbar, + Alert, } from "@mui/material"; import { setContext as setSentryContext } from "@sentry/react"; import { Chess } from "chess.js"; -import { useState } from "react"; +import { useRef, useState } from "react"; import GamePgnInput from "./gamePgnInput"; import ChessComInput from "./chessComInput"; import { useLocalStorage } from "@/hooks/useLocalStorage"; @@ -38,12 +39,12 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) { GameOrigin.ChessCom ); const [parsingError, setParsingError] = useState(""); + const parsingErrorTimeout = useRef(null); const setBoardOrientation = useSetAtom(boardOrientationAtom); const { addGame } = useGameDatabase(); - const handleAddGame = (pgn: string) => { + const handleAddGame = (pgn: string, boardOrientation?: boolean) => { if (!pgn) return; - setParsingError(""); try { const gameToAdd = getGameFromPgn(pgn); @@ -55,20 +56,33 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) { addGame(gameToAdd); } + setBoardOrientation(boardOrientation ?? true); handleClose(); } catch (error) { console.error(error); + + if (parsingErrorTimeout.current) { + clearTimeout(parsingErrorTimeout.current); + } + setParsingError( error instanceof Error ? `${error.message} !` - : "Unknown error while parsing PGN !" + : "Invalid PGN: unknown error !" ); + + parsingErrorTimeout.current = setTimeout(() => { + setParsingError(""); + }, 3000); } }; const handleClose = () => { setPgn(""); setParsingError(""); + if (parsingErrorTimeout.current) { + clearTimeout(parsingErrorTimeout.current); + } onClose(); }; @@ -129,13 +143,16 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) { )} - {parsingError && ( - - - {parsingError} - - - )} + + setParsingError("")} + severity="error" + variant="filled" + sx={{ width: "100%" }} + > + {parsingError} + + @@ -147,7 +164,6 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) { variant="contained" sx={{ marginLeft: 2 }} onClick={() => { - setBoardOrientation(true); handleAddGame(pgn); }} >