Skip to content

Commit c4fd948

Browse files
feat : add copy pgn button in analysis and database page
1 parent 3ca2ac1 commit c4fd948

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/pages/database.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
GridRowId,
1010
} from "@mui/x-data-grid";
1111
import { useCallback, useMemo } from "react";
12-
import { red } from "@mui/material/colors";
12+
import { blue, red } from "@mui/material/colors";
1313
import LoadGameButton from "@/sections/loadGame/loadGameButton";
1414
import { useGameDatabase } from "@/hooks/useGameDatabase";
1515
import { useRouter } from "next/router";
@@ -36,6 +36,16 @@ export default function GameDatabase() {
3636
[deleteGame]
3737
);
3838

39+
const handleCopyGameRow = useCallback(
40+
(id: GridRowId) => async () => {
41+
if (typeof id !== "number") {
42+
throw new Error("Unable to copy game");
43+
}
44+
await navigator.clipboard.writeText(games[id - 1].pgn);
45+
},
46+
[games]
47+
);
48+
3949
const columns: GridColDef[] = useMemo(
4050
() => [
4151
{
@@ -136,8 +146,28 @@ export default function GameDatabase() {
136146
];
137147
},
138148
},
149+
{
150+
field: "copy pgn",
151+
type: "actions",
152+
headerName: "Copy pgn",
153+
width: 100,
154+
cellClassName: "actions",
155+
getActions: ({ id }) => {
156+
return [
157+
<GridActionsCellItem
158+
icon={
159+
<Icon icon="ri:clipboard-line" color={blue[400]} width="20px" />
160+
}
161+
label="Copy pgn"
162+
onClick={handleCopyGameRow(id)}
163+
color="inherit"
164+
key={`${id}-copy-button`}
165+
/>,
166+
];
167+
},
168+
},
139169
],
140-
[handleDeleteGameRow, router]
170+
[handleDeleteGameRow, handleCopyGameRow, router]
141171
);
142172

143173
return (

src/sections/analysis/panelToolbar/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Grid2 as Grid, IconButton, Tooltip } from "@mui/material";
22
import { Icon } from "@iconify/react";
33
import { useAtomValue } from "jotai";
4-
import { boardAtom } from "../states";
4+
import { boardAtom, gameAtom } from "../states";
55
import { useChessActions } from "@/hooks/useChessActions";
66
import FlipBoardButton from "./flipBoardButton";
77
import NextMoveButton from "./nextMoveButton";
@@ -16,7 +16,7 @@ export default function PanelToolBar() {
1616
useChessActions(boardAtom);
1717

1818
const boardHistory = board.history();
19-
19+
const game = useAtomValue(gameAtom);
2020
useEffect(() => {
2121
const onKeyDown = (e: KeyboardEvent) => {
2222
if (boardHistory.length === 0) return;
@@ -65,7 +65,16 @@ export default function PanelToolBar() {
6565
<NextMoveButton />
6666

6767
<GoToLastPositionButton />
68-
68+
<Tooltip title="Copy pgn">
69+
<IconButton
70+
disabled={game.history().length === 0}
71+
onClick={() => {
72+
navigator.clipboard.writeText(game.pgn());
73+
}}
74+
>
75+
<Icon icon="ri:clipboard-line" height={30} />
76+
</IconButton>
77+
</Tooltip>
6978
<SaveButton />
7079
</Grid>
7180
);

0 commit comments

Comments
 (0)