RotNot / src /components /logic /pattern-board.tsx
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
8.92 kB
"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<string, number> = {
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 <pattern> element — React 18 useId is pure/deterministic
const baseId = useId();
const patternId = `stripe-${baseId}`;
return (
<svg
width={displaySize}
height={displaySize}
viewBox={`0 0 ${displaySize} ${displaySize}`}
className="text-violet-200"
>
{isStriped && (
<defs>
<pattern
id={patternId}
patternUnits="userSpaceOnUse"
width="4"
height="4"
patternTransform={`rotate(45)`}
>
<line
x1="0"
y1="0"
x2="0"
y2="4"
stroke="currentColor"
strokeWidth="1.5"
/>
</pattern>
</defs>
)}
<g transform={`rotate(${cell.rotation} ${center} ${center})`}>
{cell.shape === "circle" && (
<circle
cx={center}
cy={center}
r={scaledSize / 2}
fill={isSolid ? "currentColor" : isStriped ? `url(#${patternId})` : "none"}
stroke="currentColor"
strokeWidth={isSolid ? 0 : 2}
/>
)}
{cell.shape === "square" && (
<rect
x={center - scaledSize / 2}
y={center - scaledSize / 2}
width={scaledSize}
height={scaledSize}
fill={isSolid ? "currentColor" : isStriped ? `url(#${patternId})` : "none"}
stroke="currentColor"
strokeWidth={isSolid ? 0 : 2}
/>
)}
{cell.shape === "triangle" && (
<polygon
points={`${center},${center - scaledSize / 2} ${center - scaledSize / 2},${center + scaledSize / 2} ${center + scaledSize / 2},${center + scaledSize / 2}`}
fill={isSolid ? "currentColor" : isStriped ? `url(#${patternId})` : "none"}
stroke="currentColor"
strokeWidth={isSolid ? 0 : 2}
/>
)}
{cell.shape === "diamond" && (
<polygon
points={`${center},${center - scaledSize / 2} ${center + scaledSize / 2},${center} ${center},${center + scaledSize / 2} ${center - scaledSize / 2},${center}`}
fill={isSolid ? "currentColor" : isStriped ? `url(#${patternId})` : "none"}
stroke="currentColor"
strokeWidth={isSolid ? 0 : 2}
/>
)}
{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 (
<polygon
points={points.join(" ")}
fill={isSolid ? "currentColor" : isStriped ? `url(#${patternId})` : "none"}
stroke="currentColor"
strokeWidth={isSolid ? 0 : 2}
/>
);
})()}
{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 (
<polygon
points={points.join(" ")}
fill={isSolid ? "currentColor" : isStriped ? `url(#${patternId})` : "none"}
stroke="currentColor"
strokeWidth={isSolid ? 0 : 2}
/>
);
})()}
</g>
</svg>
);
}
export function PatternBoard({
grid,
choices,
hint,
activeAttributes,
onSubmit,
disabled = false,
}: PatternBoardProps) {
const [selectedIndex, setSelectedIndex] = useState<number | null>(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 (
<div className="space-y-6">
{/* Header: attributes badge + hint toggle */}
<div className="flex items-center justify-between">
<span className="text-xs font-mono uppercase tracking-wider text-violet-400/70">
Pattern Matrix · {activeAttributes.join(", ")}
</span>
<button
onClick={() => setShowHint((h) => !h)}
className="text-xs text-violet-400/50 hover:text-violet-400 transition-colors"
disabled={disabled}
>
{showHint ? "Hide Hint" : "💡 Hint"}
</button>
</div>
{/* Hint */}
{showHint && (
<motion.div
initial={{ opacity: 0, height: 0 }}
animate={{ opacity: 1, height: "auto" }}
className="text-sm text-violet-300/80 bg-violet-500/10 rounded-lg px-4 py-2 border border-violet-500/20"
>
{hint}
</motion.div>
)}
{/* 3×3 Grid */}
<div className="flex justify-center">
<div className="grid grid-cols-3 gap-2">
{grid.map((cell, idx) => (
<motion.div
key={idx}
initial={{ scale: 0.8, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
transition={{ delay: idx * 0.04 }}
className={`w-20 h-20 sm:w-24 sm:h-24 flex items-center justify-center rounded-lg border
${
cell === null
? "border-2 border-dashed border-violet-400/40 bg-violet-500/10"
: "border-white/10 bg-white/5"
}`}
>
{cell ? (
<ShapeIcon cell={cell} size={48} />
) : (
<span className="text-2xl text-violet-400/50">?</span>
)}
</motion.div>
))}
</div>
</div>
{/* Choices */}
<div className="space-y-2">
<p className="text-xs text-center text-white/40 uppercase tracking-wider">
Select the missing piece
</p>
<div className="flex flex-wrap justify-center gap-2">
{choices.map((choice, idx) => (
<motion.button
key={idx}
initial={{ scale: 0.8, opacity: 0 }}
animate={{ scale: 1, opacity: 1 }}
transition={{ delay: 0.3 + idx * 0.05 }}
onClick={() => 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`}
>
<ShapeIcon cell={choice} size={40} />
</motion.button>
))}
</div>
</div>
{/* Submit */}
<div className="flex justify-center">
<button
onClick={handleSubmit}
disabled={disabled || selectedIndex === null}
className="px-6 py-2 rounded-lg font-medium transition-all
bg-violet-600 hover:bg-violet-500 text-white
disabled:opacity-40 disabled:cursor-not-allowed
active:scale-95"
>
Check Answer
</button>
</div>
</div>
);
}