From 2013f3fce84915811abec5316922774b1da79b03 Mon Sep 17 00:00:00 2001 From: Elias Wennerlund Date: Sat, 11 May 2024 02:29:46 +0200 Subject: [PATCH] Add symbols to opening database view --- src/components/boards/MoveCell.tsx | 17 ++++++----------- .../panels/database/OpeningsTable.tsx | 10 +++++++++- src/utils/annotation.ts | 9 +++++++++ 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/components/boards/MoveCell.tsx b/src/components/boards/MoveCell.tsx index 2826a4c7..a1e10299 100644 --- a/src/components/boards/MoveCell.tsx +++ b/src/components/boards/MoveCell.tsx @@ -1,20 +1,15 @@ import { moveNotationTypeAtom } from "@/state/atoms"; -import { ANNOTATION_INFO, type Annotation } from "@/utils/annotation"; +import { + ANNOTATION_INFO, + type Annotation, + addPieceSymbol, +} from "@/utils/annotation"; import { Box, rgba, useMantineTheme } from "@mantine/core"; import { IconFlag } from "@tabler/icons-react"; import { useAtom } from "jotai"; import { type ForwardedRef, forwardRef } from "react"; import * as classes from "./MoveCell.css"; -const pieceChars = { K: "♔", Q: "♕", R: "♖", B: "♗", N: "♘" }; - -function addPieceIcon(move: string): string { - const pieceChar = pieceChars[move[0] as keyof typeof pieceChars]; - - if (typeof pieceChar === "undefined") return move; - return pieceChar + move.slice(1); -} - interface MoveCellProps { annotations: Annotation[]; isStart: boolean; @@ -71,7 +66,7 @@ const MoveCell = forwardRef(function MoveCell( onContextMenu={props.onContextMenu} > {props.isStart && } - {moveNotationType === "symbols" ? addPieceIcon(props.move) : props.move} + {moveNotationType === "symbols" ? addPieceSymbol(props.move) : props.move} {props.annotations.join("")} ); diff --git a/src/components/panels/database/OpeningsTable.tsx b/src/components/panels/database/OpeningsTable.tsx index 9b7aa449..a730765e 100644 --- a/src/components/panels/database/OpeningsTable.tsx +++ b/src/components/panels/database/OpeningsTable.tsx @@ -1,7 +1,10 @@ import { TreeStateContext } from "@/components/common/TreeStateContext"; +import { moveNotationTypeAtom } from "@/state/atoms"; +import { addPieceSymbol } from "@/utils/annotation"; import type { Opening } from "@/utils/db"; import { formatNumber } from "@/utils/format"; import { Group, Progress, Text } from "@mantine/core"; +import { useAtom } from "jotai"; import { DataTable } from "mantine-datatable"; import { memo, useContext } from "react"; import { useStore } from "zustand"; @@ -15,6 +18,7 @@ function OpeningsTable({ }) { const store = useContext(TreeStateContext)!; const makeMove = useStore(store, (s) => s.makeMove); + const [moveNotationType] = useAtom(moveNotationTypeAtom); const whiteTotal = openings?.reduce((acc, curr) => acc + curr.white, 0); const blackTotal = openings?.reduce((acc, curr) => acc + curr.black, 0); @@ -60,7 +64,11 @@ function OpeningsTable({ Game end ); - return {move}; + return ( + + {moveNotationType === "symbols" ? addPieceSymbol(move) : move} + + ); }, }, { diff --git a/src/utils/annotation.ts b/src/utils/annotation.ts index 9fc77369..3a26a8d2 100644 --- a/src/utils/annotation.ts +++ b/src/utils/annotation.ts @@ -1,5 +1,14 @@ import type { MantineColor } from "@mantine/core"; +const pieceChars = { K: "♔", Q: "♕", R: "♖", B: "♗", N: "♘" }; + +export function addPieceSymbol(move: string): string { + const pieceChar = pieceChars[move[0] as keyof typeof pieceChars]; + + if (typeof pieceChar === "undefined") return move; + return pieceChar + move.slice(1); +} + export type Annotation = | "" | "!"