-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathloadGame.tsx
More file actions
124 lines (112 loc) · 3.71 KB
/
loadGame.tsx
File metadata and controls
124 lines (112 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import LoadGameButton from "../../loadGame/loadGameButton";
import { useCallback, useEffect, useMemo } from "react";
import { useChessActions } from "@/hooks/useChessActions";
import {
boardAtom,
boardOrientationAtom,
evaluationProgressAtom,
gameAtom,
gameEvalAtom,
} from "../states";
import { useGameDatabase } from "@/hooks/useGameDatabase";
import { useAtomValue, useSetAtom } from "jotai";
import { Chess } from "chess.js";
import { useRouter } from "next/router";
import { GameEval } from "@/types/eval";
import { fetchLichessGame } from "@/lib/lichess";
import { decompressPgn } from "@/lib/shareGame";
export default function LoadGame() {
const router = useRouter();
const game = useAtomValue(gameAtom);
const { setPgn: setGamePgn } = useChessActions(gameAtom);
const { resetToStartingPosition: resetBoard } = useChessActions(boardAtom);
const { gameFromUrl } = useGameDatabase();
const setEval = useSetAtom(gameEvalAtom);
const setBoardOrientation = useSetAtom(boardOrientationAtom);
const evaluationProgress = useAtomValue(evaluationProgressAtom);
const joinedGameHistory = useMemo(() => game.history().join(), [game]);
const resetAndSetGamePgn = useCallback(
(pgn: string, orientation?: boolean, gameEval?: GameEval) => {
const gameFromPgn = new Chess();
gameFromPgn.loadPgn(pgn);
if (joinedGameHistory === gameFromPgn.history().join()) return;
resetBoard(pgn);
setEval(gameEval);
setGamePgn(pgn);
setBoardOrientation(orientation ?? true);
},
[joinedGameHistory, resetBoard, setGamePgn, setEval, setBoardOrientation]
);
const {
lichessGameId,
orientation: orientationParam,
pgn: pgnParam,
} = router.query;
useEffect(() => {
const handleLichess = async (id: string) => {
const res = await fetchLichessGame(id);
if (typeof res === "string") {
resetAndSetGamePgn(res, orientationParam !== "black");
}
};
if (gameFromUrl) {
const orientation = !(
gameFromUrl.site === "Chesskit.org" && gameFromUrl.black.name === "You"
);
resetAndSetGamePgn(gameFromUrl.pgn, orientation, gameFromUrl.eval);
} else if (typeof lichessGameId === "string" && !!lichessGameId) {
handleLichess(lichessGameId);
} else if (typeof pgnParam === "string" && !!pgnParam) {
const decompressed = decompressPgn(pgnParam);
if (decompressed) {
resetAndSetGamePgn(decompressed, orientationParam !== "black");
}
}
}, [
gameFromUrl,
lichessGameId,
orientationParam,
pgnParam,
resetAndSetGamePgn,
]);
useEffect(() => {
const eventHandler = (event: MessageEvent) => {
try {
if (!event?.data?.pgn) return;
const { pgn, orientation } = event.data as {
pgn: string;
orientation?: "white" | "black";
};
resetAndSetGamePgn(pgn, orientation !== "black");
} catch (error) {
console.error("Error processing message event:", error);
}
};
window.addEventListener("message", eventHandler);
return () => {
window.removeEventListener("message", eventHandler);
};
}, [resetAndSetGamePgn]);
const isGameLoaded =
gameFromUrl !== undefined ||
(!!game.getHeaders().White && game.getHeaders().White !== "?") ||
game.history().length > 0;
if (evaluationProgress) return null;
return (
<LoadGameButton
label={isGameLoaded ? "Load another game" : "Load game"}
size="small"
setGame={async (game) => {
await router.replace(
{
query: {},
pathname: router.pathname,
},
undefined,
{ shallow: true, scroll: false }
);
resetAndSetGamePgn(game.pgn());
}}
/>
);
}