From b8bd48885e7ad0dd850c4c8606454580ac156927 Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 2 Jun 2026 14:24:17 +0200 Subject: [PATCH] pass ply change to callback --- src/components/PgnViewer.tsx | 12 +++++++----- src/hooks/useChessGame.ts | 6 ++++++ src/pages/Chessboard.container.tsx | 1 + src/pages/Chessboard.tsx | 11 ++++++++--- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/components/PgnViewer.tsx b/src/components/PgnViewer.tsx index aeb6a99..5e44dcf 100644 --- a/src/components/PgnViewer.tsx +++ b/src/components/PgnViewer.tsx @@ -3,9 +3,10 @@ import { useEffect, useRef } from "react"; interface IProps { gamePgn: string; + handlePlyChange: ({ ply, fen }: { ply: number; fen: string }) => void; } -const PgnViewer = ({ gamePgn }: IProps) => { +const PgnViewer = ({ gamePgn, handlePlyChange }: IProps) => { const containerRef = useRef(null); const viewerRef = useRef(null); @@ -18,7 +19,6 @@ const PgnViewer = ({ gamePgn }: IProps) => { scrollToMove: false, }); - // BOARD EVENT LISTENERS - MOVE INTO HOOK? const boardButtons = document.querySelectorAll(".lpv__controls"); const movesList = document.querySelectorAll(".lpv__moves"); if (!boardButtons || !movesList) return; @@ -27,6 +27,7 @@ const PgnViewer = ({ gamePgn }: IProps) => { setTimeout(() => handleClick(), 250); }; + // Hack using DOM manipulation const boardEventListener = () => { const variationTags = document.querySelector("variation"); const moves = [...document.querySelectorAll("move")].filter((m) => { @@ -40,8 +41,9 @@ const PgnViewer = ({ gamePgn }: IProps) => { }; const handleClick = () => { - const { ply, moves } = boardEventListener(); - console.log(ply, moves); + const { ply } = boardEventListener(); + const fen = viewerRef.current?.curData().fen; + handlePlyChange({ ply, fen }); }; boardButtons.forEach((button) => { @@ -69,7 +71,7 @@ const PgnViewer = ({ gamePgn }: IProps) => { move.removeEventListener("touchstart", clickHandlerDelay); }); }; - }, [gamePgn]); + }, [gamePgn, handlePlyChange]); return
; }; diff --git a/src/hooks/useChessGame.ts b/src/hooks/useChessGame.ts index cda0477..29b0f28 100644 --- a/src/hooks/useChessGame.ts +++ b/src/hooks/useChessGame.ts @@ -7,6 +7,11 @@ export const useChessGame = () => { const [gameState, gameDispatch] = useReducer(gameReducer, initialGameState); const fileInputRef = useRef(null); + const handlePlyChange = ({ ply, fen }: { ply: number; fen: string }) => { + console.log("Ply change", ply, fen); + return; + }; + const handleSavePgn = () => { const pgnString = buildPgnString(gameState); downloadString(pgnString, "game.pgn"); @@ -84,6 +89,7 @@ export const useChessGame = () => { handleClearGame, handleLoadPgn, handleToggleClock, + handlePlyChange, handleToggleDiagram, }; }; diff --git a/src/pages/Chessboard.container.tsx b/src/pages/Chessboard.container.tsx index f19c96d..9e08ee7 100644 --- a/src/pages/Chessboard.container.tsx +++ b/src/pages/Chessboard.container.tsx @@ -13,6 +13,7 @@ const ChessboardContainer = () => { handleSavePdf={chessGameProps.handleSavePdf} handleToggleClock={chessGameProps.handleToggleClock} handleToggleDiagram={chessGameProps.handleToggleDiagram} + handlePlyChange={chessGameProps.handlePlyChange} fileInputRef={chessGameProps.fileInputRef} /> ); diff --git a/src/pages/Chessboard.tsx b/src/pages/Chessboard.tsx index 355e45a..5a821d8 100644 --- a/src/pages/Chessboard.tsx +++ b/src/pages/Chessboard.tsx @@ -12,6 +12,7 @@ interface IProps { handleSavePdf: () => void; handleToggleClock: () => void; handleToggleDiagram: () => void; + handlePlyChange: ({ ply, fen }: { ply: number; fen: string }) => void; fileInputRef: RefObject; } @@ -23,6 +24,7 @@ const Chessboard = ({ handleSavePdf, handleToggleClock, handleToggleDiagram, + handlePlyChange, fileInputRef, }: IProps) => { return ( @@ -65,7 +67,10 @@ const Chessboard = ({ Save PDF
- +