From 7cbad399ed5687671647cafd1eaaf368acee9ceb Mon Sep 17 00:00:00 2001 From: supertorpe Date: Fri, 4 Jul 2025 15:45:45 +0200 Subject: [PATCH] feat : add autoload pgn with a base64 encoded "pgn" query param (#57) --- src/lib/helpers.ts | 16 ++++++++++++++++ src/pages/index.tsx | 18 ++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index f5d6864..a9d832e 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -16,3 +16,19 @@ export const isInViewport = (element: HTMLElement) => { export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); + +export const decodeBase64 = (encoded: string | null): string | null => { + if (!encoded) return null; + try { + return atob(encoded); + } catch (err) { + console.error("Error decoding base64:", err); + return null; + } +}; + +export const decodeBase64Param = (param: string): string | null => { + const params = new URLSearchParams(location.search); + const encodedParam = params.get(param); + return decodeBase64(encodedParam); +}; diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 5065dcf..e4fb1ad 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -26,6 +26,7 @@ import { Icon } from "@iconify/react"; import EngineSettingsButton from "@/sections/engineSettings/engineSettingsButton"; import GraphTab from "@/sections/analysis/panelBody/graphTab"; import { PageTitle } from "@/components/pageTitle"; +import { decodeBase64Param } from "@/lib/helpers"; export default function GameAnalysis() { const theme = useTheme(); @@ -38,18 +39,31 @@ export default function GameAnalysis() { const game = useAtomValue(gameAtom); 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 (!gameId) { + if (pgnParam) { + setGameEval(undefined); + setGamePgn(pgnParam); + } else if (!gameId) { resetBoard(); setGameEval(undefined); setBoardOrientation(true); resetGame({ noHeaders: true }); } - }, [gameId, setGameEval, setBoardOrientation, resetBoard, resetGame]); + }, [ + gameId, + pgnParam, + setGameEval, + setGamePgn, + setBoardOrientation, + resetBoard, + resetGame, + ]); const showMovesTab = game.history().length > 0 || board.history().length > 0;