Skip to content

Commit

Permalink
Add symbols to opening database view
Browse files Browse the repository at this point in the history
  • Loading branch information
Scoutboy06 committed May 11, 2024
1 parent 3f283a2 commit 2013f3f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
17 changes: 6 additions & 11 deletions src/components/boards/MoveCell.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -71,7 +66,7 @@ const MoveCell = forwardRef(function MoveCell(
onContextMenu={props.onContextMenu}
>
{props.isStart && <IconFlag style={{ marginRight: 5 }} size="0.875rem" />}
{moveNotationType === "symbols" ? addPieceIcon(props.move) : props.move}
{moveNotationType === "symbols" ? addPieceSymbol(props.move) : props.move}
{props.annotations.join("")}
</Box>
);
Expand Down
10 changes: 9 additions & 1 deletion src/components/panels/database/OpeningsTable.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand Down Expand Up @@ -60,7 +64,11 @@ function OpeningsTable({
Game end
</Text>
);
return <Text fz="sm">{move}</Text>;
return (
<Text fz="sm">
{moveNotationType === "symbols" ? addPieceSymbol(move) : move}
</Text>
);
},
},
{
Expand Down
9 changes: 9 additions & 0 deletions src/utils/annotation.ts
Original file line number Diff line number Diff line change
@@ -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 =
| ""
| "!"
Expand Down

0 comments on commit 2013f3f

Please sign in to comment.