Skip to content

Commit a67abd1

Browse files
feat: add export pgn and copy fen option below game board
1 parent ec625e2 commit a67abd1

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/components/Core/ExportGame.tsx

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { useEffect, useState } from 'react'
2+
import { Chess } from 'chess.ts'
3+
4+
import { Move } from 'src/types'
5+
6+
interface Props {
7+
moves: Move[]
8+
}
9+
10+
export const ExportGame: React.FC<Props> = ({ moves }) => {
11+
const [pgn, setPgn] = useState('')
12+
13+
useEffect(() => {
14+
const chess = new Chess()
15+
moves.forEach((move) => {
16+
if (move.san) {
17+
chess.move(move.san)
18+
}
19+
})
20+
setPgn(chess.pgn())
21+
}, [moves])
22+
23+
const copy = (content: string) => {
24+
navigator.clipboard.writeText(content)
25+
}
26+
27+
return (
28+
<div className="flex flex-row items-center justify-center gap-2">
29+
<div className="flex h-10 items-center overflow-hidden rounded border border-primary/10 bg-background-1">
30+
<div className="flex w-[500px] items-center justify-start bg-background-1 px-4">
31+
<p className="whitespace-nowrap text-sm text-secondary">
32+
{moves[moves.length - 1].board}
33+
</p>
34+
</div>
35+
<button
36+
onClick={() => copy(moves[moves.length - 1].board)}
37+
className="flex h-10 w-10 min-w-10 items-center justify-center border-l border-primary/10 bg-background-2 transition duration-200 hover:bg-background-3"
38+
>
39+
<span className="material-symbols-outlined text-xl">
40+
content_copy
41+
</span>
42+
</button>
43+
</div>
44+
<button
45+
onClick={() => copy(pgn)}
46+
className="flex h-10 items-center justify-center rounded border border-primary/10 bg-background-2 px-4 py-1 transition duration-300 hover:bg-background-3"
47+
>
48+
Copy PGN
49+
</button>
50+
</div>
51+
)
52+
}

src/components/Core/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './GameInfo'
2+
export * from './ExportGame'
23
export * from './ContinueAgainstMaia'

src/components/GameplayInterface/GameplayInterface.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
WindowSizeContext,
1010
} from 'src/contexts'
1111
import { GameBoard, MovesContainer, BoardController } from 'src/components'
12-
import { GameInfo } from '../Core'
12+
import { GameInfo, ExportGame } from '../Core'
1313
import { GameClock } from '../GameClock'
1414
import { useGameController } from 'src/hooks'
1515
import { StatsDisplay } from '../StatsDisplay'
@@ -159,7 +159,7 @@ export const GameplayInterface: React.FC<Props> = (
159159
const desktopLayout = (
160160
<>
161161
<div className="flex h-full flex-1 flex-col justify-center gap-1">
162-
<div className="mt-2 flex w-full flex-row items-center justify-center gap-1">
162+
<div className="flex w-full flex-row items-center justify-center gap-1">
163163
<div
164164
style={{
165165
maxWidth: 'min(20vw, 100vw - 75vh)',
@@ -219,6 +219,7 @@ export const GameplayInterface: React.FC<Props> = (
219219
) : null}
220220
</div>
221221
</div>
222+
<ExportGame moves={game.moves.slice(0, controller.currentIndex + 1)} />
222223
</div>
223224
</>
224225
)

0 commit comments

Comments
 (0)