File size: 8,924 Bytes
9580dce | 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | "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>
);
}
|