From c4fd94860c8cac4541fad9c744d7cab1c45a0483 Mon Sep 17 00:00:00 2001
From: Theprathamshah <75787080+Theprathamshah@users.noreply.github.com>
Date: Tue, 1 Apr 2025 04:02:02 +0530
Subject: [PATCH] feat : add copy pgn button in analysis and database page
---
src/pages/database.tsx | 34 ++++++++++++++++++--
src/sections/analysis/panelToolbar/index.tsx | 15 +++++++--
2 files changed, 44 insertions(+), 5 deletions(-)
diff --git a/src/pages/database.tsx b/src/pages/database.tsx
index 458faa4..8696e62 100644
--- a/src/pages/database.tsx
+++ b/src/pages/database.tsx
@@ -9,7 +9,7 @@ import {
GridRowId,
} from "@mui/x-data-grid";
import { useCallback, useMemo } from "react";
-import { red } from "@mui/material/colors";
+import { blue, red } from "@mui/material/colors";
import LoadGameButton from "@/sections/loadGame/loadGameButton";
import { useGameDatabase } from "@/hooks/useGameDatabase";
import { useRouter } from "next/router";
@@ -36,6 +36,16 @@ export default function GameDatabase() {
[deleteGame]
);
+ const handleCopyGameRow = useCallback(
+ (id: GridRowId) => async () => {
+ if (typeof id !== "number") {
+ throw new Error("Unable to copy game");
+ }
+ await navigator.clipboard.writeText(games[id - 1].pgn);
+ },
+ [games]
+ );
+
const columns: GridColDef[] = useMemo(
() => [
{
@@ -136,8 +146,28 @@ export default function GameDatabase() {
];
},
},
+ {
+ field: "copy pgn",
+ type: "actions",
+ headerName: "Copy pgn",
+ width: 100,
+ cellClassName: "actions",
+ getActions: ({ id }) => {
+ return [
+
+ }
+ label="Copy pgn"
+ onClick={handleCopyGameRow(id)}
+ color="inherit"
+ key={`${id}-copy-button`}
+ />,
+ ];
+ },
+ },
],
- [handleDeleteGameRow, router]
+ [handleDeleteGameRow, handleCopyGameRow, router]
);
return (
diff --git a/src/sections/analysis/panelToolbar/index.tsx b/src/sections/analysis/panelToolbar/index.tsx
index d0b1f9b..27faa36 100644
--- a/src/sections/analysis/panelToolbar/index.tsx
+++ b/src/sections/analysis/panelToolbar/index.tsx
@@ -1,7 +1,7 @@
import { Grid2 as Grid, IconButton, Tooltip } from "@mui/material";
import { Icon } from "@iconify/react";
import { useAtomValue } from "jotai";
-import { boardAtom } from "../states";
+import { boardAtom, gameAtom } from "../states";
import { useChessActions } from "@/hooks/useChessActions";
import FlipBoardButton from "./flipBoardButton";
import NextMoveButton from "./nextMoveButton";
@@ -16,7 +16,7 @@ export default function PanelToolBar() {
useChessActions(boardAtom);
const boardHistory = board.history();
-
+ const game = useAtomValue(gameAtom);
useEffect(() => {
const onKeyDown = (e: KeyboardEvent) => {
if (boardHistory.length === 0) return;
@@ -65,7 +65,16 @@ export default function PanelToolBar() {
-
+
+ {
+ navigator.clipboard.writeText(game.pgn());
+ }}
+ >
+
+
+
);