import { useEffect, useRef, useState } from 'react' import type { Pregunta, Categoria } from '../../types' interface QuestionModalProps { pregunta: Pregunta categoria: Categoria mostrarFeedback: boolean feedbackCorrecto: boolean onResponder: (indice: number | null) => void onCerrar: () => void } const LETRAS = ['A', 'B', 'C', 'D'] const TIEMPO_LIMITE = 30 export default function QuestionModal({ pregunta, categoria, mostrarFeedback, feedbackCorrecto, onResponder, onCerrar }: QuestionModalProps) { const [tiempo, setTiempo] = useState(TIEMPO_LIMITE) const intervalRef = useRef | null>(null) const tiempoAgotadoRef = useRef(false) useEffect(() => { if (mostrarFeedback) { if (intervalRef.current) clearInterval(intervalRef.current) return } setTiempo(TIEMPO_LIMITE) tiempoAgotadoRef.current = false intervalRef.current = setInterval(() => { setTiempo((prev) => { if (prev <= 1) { clearInterval(intervalRef.current!) return 0 } return prev - 1 }) }, 1000) return () => { if (intervalRef.current) clearInterval(intervalRef.current) } }, [pregunta.id, mostrarFeedback]) useEffect(() => { if (tiempo === 0 && !mostrarFeedback && !tiempoAgotadoRef.current) { tiempoAgotadoRef.current = true onResponder(null) } }, [tiempo, mostrarFeedback]) const porcentaje = (tiempo / TIEMPO_LIMITE) * 100 const colorBarra = porcentaje > 50 ? '#22c55e' : porcentaje > 25 ? '#f97316' : '#ef4444' return (
{!mostrarFeedback && ( <> {/* Header con categoría */}
{categoria.emoji}

{categoria.nombre}

{pregunta.pregunta}

{/* Temporizador */}
Tiempo restante {tiempo}s
{/* Opciones */}
{pregunta.opciones.map((opcion, i) => ( ))}
)} {/* Feedback */} {mostrarFeedback && (
{feedbackCorrecto ? '🎉' : '❌'}

{feedbackCorrecto ? '¡Correcto!' : '¡Incorrecto!'}

{feedbackCorrecto ?

{pregunta.retroalimentacion}

:

{tiempo === 0 ? 'Se agotó el tiempo.' : 'Respuesta incorrecta.'}

}
)}
) }