import { useGameDatabase } from "@/hooks/useGameDatabase"; import { getGameFromPgn } from "@/lib/chess"; import { GameOrigin } from "@/types/enums"; import { MenuItem, Select, Button, Dialog, DialogTitle, DialogContent, FormControl, InputLabel, OutlinedInput, DialogActions, Typography, Grid2 as Grid, } from "@mui/material"; import { Chess } from "chess.js"; import { useState } from "react"; import GamePgnInput from "./gamePgnInput"; import ChessComInput from "./chessComInput"; import { useLocalStorage } from "@/hooks/useLocalStorage"; import LichessInput from "./lichessInput"; interface Props { open: boolean; onClose: () => void; setGame?: (game: Chess) => void; } export default function NewGameDialog({ open, onClose, setGame }: Props) { const [pgn, setPgn] = useState(""); const [gameOrigin, setGameOrigin] = useLocalStorage( "preferred-game-origin", GameOrigin.Pgn ); const [parsingError, setParsingError] = useState(""); const { addGame } = useGameDatabase(); const handleAddGame = () => { if (!pgn) return; setParsingError(""); try { const gameToAdd = getGameFromPgn(pgn); if (setGame) { setGame(gameToAdd); } else { addGame(gameToAdd); } handleClose(); } catch (error) { console.error(error); setParsingError( error instanceof Error ? `${error.message} !` : "Unknown error while parsing PGN !" ); } }; const handleClose = () => { setPgn(""); setParsingError(""); onClose(); }; return ( {setGame ? "Load a game" : "Add a game to your database"} Game origin {gameOrigin === GameOrigin.Pgn && ( )} {gameOrigin === GameOrigin.ChessCom && ( )} {gameOrigin === GameOrigin.Lichess && ( )} {parsingError && ( {parsingError} )} ); } const gameOriginLabel: Record = { [GameOrigin.Pgn]: "PGN", [GameOrigin.ChessCom]: "Chess.com", [GameOrigin.Lichess]: "Lichess.org", };