fix : loading game error display

This commit is contained in:
GuillaumeSD
2025-05-17 04:57:47 +02:00
parent e7e4b737e9
commit 0e5939675b
3 changed files with 37 additions and 29 deletions

View File

@@ -9,13 +9,11 @@ import {
ListItemText, ListItemText,
TextField, TextField,
} from "@mui/material"; } from "@mui/material";
import { useSetAtom } from "jotai";
import { boardOrientationAtom } from "../analysis/states";
import { useDebounce } from "@/hooks/useDebounce"; import { useDebounce } from "@/hooks/useDebounce";
import { useQuery } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query";
interface Props { interface Props {
onSelect: (pgn: string) => void; onSelect: (pgn: string, boardOrientation?: boolean) => void;
} }
export default function ChessComInput({ onSelect }: Props) { export default function ChessComInput({ onSelect }: Props) {
@@ -24,7 +22,6 @@ export default function ChessComInput({ onSelect }: Props) {
"" ""
); );
const debouncedUsername = useDebounce(chessComUsername, 300); const debouncedUsername = useDebounce(chessComUsername, 300);
const setBoardOrientation = useSetAtom(boardOrientationAtom);
const { const {
data: games, data: games,
@@ -72,11 +69,10 @@ export default function ChessComInput({ onSelect }: Props) {
games.map((game) => ( games.map((game) => (
<ListItemButton <ListItemButton
onClick={() => { onClick={() => {
setBoardOrientation( const boardOrientation =
chessComUsername.toLowerCase() !== chessComUsername.toLowerCase() !==
game.black?.username?.toLowerCase() game.black?.username?.toLowerCase();
); onSelect(game.pgn, boardOrientation);
onSelect(game.pgn);
}} }}
style={{ width: 350, maxWidth: 350 }} style={{ width: 350, maxWidth: 350 }}
key={game.uuid} key={game.uuid}

View File

@@ -9,13 +9,11 @@ import {
ListItemText, ListItemText,
TextField, TextField,
} from "@mui/material"; } from "@mui/material";
import { useSetAtom } from "jotai";
import { boardOrientationAtom } from "../analysis/states";
import { useDebounce } from "@/hooks/useDebounce"; import { useDebounce } from "@/hooks/useDebounce";
import { useQuery } from "@tanstack/react-query"; import { useQuery } from "@tanstack/react-query";
interface Props { interface Props {
onSelect: (pgn: string) => void; onSelect: (pgn: string, boardOrientation?: boolean) => void;
} }
export default function LichessInput({ onSelect }: Props) { export default function LichessInput({ onSelect }: Props) {
@@ -24,7 +22,6 @@ export default function LichessInput({ onSelect }: Props) {
"" ""
); );
const debouncedUsername = useDebounce(lichessUsername, 500); const debouncedUsername = useDebounce(lichessUsername, 500);
const setBoardOrientation = useSetAtom(boardOrientationAtom);
const { const {
data: games, data: games,
@@ -72,11 +69,10 @@ export default function LichessInput({ onSelect }: Props) {
games.map((game) => ( games.map((game) => (
<ListItemButton <ListItemButton
onClick={() => { onClick={() => {
setBoardOrientation( const boardOrientation =
lichessUsername.toLowerCase() !== lichessUsername.toLowerCase() !==
game.players?.black?.user?.name?.toLowerCase() game.players?.black?.user?.name?.toLowerCase();
); onSelect(game.pgn, boardOrientation);
onSelect(game.pgn);
}} }}
style={{ width: 350, maxWidth: 350 }} style={{ width: 350, maxWidth: 350 }}
key={game.id} key={game.id}

View File

@@ -12,12 +12,13 @@ import {
InputLabel, InputLabel,
OutlinedInput, OutlinedInput,
DialogActions, DialogActions,
Typography,
Grid2 as Grid, Grid2 as Grid,
Snackbar,
Alert,
} from "@mui/material"; } from "@mui/material";
import { setContext as setSentryContext } from "@sentry/react"; import { setContext as setSentryContext } from "@sentry/react";
import { Chess } from "chess.js"; import { Chess } from "chess.js";
import { useState } from "react"; import { useRef, useState } from "react";
import GamePgnInput from "./gamePgnInput"; import GamePgnInput from "./gamePgnInput";
import ChessComInput from "./chessComInput"; import ChessComInput from "./chessComInput";
import { useLocalStorage } from "@/hooks/useLocalStorage"; import { useLocalStorage } from "@/hooks/useLocalStorage";
@@ -38,12 +39,12 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) {
GameOrigin.ChessCom GameOrigin.ChessCom
); );
const [parsingError, setParsingError] = useState(""); const [parsingError, setParsingError] = useState("");
const parsingErrorTimeout = useRef<NodeJS.Timeout | null>(null);
const setBoardOrientation = useSetAtom(boardOrientationAtom); const setBoardOrientation = useSetAtom(boardOrientationAtom);
const { addGame } = useGameDatabase(); const { addGame } = useGameDatabase();
const handleAddGame = (pgn: string) => { const handleAddGame = (pgn: string, boardOrientation?: boolean) => {
if (!pgn) return; if (!pgn) return;
setParsingError("");
try { try {
const gameToAdd = getGameFromPgn(pgn); const gameToAdd = getGameFromPgn(pgn);
@@ -55,20 +56,33 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) {
addGame(gameToAdd); addGame(gameToAdd);
} }
setBoardOrientation(boardOrientation ?? true);
handleClose(); handleClose();
} catch (error) { } catch (error) {
console.error(error); console.error(error);
if (parsingErrorTimeout.current) {
clearTimeout(parsingErrorTimeout.current);
}
setParsingError( setParsingError(
error instanceof Error error instanceof Error
? `${error.message} !` ? `${error.message} !`
: "Unknown error while parsing PGN !" : "Invalid PGN: unknown error !"
); );
parsingErrorTimeout.current = setTimeout(() => {
setParsingError("");
}, 3000);
} }
}; };
const handleClose = () => { const handleClose = () => {
setPgn(""); setPgn("");
setParsingError(""); setParsingError("");
if (parsingErrorTimeout.current) {
clearTimeout(parsingErrorTimeout.current);
}
onClose(); onClose();
}; };
@@ -129,13 +143,16 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) {
<LichessInput onSelect={handleAddGame} /> <LichessInput onSelect={handleAddGame} />
)} )}
{parsingError && ( <Snackbar open={!!parsingError}>
<FormControl fullWidth> <Alert
<Typography color="salmon" textAlign="center" marginTop={1}> onClose={() => setParsingError("")}
severity="error"
variant="filled"
sx={{ width: "100%" }}
>
{parsingError} {parsingError}
</Typography> </Alert>
</FormControl> </Snackbar>
)}
</Grid> </Grid>
</DialogContent> </DialogContent>
<DialogActions sx={{ m: 2 }}> <DialogActions sx={{ m: 2 }}>
@@ -147,7 +164,6 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) {
variant="contained" variant="contained"
sx={{ marginLeft: 2 }} sx={{ marginLeft: 2 }}
onClick={() => { onClick={() => {
setBoardOrientation(true);
handleAddGame(pgn); handleAddGame(pgn);
}} }}
> >