"use client"; import { useState, useRef, useEffect, useCallback } from "react"; import { motion } from "framer-motion"; interface SequenceBoardProps { sequence: (number | null)[]; hiddenIndices: number[]; hint: string; type: string; onSubmit: (answers: number[]) => void; disabled?: boolean; } const TYPE_LABELS: Record = { arithmetic: "Arithmetic", geometric: "Geometric", fibonacci: "Fibonacci", triangular: "Triangular", square: "Square Numbers", prime: "Primes", quadratic: "Quadratic", alternating: "Alternating", }; export function SequenceBoard({ sequence, hiddenIndices, hint, type, onSubmit, disabled = false, }: SequenceBoardProps) { const [answers, setAnswers] = useState>({}); const [showHint, setShowHint] = useState(false); const inputRefs = useRef>({}); // Focus first input on mount useEffect(() => { const firstIdx = hiddenIndices[0]; if (firstIdx !== undefined) { inputRefs.current[firstIdx]?.focus(); } }, [hiddenIndices]); const handleChange = useCallback( (index: number, value: string) => { // Allow negative numbers and digits only if (value !== "" && value !== "-" && !/^-?\d+$/.test(value)) return; setAnswers((prev) => ({ ...prev, [index]: value })); }, [], ); const handleKeyDown = useCallback( (e: React.KeyboardEvent, index: number) => { if (e.key === "Enter" || e.key === "Tab") { e.preventDefault(); const currentPos = hiddenIndices.indexOf(index); const nextIdx = hiddenIndices[currentPos + 1]; if (nextIdx !== undefined) { inputRefs.current[nextIdx]?.focus(); } } }, [hiddenIndices], ); const handleSubmit = useCallback(() => { const playerAnswers = hiddenIndices.map((idx) => { const val = answers[idx]; return val ? parseInt(val, 10) : NaN; }); if (playerAnswers.some((v) => isNaN(v))) return; onSubmit(playerAnswers); }, [answers, hiddenIndices, onSubmit]); const allFilled = hiddenIndices.every((idx) => { const val = answers[idx]; return val !== undefined && val !== "" && val !== "-" && !isNaN(parseInt(val, 10)); }); return (
{/* Sequence type badge */}
{TYPE_LABELS[type] || type} Sequence
{/* Hint */} {showHint && ( {hint} )} {/* Sequence display */}
{sequence.map((val, idx) => { const isHidden = hiddenIndices.includes(idx); const isLast = idx === sequence.length - 1; return (
{isHidden ? ( { inputRefs.current[idx] = el; }} type="text" inputMode="numeric" value={answers[idx] ?? ""} onChange={(e) => handleChange(idx, e.target.value)} onKeyDown={(e) => handleKeyDown(e, idx)} disabled={disabled} className="w-16 h-12 text-center text-lg font-mono font-bold bg-violet-500/20 border-2 border-violet-400/40 rounded-lg text-white placeholder-violet-400/30 focus:border-violet-400 focus:ring-2 focus:ring-violet-400/20 focus:outline-none transition-all disabled:opacity-50 disabled:cursor-not-allowed" placeholder="?" /> ) : ( {val} )} {/* Separator between terms (except last) */} {!isLast && ( , )}
); })} {/* Ellipsis to suggest continuation */} …
{/* Submit */}
); }