"use client"; import { useState, useCallback, useId } from "react"; import { motion } from "framer-motion"; import type { PatternCell } from "@/lib/puzzles/patterns"; interface PatternBoardProps { grid: (PatternCell | null)[]; choices: PatternCell[]; hint: string; activeAttributes: string[]; onSubmit: (selectedIndex: number) => void; disabled?: boolean; } const SIZE_SCALE: Record = { small: 0.5, medium: 0.75, large: 1.0, }; /** * Render a shape SVG element with proper fill, size, and rotation. */ function ShapeIcon({ cell, size: displaySize = 40, }: { cell: PatternCell; size?: number; }) { const scale = SIZE_SCALE[cell.size] ?? 0.75; const scaledSize = displaySize * scale; const center = displaySize / 2; const isSolid = cell.fill === "solid"; const isStriped = cell.fill === "striped"; // Stable unique ID for SVG element — React 18 useId is pure/deterministic const baseId = useId(); const patternId = `stripe-${baseId}`; return ( {isStriped && ( )} {cell.shape === "circle" && ( )} {cell.shape === "square" && ( )} {cell.shape === "triangle" && ( )} {cell.shape === "diamond" && ( )} {cell.shape === "star" && (() => { const points: string[] = []; for (let i = 0; i < 5; i++) { const outerAngle = (i * 72 - 90) * (Math.PI / 180); const innerAngle = ((i * 72 + 36) - 90) * (Math.PI / 180); const outerR = scaledSize / 2; const innerR = scaledSize / 4.5; points.push( `${center + outerR * Math.cos(outerAngle)},${center + outerR * Math.sin(outerAngle)}`, ); points.push( `${center + innerR * Math.cos(innerAngle)},${center + innerR * Math.sin(innerAngle)}`, ); } return ( ); })()} {cell.shape === "hexagon" && (() => { const points: string[] = []; for (let i = 0; i < 6; i++) { const angle = (i * 60 - 30) * (Math.PI / 180); const r = scaledSize / 2; points.push( `${center + r * Math.cos(angle)},${center + r * Math.sin(angle)}`, ); } return ( ); })()} ); } export function PatternBoard({ grid, choices, hint, activeAttributes, onSubmit, disabled = false, }: PatternBoardProps) { const [selectedIndex, setSelectedIndex] = useState(null); const [showHint, setShowHint] = useState(false); const handleSelect = useCallback( (index: number) => { if (disabled) return; setSelectedIndex(index); }, [disabled], ); const handleSubmit = useCallback(() => { if (selectedIndex === null || disabled) return; onSubmit(selectedIndex); }, [selectedIndex, disabled, onSubmit]); return (
{/* Header: attributes badge + hint toggle */}
Pattern Matrix · {activeAttributes.join(", ")}
{/* Hint */} {showHint && ( {hint} )} {/* 3×3 Grid */}
{grid.map((cell, idx) => ( {cell ? ( ) : ( ? )} ))}
{/* Choices */}

Select the missing piece

{choices.map((choice, idx) => ( handleSelect(idx)} disabled={disabled} className={`w-16 h-16 sm:w-20 sm:h-20 flex items-center justify-center rounded-lg border-2 transition-all ${ selectedIndex === idx ? "border-violet-400 bg-violet-500/20 ring-2 ring-violet-400/30 scale-105" : "border-white/10 bg-white/5 hover:border-violet-400/30 hover:bg-violet-500/10" } disabled:opacity-50 disabled:cursor-not-allowed`} > ))}
{/* Submit */}
); }