mirror of
https://github.com/TheRealOwenRees/chess-scribe-website.git
synced 2026-07-23 01:46:57 +00:00
use shared IPosition interface for diagrams and current move state
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
import LichessPgnViewer from "lichess-pgn-viewer";
|
import LichessPgnViewer from "lichess-pgn-viewer";
|
||||||
import { useEffect, useRef } from "react";
|
import { useEffect, useRef } from "react";
|
||||||
|
import type { IPosition } from "#/interfaces.ts";
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
gamePgn: string;
|
gamePgn: string;
|
||||||
handlePlyChange: ({ ply, fen }: { ply: number; fen: string }) => void;
|
handlePlyChange: ({ ply, fen }: IPosition) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const PgnViewer = ({ gamePgn, handlePlyChange }: IProps) => {
|
const PgnViewer = ({ gamePgn, handlePlyChange }: IProps) => {
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
import { type ChangeEvent, useReducer, useRef, useState } from "react";
|
import { type ChangeEvent, useReducer, useRef, useState } from "react";
|
||||||
|
import type { IPosition } from "#/interfaces.ts";
|
||||||
import { gameReducer, initialGameState } from "#/reducers/gameReducer.ts";
|
import { gameReducer, initialGameState } from "#/reducers/gameReducer.ts";
|
||||||
import { downloadPDF } from "#/utils/pdfUtils.ts";
|
import { downloadPDF } from "#/utils/pdfUtils.ts";
|
||||||
import { buildPgnString, getHeaders } from "#/utils/pgnUtils.ts";
|
import { buildPgnString, getHeaders } from "#/utils/pgnUtils.ts";
|
||||||
import { downloadString } from "#/utils/stringUtils.ts";
|
import { downloadString } from "#/utils/stringUtils.ts";
|
||||||
|
|
||||||
interface IPosition {
|
|
||||||
ply: number;
|
|
||||||
fen: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const useChessGame = () => {
|
export const useChessGame = () => {
|
||||||
const [gameState, gameDispatch] = useReducer(gameReducer, initialGameState);
|
const [gameState, gameDispatch] = useReducer(gameReducer, initialGameState);
|
||||||
|
|
||||||
@@ -21,7 +17,7 @@ export const useChessGame = () => {
|
|||||||
|
|
||||||
const fileInputRef = useRef<HTMLInputElement | null>(null);
|
const fileInputRef = useRef<HTMLInputElement | null>(null);
|
||||||
|
|
||||||
const handlePlyChange = ({ ply, fen }: { ply: number; fen: string }) => {
|
const handlePlyChange = ({ ply, fen }: IPosition) => {
|
||||||
setCurrentPosition({ ply, fen });
|
setCurrentPosition({ ply, fen });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
export interface IPosition {
|
||||||
|
ply: number;
|
||||||
|
fen: string;
|
||||||
|
}
|
||||||
@@ -1,7 +1,4 @@
|
|||||||
interface IDiagrams {
|
import type { IPosition } from "#/interfaces.ts";
|
||||||
ply: number;
|
|
||||||
fen: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IHeader {
|
export interface IHeader {
|
||||||
event: string;
|
event: string;
|
||||||
@@ -23,7 +20,7 @@ export interface IHeader {
|
|||||||
export interface IGameState {
|
export interface IGameState {
|
||||||
pgn: string;
|
pgn: string;
|
||||||
headers: IHeader;
|
headers: IHeader;
|
||||||
diagrams: IDiagrams[];
|
diagrams: IPosition[];
|
||||||
diagramClock: boolean;
|
diagramClock: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +28,7 @@ type GameAction =
|
|||||||
| { type: "SET_GAME"; payload: { pgn: string; headers: IHeader } }
|
| { type: "SET_GAME"; payload: { pgn: string; headers: IHeader } }
|
||||||
| { type: "CLEAR_GAME" }
|
| { type: "CLEAR_GAME" }
|
||||||
| { type: "SET_HEADERS"; payload: IHeader }
|
| { type: "SET_HEADERS"; payload: IHeader }
|
||||||
| { type: "ADD_DIAGRAM"; payload: IDiagrams }
|
| { type: "ADD_DIAGRAM"; payload: IPosition }
|
||||||
| { type: "DELETE_DIAGRAM"; payload: { ply: number } }
|
| { type: "DELETE_DIAGRAM"; payload: { ply: number } }
|
||||||
| { type: "TOGGLE_DIAGRAM_CLOCK" };
|
| { type: "TOGGLE_DIAGRAM_CLOCK" };
|
||||||
|
|
||||||
@@ -86,7 +83,7 @@ export const gameReducer = (state: IGameState, action: GameAction) => {
|
|||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
diagrams: state.diagrams.filter(
|
diagrams: state.diagrams.filter(
|
||||||
(diagram: IDiagrams) => diagram.ply !== action.payload.ply,
|
(diagram: IPosition) => diagram.ply !== action.payload.ply,
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
case "TOGGLE_DIAGRAM_CLOCK":
|
case "TOGGLE_DIAGRAM_CLOCK":
|
||||||
|
|||||||
Reference in New Issue
Block a user