save as PGN, reuse clear game handler, get headers

This commit is contained in:
2026-06-01 15:18:10 +02:00
parent 38abf30ecc
commit 324e4eac31
5 changed files with 162 additions and 17 deletions
+13 -12
View File
@@ -1,13 +1,16 @@
import { type ChangeEvent, useReducer, useRef } from "react";
import Chessboard from "#/pages/Chessboard.tsx";
import { gameReducer, initialGameState } from "#/reducers/gameReducer.ts";
import { buildPgnString, getHeaders } from "#/utils/pgnUtils.ts";
import { downloadString } from "#/utils/stringUtils.ts";
const ChessboardContainer = () => {
const [gameState, gameDispatch] = useReducer(gameReducer, initialGameState);
const fileInputRef = useRef<HTMLInputElement | null>(null);
const handleSavePgn = () => {
console.log("Save PGN");
const pgnString = buildPgnString(gameState);
downloadString(pgnString, "game.pgn");
};
const handleSavePdf = () => {
@@ -44,25 +47,23 @@ const ChessboardContainer = () => {
}
if (pgnString) {
const headers = {
...getHeaders(pgnString),
title: "",
subtitle: "",
};
gameDispatch({
type: "SET_GAME",
payload: { pgn: pgnString, headers: "" },
payload: { pgn: pgnString, headers: headers },
});
} else {
gameDispatch({
type: "CLEAR_GAME",
});
e.target.files = null;
handleClearGame();
}
};
reader.readAsText(selectedFile);
} else {
if (fileInputRef.current) {
fileInputRef.current.value = "";
}
e.target.files = null;
gameDispatch({
type: "CLEAR_GAME",
});
handleClearGame();
}
};