more intuitive game loading (#13)
* faster game laoding without need for add button
This commit is contained in:
@@ -15,9 +15,11 @@ import { useEffect, useState } from "react";
|
|||||||
interface Props {
|
interface Props {
|
||||||
pgn: string;
|
pgn: string;
|
||||||
setPgn: (pgn: string) => void;
|
setPgn: (pgn: string) => void;
|
||||||
|
/** Called when a game is selected; if provided, bypasses manual 'Add' */
|
||||||
|
onSelect?: (pgn: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function ChessComInput({ pgn, setPgn }: Props) {
|
export default function ChessComInput({ pgn, setPgn, onSelect }: Props) {
|
||||||
const [requestCount, setRequestCount] = useState(0);
|
const [requestCount, setRequestCount] = useState(0);
|
||||||
const [chessComUsername, setChessComUsername] = useLocalStorage(
|
const [chessComUsername, setChessComUsername] = useLocalStorage(
|
||||||
"chesscom-username",
|
"chesscom-username",
|
||||||
@@ -68,7 +70,13 @@ export default function ChessComInput({ pgn, setPgn }: Props) {
|
|||||||
>
|
>
|
||||||
{games.map((game) => (
|
{games.map((game) => (
|
||||||
<ListItemButton
|
<ListItemButton
|
||||||
onClick={() => setPgn(game.pgn)}
|
onClick={() => {
|
||||||
|
if (onSelect) {
|
||||||
|
onSelect(game.pgn);
|
||||||
|
} else {
|
||||||
|
setPgn(game.pgn);
|
||||||
|
}
|
||||||
|
}}
|
||||||
selected={pgn === game.pgn}
|
selected={pgn === game.pgn}
|
||||||
style={{ width: 350, maxWidth: 350 }}
|
style={{ width: 350, maxWidth: 350 }}
|
||||||
key={game.uuid}
|
key={game.uuid}
|
||||||
|
|||||||
@@ -15,9 +15,11 @@ import { useEffect, useState } from "react";
|
|||||||
interface Props {
|
interface Props {
|
||||||
pgn: string;
|
pgn: string;
|
||||||
setPgn: (pgn: string) => void;
|
setPgn: (pgn: string) => void;
|
||||||
|
/** Called when a game is selected; if provided, bypasses manual 'Add' */
|
||||||
|
onSelect?: (pgn: string) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function LichessInput({ pgn, setPgn }: Props) {
|
export default function LichessInput({ pgn, setPgn, onSelect }: Props) {
|
||||||
const [requestCount, setRequestCount] = useState(0);
|
const [requestCount, setRequestCount] = useState(0);
|
||||||
const [lichessUsername, setLichessUsername] = useLocalStorage(
|
const [lichessUsername, setLichessUsername] = useLocalStorage(
|
||||||
"lichess-username",
|
"lichess-username",
|
||||||
@@ -68,7 +70,14 @@ export default function LichessInput({ pgn, setPgn }: Props) {
|
|||||||
>
|
>
|
||||||
{games.map((game) => (
|
{games.map((game) => (
|
||||||
<ListItemButton
|
<ListItemButton
|
||||||
onClick={() => setPgn(game.pgn)}
|
onClick={() => {
|
||||||
|
const selectedPgn: string = game.pgn;
|
||||||
|
if (onSelect) {
|
||||||
|
onSelect(selectedPgn);
|
||||||
|
} else {
|
||||||
|
setPgn(selectedPgn);
|
||||||
|
}
|
||||||
|
}}
|
||||||
selected={pgn === game.pgn}
|
selected={pgn === game.pgn}
|
||||||
style={{ width: 350, maxWidth: 350 }}
|
style={{ width: 350, maxWidth: 350 }}
|
||||||
key={game.id}
|
key={game.id}
|
||||||
|
|||||||
@@ -38,13 +38,14 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) {
|
|||||||
const [parsingError, setParsingError] = useState("");
|
const [parsingError, setParsingError] = useState("");
|
||||||
const { addGame } = useGameDatabase();
|
const { addGame } = useGameDatabase();
|
||||||
|
|
||||||
const handleAddGame = () => {
|
const handleAddGame = (overridePgn?: string) => {
|
||||||
if (!pgn) return;
|
const usedPgn = overridePgn ?? pgn;
|
||||||
|
if (!usedPgn) return;
|
||||||
setParsingError("");
|
setParsingError("");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const gameToAdd = getGameFromPgn(pgn);
|
const gameToAdd = getGameFromPgn(usedPgn);
|
||||||
setSentryContext("loadedGame", { pgn });
|
setSentryContext("loadedGame", { pgn: usedPgn });
|
||||||
|
|
||||||
if (setGame) {
|
if (setGame) {
|
||||||
setGame(gameToAdd);
|
setGame(gameToAdd);
|
||||||
@@ -105,11 +106,25 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
{gameOrigin === GameOrigin.ChessCom && (
|
{gameOrigin === GameOrigin.ChessCom && (
|
||||||
<ChessComInput pgn={pgn} setPgn={setPgn} />
|
<ChessComInput
|
||||||
|
pgn={pgn}
|
||||||
|
setPgn={setPgn}
|
||||||
|
onSelect={(selectedPgn: string) => {
|
||||||
|
setPgn(selectedPgn);
|
||||||
|
handleAddGame(selectedPgn);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{gameOrigin === GameOrigin.Lichess && (
|
{gameOrigin === GameOrigin.Lichess && (
|
||||||
<LichessInput pgn={pgn} setPgn={setPgn} />
|
<LichessInput
|
||||||
|
pgn={pgn}
|
||||||
|
setPgn={setPgn}
|
||||||
|
onSelect={(selectedPgn: string) => {
|
||||||
|
setPgn(selectedPgn);
|
||||||
|
handleAddGame(selectedPgn);
|
||||||
|
}}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{parsingError && (
|
{parsingError && (
|
||||||
@@ -122,16 +137,9 @@ export default function NewGameDialog({ open, onClose, setGame }: Props) {
|
|||||||
</Grid>
|
</Grid>
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
<DialogActions sx={{ m: 2 }}>
|
<DialogActions sx={{ m: 2 }}>
|
||||||
<Button
|
<Button variant="outlined" onClick={handleClose}>
|
||||||
variant="outlined"
|
|
||||||
sx={{ marginRight: 2 }}
|
|
||||||
onClick={handleClose}
|
|
||||||
>
|
|
||||||
Cancel
|
Cancel
|
||||||
</Button>
|
</Button>
|
||||||
<Button variant="contained" onClick={handleAddGame}>
|
|
||||||
Add
|
|
||||||
</Button>
|
|
||||||
</DialogActions>
|
</DialogActions>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user