import { useState, useEffect, useRef } from 'react' import styles from './ExamStartModal.module.css' export default function ExamStartModal({ bundle, onStart, onCancel }) { const totalScenarios = bundle?.scenario_ids?.length || 0 const [minutes, setMinutes] = useState(bundle?.exam_minutes || 120) const [scenarioCount, setScenarioCount] = useState(totalScenarios) const inputRef = useRef(null) // Keep scenarioCount in sync if bundle changes useEffect(() => { setScenarioCount(bundle?.scenario_ids?.length || 0) }, [bundle]) useEffect(() => { // Focus duration input on open const t = setTimeout(() => inputRef.current?.select(), 60) return () => clearTimeout(t) }, []) if (!bundle) return null const numMinutes = Number(minutes) const numScenarios = Number(scenarioCount) const isMinutesValid = numMinutes >= 5 && numMinutes <= 300 const isScenariosValid = numScenarios >= 1 && numScenarios <= totalScenarios const isValid = isMinutesValid && isScenariosValid const handleStart = () => { if (!isValid) return onStart(numMinutes, numScenarios) } const presets = [ { label: '30 min', value: 30 }, { label: '60 min', value: 60 }, { label: '90 min', value: 90 }, { label: '120 min', value: 120 }, ] return (
e.target === e.currentTarget && onCancel()}>
{bundle.icon}
Start Exam
{bundle.name}
๐Ÿ“‹ {totalScenarios} scenarios ยท Recommended: {bundle.exam_minutes} min
{/* Duration field */}
{presets.map(p => ( ))}
setMinutes(e.target.value)} onKeyDown={e => e.key === 'Enter' && handleStart()} /> minutes
{!isMinutesValid ? ( โš  Duration must be between 5 and 300 minutes ) : numMinutes < 60 ? 'โšก Speed run mode' : numMinutes <= 120 ? '๐ŸŽฏ Realistic exam timing' : '๐Ÿง˜ Relaxed practice pace'}
{/* Scenario count field */}
setScenarioCount(Number(e.target.value))} style={{ '--bcolor': bundle.color }} /> setScenarioCount(e.target.value)} onKeyDown={e => e.key === 'Enter' && handleStart()} />
{!isScenariosValid ? ( โš  Must be between 1 and {totalScenarios} ) : numScenarios === totalScenarios ? `๐Ÿ“š Full exam โ€” all ${totalScenarios} scenarios` : `๐ŸŽฏ ${numScenarios} randomly selected scenario${numScenarios > 1 ? 's' : ''}`}
) }