File size: 2,968 Bytes
ece5841
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Image, createStyles } from "@mantine/core";
import React, { useContext, useEffect } from "react";
import { useDraggable } from "@dnd-kit/core";
import { ChessGameContext } from "../context/chess-game-context";
import PropTypes from "prop-types";
import { motion } from "framer-motion";
const useStyles = createStyles(() => ({
  "chess-piece": {
    outlineStyle: "none",
    boxShadow: "none",
    borderColor: "transparent",
  },
}));

const Piece = ({ cell }) => {
  const { classes } = useStyles();
  const { selectPiece, isSquareMarked } = useContext(ChessGameContext);
  let { square, type, color } = cell;
  let marked = isSquareMarked(square);
  let logo = null;

  switch (type) {
    case "p":
      logo = color === "w" ? "pawn_white" : "pawn_black";
      break;
    case "r":
      logo = color === "w" ? "rook_white" : "rook_black";
      break;
    case "n":
      logo = color === "w" ? "knight_white" : "knight_black";
      break;
    case "b":
      logo = color === "w" ? "bishop_white" : "bishop_black";
      break;
    case "q":
      logo = color === "w" ? "queen_white" : "queen_black";
      break;
    case "k":
      logo = color === "w" ? "king_white" : "king_black";
      break;
  }

  const { attributes, listeners, setNodeRef, transform, isDragging } =
    useDraggable({
      id: square,
      data: {
        ...cell,
      },
    });

  let borderColor = marked ? "#77777744" : "transparent";

  const style = transform
    ? {
        transform: `translate3d(${transform.x}px, ${transform.y}px, 0)`,
        cursor: isDragging ? "grabbing" : "pointer",
        zIndex: 1000,
        aspectRatio: "1",
        touchAction: "none",
        borderRadius: "10px",
        outline: "none",
      }
    : {
        zIndex: 10,
      };

  useEffect(() => {
    if (isDragging) {
      selectPiece(cell);
    }
  }, [isDragging]);

  if (logo) {
    return (
      <div style={{ position: "relative", width: "100%", height: "100%" }}>
        <div
          style={{
            borderRadius: "50%",
            position: "absolute",
            boxSizing: "border-box",
            borderWidth: "7px",
            width: "100%",
            height: "100%",
            borderStyle: "solid",
            borderColor,
          }}
        ></div>
        <motion.div style={{ width: "100%", height: "100%" }} layout>
          <Image
            classNames={{ root: classes["chess-piece"] }}
            ref={setNodeRef}
            style={style}
            sx={{ cursor: "pointer" }}
            {...listeners}
            {...attributes}
            src={`/assets/images/${logo}.png`}
          />
        </motion.div>
      </div>
    );
  } else {
    return <div style={{ width: "100%" }}></div>;
  }
};

Piece.propTypes = {
  cell: PropTypes.shape({
    square: PropTypes.string.isRequired,
    type: PropTypes.oneOf(["p", "r", "n", "b", "q", "k"]),
    color: PropTypes.oneOf(["w", "b"]),
  }),
};

export default Piece;