Download src/components/logic/sequence-board.tsx from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 6.04 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/components/logic/sequence-board.tsx
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/components/logic/sequence-board.tsx
-
curl -L -o sequence-board.tsx https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/components/logic/sequence-board.tsx
6.04 kB
| "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<string, string> = { | |
| 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<Record<number, string>>({}); | |
| const [showHint, setShowHint] = useState(false); | |
| const inputRefs = useRef<Record<number, HTMLInputElement | null>>({}); | |
| // 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 ( | |
| <div className="space-y-6"> | |
| {/* Sequence type badge */} | |
| <div className="flex items-center justify-between"> | |
| <span className="text-xs font-mono uppercase tracking-wider text-violet-400/70"> | |
| {TYPE_LABELS[type] || type} Sequence | |
| </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> | |
| )} | |
| {/* Sequence display */} | |
| <div className="flex flex-wrap items-center justify-center gap-2"> | |
| {sequence.map((val, idx) => { | |
| const isHidden = hiddenIndices.includes(idx); | |
| const isLast = idx === sequence.length - 1; | |
| return ( | |
| <div key={idx} className="flex items-center gap-2"> | |
| {isHidden ? ( | |
| <motion.div | |
| initial={{ scale: 0.8, opacity: 0 }} | |
| animate={{ scale: 1, opacity: 1 }} | |
| transition={{ delay: idx * 0.05 }} | |
| > | |
| <input | |
| ref={(el) => { | |
| 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="?" | |
| /> | |
| </motion.div> | |
| ) : ( | |
| <motion.div | |
| initial={{ scale: 0.8, opacity: 0 }} | |
| animate={{ scale: 1, opacity: 1 }} | |
| transition={{ delay: idx * 0.05 }} | |
| className="w-12 h-12 flex items-center justify-center | |
| text-lg font-mono font-semibold text-white/90 | |
| bg-white/5 rounded-lg border border-white/10" | |
| > | |
| {val} | |
| </motion.div> | |
| )} | |
| {/* Separator between terms (except last) */} | |
| {!isLast && ( | |
| <span className="text-white/20 font-mono text-lg">,</span> | |
| )} | |
| </div> | |
| ); | |
| })} | |
| {/* Ellipsis to suggest continuation */} | |
| <span className="text-white/30 font-mono text-lg ml-1">…</span> | |
| </div> | |
| {/* Submit */} | |
| <div className="flex justify-center"> | |
| <button | |
| onClick={handleSubmit} | |
| disabled={disabled || !allFilled} | |
| 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{hiddenIndices.length > 1 ? "s" : ""} | |
| </button> | |
| </div> | |
| </div> | |
| ); | |
| } | |