import React, { memo, useMemo } from 'react'; interface CapturedPiecesProps { captured: string[]; // Array of piece types, e.g., ['p', 'n', 'q'] color: 'w' | 'b'; // The color of the pieces (to display the correct icon) score?: number | null; // Material advantage, e.g., +2 } const PIECE_ICONS: Record = { 'p': '♟', 'n': '♞', 'b': '♝', 'r': '♜', 'q': '♛', 'k': '♚', // King is never captured, but for completeness }; const sortOrder = ['q', 'r', 'b', 'n', 'p']; /** * Displays captured pieces with optional material advantage score. * Memoized to prevent unnecessary re-renders. */ export const CapturedPieces = memo(function CapturedPieces({ captured, color, score }: CapturedPiecesProps) { // Memoize sorted pieces to prevent recalculation on every render const sortedPieces = useMemo( () => [...captured].sort((a, b) => sortOrder.indexOf(a) - sortOrder.indexOf(b)), [captured] ); return (
{sortedPieces.map((piece, index) => ( {PIECE_ICONS[piece.toLowerCase()] || piece} ))}
{score && score > 0 && ( +{score} )}
); });