fix : PR#57 game loading from URL

This commit is contained in:
GuillaumeSD
2025-07-04 17:21:46 +02:00
parent 7cbad399ed
commit aece5db7ce
3 changed files with 39 additions and 55 deletions

View File

@@ -26,9 +26,3 @@ export const decodeBase64 = (encoded: string | null): string | null => {
return null; return null;
} }
}; };
export const decodeBase64Param = (param: string): string | null => {
const params = new URLSearchParams(location.search);
const encodedParam = params.get(param);
return decodeBase64(encodedParam);
};

View File

@@ -1,15 +1,9 @@
import { useChessActions } from "@/hooks/useChessActions";
import Board from "@/sections/analysis/board"; import Board from "@/sections/analysis/board";
import PanelHeader from "@/sections/analysis/panelHeader"; import PanelHeader from "@/sections/analysis/panelHeader";
import PanelToolBar from "@/sections/analysis/panelToolbar"; import PanelToolBar from "@/sections/analysis/panelToolbar";
import AnalysisTab from "@/sections/analysis/panelBody/analysisTab"; import AnalysisTab from "@/sections/analysis/panelBody/analysisTab";
import ClassificationTab from "@/sections/analysis/panelBody/classificationTab"; import ClassificationTab from "@/sections/analysis/panelBody/classificationTab";
import { import { boardAtom, gameAtom, gameEvalAtom } from "@/sections/analysis/states";
boardAtom,
boardOrientationAtom,
gameAtom,
gameEvalAtom,
} from "@/sections/analysis/states";
import { import {
Box, Box,
Divider, Divider,
@@ -19,51 +13,21 @@ import {
useMediaQuery, useMediaQuery,
useTheme, useTheme,
} from "@mui/material"; } from "@mui/material";
import { useAtom, useAtomValue, useSetAtom } from "jotai"; import { useAtomValue } from "jotai";
import { useRouter } from "next/router";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { Icon } from "@iconify/react"; import { Icon } from "@iconify/react";
import EngineSettingsButton from "@/sections/engineSettings/engineSettingsButton"; import EngineSettingsButton from "@/sections/engineSettings/engineSettingsButton";
import GraphTab from "@/sections/analysis/panelBody/graphTab"; import GraphTab from "@/sections/analysis/panelBody/graphTab";
import { PageTitle } from "@/components/pageTitle"; import { PageTitle } from "@/components/pageTitle";
import { decodeBase64Param } from "@/lib/helpers";
export default function GameAnalysis() { export default function GameAnalysis() {
const theme = useTheme(); const theme = useTheme();
const [tab, setTab] = useState(0); const [tab, setTab] = useState(0);
const isLgOrGreater = useMediaQuery(theme.breakpoints.up("lg")); const isLgOrGreater = useMediaQuery(theme.breakpoints.up("lg"));
const { reset: resetBoard } = useChessActions(boardAtom); const gameEval = useAtomValue(gameEvalAtom);
const { reset: resetGame } = useChessActions(gameAtom);
const [gameEval, setGameEval] = useAtom(gameEvalAtom);
const game = useAtomValue(gameAtom); const game = useAtomValue(gameAtom);
const board = useAtomValue(boardAtom); const board = useAtomValue(boardAtom);
const setBoardOrientation = useSetAtom(boardOrientationAtom);
const { setPgn: setGamePgn } = useChessActions(gameAtom);
const router = useRouter();
const { gameId } = router.query;
const pgnParam = decodeBase64Param("pgn");
useEffect(() => {
if (pgnParam) {
setGameEval(undefined);
setGamePgn(pgnParam);
} else if (!gameId) {
resetBoard();
setGameEval(undefined);
setBoardOrientation(true);
resetGame({ noHeaders: true });
}
}, [
gameId,
pgnParam,
setGameEval,
setGamePgn,
setBoardOrientation,
resetBoard,
resetGame,
]);
const showMovesTab = game.history().length > 0 || board.history().length > 0; const showMovesTab = game.history().length > 0 || board.history().length > 0;

View File

@@ -12,6 +12,8 @@ import { useGameDatabase } from "@/hooks/useGameDatabase";
import { useAtomValue, useSetAtom } from "jotai"; import { useAtomValue, useSetAtom } from "jotai";
import { Chess } from "chess.js"; import { Chess } from "chess.js";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { decodeBase64 } from "@/lib/helpers";
import { Game } from "@/types/game";
export default function LoadGame() { export default function LoadGame() {
const router = useRouter(); const router = useRouter();
@@ -25,32 +27,56 @@ export default function LoadGame() {
const resetAndSetGamePgn = useCallback( const resetAndSetGamePgn = useCallback(
(pgn: string) => { (pgn: string) => {
resetBoard(pgn); resetBoard();
setEval(undefined); setEval(undefined);
setGamePgn(pgn); setGamePgn(pgn);
}, },
[resetBoard, setGamePgn, setEval] [resetBoard, setGamePgn, setEval]
); );
useEffect(() => { const { pgn: pgnParam, orientation: orientationParam } = router.query;
const loadGame = async () => {
if (!gameFromUrl) return;
useEffect(() => {
const loadGameFromIdParam = (gameUrl: Game) => {
const gamefromDbChess = new Chess(); const gamefromDbChess = new Chess();
gamefromDbChess.loadPgn(gameFromUrl.pgn); gamefromDbChess.loadPgn(gameUrl.pgn);
if (game.history().join() === gamefromDbChess.history().join()) return; if (game.history().join() === gamefromDbChess.history().join()) return;
resetAndSetGamePgn(gameFromUrl.pgn); resetAndSetGamePgn(gameUrl.pgn);
setEval(gameFromUrl.eval); setEval(gameUrl.eval);
setBoardOrientation( setBoardOrientation(
gameFromUrl.black.name === "You" && gameFromUrl.site === "Chesskit.org" gameUrl.black.name === "You" && gameUrl.site === "Chesskit.org"
? false ? false
: true : true
); );
}; };
loadGame(); const loadGameFromPgnParam = (encodedPgn: string) => {
}, [gameFromUrl, game, resetAndSetGamePgn, setEval, setBoardOrientation]); const decodedPgn = decodeBase64(encodedPgn);
if (!decodedPgn) return;
const gameFromPgnParam = new Chess();
gameFromPgnParam.loadPgn(decodedPgn || "");
if (game.history().join() === gameFromPgnParam.history().join()) return;
resetAndSetGamePgn(decodedPgn);
setBoardOrientation(orientationParam !== "black");
};
if (gameFromUrl) {
loadGameFromIdParam(gameFromUrl);
} else if (typeof pgnParam === "string") {
loadGameFromPgnParam(pgnParam);
}
}, [
gameFromUrl,
pgnParam,
orientationParam,
game,
resetAndSetGamePgn,
setEval,
setBoardOrientation,
]);
const isGameLoaded = const isGameLoaded =
gameFromUrl !== undefined || gameFromUrl !== undefined ||