<commit_before>import React from "react";
import { useDrag } from "react-dnd";
import { Color } from "shogi.js";
import { IPlaceFormat } from "json-kifu-format/dist/src/Formats";

export type PieceProps = {
  place: IPlaceFormat | undefined;
  color: Color;
  kind: string;
  reversed: boolean;
};

export type PieceDragObject = {
  type: "piece";
  from: IPlaceFormat | undefined;
  color: Color;
  kind: string;
};

export const Piece: React.FC<PieceProps> = ({ place, color, kind, reversed }) => {
  const normalizedColor = normalizeColor(color, reversed);

  const item: PieceDragObject = {
    type: "piece",
    color,
    kind,
    from: place,
  };

  const [{ isDragging }, drag] = useDrag({
    item,
    collect: (monitor) => ({
      isDragging: !!monitor.isDragging(),
    }),
  });

  const imageURL = `/images/shogi-pieces/${normalizedColor}${kind}.svg`;
  return (
    <img
      ref={drag}
      className="piece"
      src={imageURL}
      alt={kind}
      style={{ opacity: isDragging ? 0.5 : 1 }}
    />
  );
};

const normalizeColor = (color: Color, reversed: boolean): Color => {
  if (!reversed) {
    return color;
  }
  return (1 - color) as Color;
};<commit_msg>Refactor Piece<commit_after>import React from "react";
import { useDrag } from "react-dnd";
import { Color } from "shogi.js";
import { IPlaceFormat } from "json-kifu-format/dist/src/Formats";

export type PieceProps = {
  place: IPlaceFormat | undefined;
  color: Color;
  kind: string;
  reversed: boolean;
};

export type PieceDragObject = {
  type: "piece";
  from: IPlaceFormat | undefined;
  color: Color;
  kind: string;
};

export const Piece: React.FC<PieceProps> = ({ place, color, kind, reversed }) => {
  const item: PieceDragObject = {
    type: "piece",
    color, // Don't use orientation here
    kind,
    from: place,
  };

  const [{ isDragging }, drag] = useDrag({
    item,
    collect: (monitor) => ({
      isDragging: !!monitor.isDragging(),
    }),
  });

  const orientation = !reversed ? color : ((1 - color) as Color);
  const imageURL = `/images/shogi-pieces/${orientation}${kind}.svg`;

  return (
    <img
      ref={drag}
      className="piece"
      src={imageURL}
      alt={kind}
      style={{ opacity: isDragging ? 0.5 : 1 }}
    />
  );
};
{'fraction_non_alphanumeric': 0.1562913907284768, 'fraction_numerical': 0.003532008830022075, 'mean_word_length': 3.15018315018315, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5977511', 'n_tokens_mistral': 781, 'n_tokens_neox': 758, 'n_words': 193}