import { ArrowDown, ArrowUp, BookOpen, Check, ChevronRight, ClipboardList, Copy, Download, FolderInput, History, Layers, ListPlus, MoreHorizontal, Pencil, Play, Plus, RotateCcw, Search, Share2, Sparkles, Stamp, Timer, Trash2, WandSparkles } from 'lucide-react'; import { useEffect, useMemo, useRef, useState } from 'react'; import { Link, useNavigate, useParams, useSearchParams } from 'react-router'; import { toast } from 'sonner'; import { blankQuestion, letterGrade, optionLetter, QUESTION_TYPE_LABEL, questionToDraft, validateDraftQuestion, type DraftCard, type DraftQuestion, type Question } from '@shared/content'; import { parseImport } from '@shared/importer'; import { MoveDialog, RenameDialog, ShareDialog } from '@/features/dialogs'; import { QuestionEditor } from '@/features/editors'; import { GenerateDialog, type GeneratePreset } from '@/features/GenerateDialog'; import { useStartSession } from '@/features/study'; import { api, errorMessage } from '@/lib/api'; import { useAssistantContext } from '@/lib/assistantStore'; import { useAiStatus, useQuiz, useRefresh } from '@/lib/queries'; import { cx, downloadFile, formatDateTime, formatMinutes, gradeTone, pct, plural, timeAgo, useDocumentTitle, useLocalStorage } from '@/lib/util'; import { Markdown } from '@/ui/Markdown'; import { SortableList } from '@/ui/Sortable'; import { Badge, Button, Callout, ClassDot, Dialog, EmptyState, ErrorState, Field, IconButton, Input, Menu, MenuItem, MenuSeparator, Segmented, Skeleton, Switch, Textarea, useConfirm } from '@/ui'; interface StartPrefs { mode: 'practice' | 'exam'; shuffleQuestions: boolean; shuffleOptions: boolean; } /** Session options (mode, length, shuffling, exam time limit), shared by the start screen and the start dialog. */ function useStartOptions(quizId: string, total: number, active: boolean) { const { start, starting } = useStartSession(); const [prefs, setPrefs] = useLocalStorage('sd.startPrefs', { mode: 'practice', shuffleQuestions: true, shuffleOptions: true }); const [count, setCount] = useState(total); const [timed, setTimed] = useState(false); const [minutes, setMinutes] = useState(30); useEffect(() => { if (active) { setCount(total); setMinutes(Math.max(5, Math.round(total * 1.2))); setTimed(false); } }, [active, total]); const go = () => start({ mode: prefs.mode, source: { type: 'quiz', quizId }, shuffleQuestions: prefs.shuffleQuestions, shuffleOptions: prefs.shuffleOptions, // An exam is always the whole quiz. count: prefs.mode === 'practice' && count < total ? count : undefined, // A time limit is an exam option only; practice sessions are never timed. timeLimitMin: prefs.mode === 'exam' && timed ? minutes : null, }); return { prefs, setPrefs, count, setCount, timed, setTimed, minutes, setMinutes, go, starting, total }; } function StartOptionsFields({ o }: { o: ReturnType }) { const { prefs, setPrefs, count, setCount, timed, setTimed, minutes, setMinutes, total } = o; return (
setPrefs((p) => ({ ...p, mode }))} options={[ { value: 'practice', label: 'Practice' }, { value: 'exam', label: 'Exam' }, ]} className="w-full" /> {prefs.mode === 'practice' && ( setCount(Number(e.target.value))} className="mt-1 w-full accent-[var(--accent)]" disabled={total <= 1} /> )}
setPrefs((p) => ({ ...p, shuffleQuestions: v }))} label="Shuffle questions" /> setPrefs((p) => ({ ...p, shuffleOptions: v }))} label="Shuffle answer choices" /> {prefs.mode === 'exam' && ( <> {timed && (
setMinutes(Math.max(1, Math.min(600, Number(e.target.value) || 1)))} className="w-24" aria-label="Minutes" /> minutes
)} )}
); } export function StartSessionDialog({ open, onClose, quizId, total, title }: { open: boolean; onClose: () => void; quizId: string; total: number; title: string }) { const o = useStartOptions(quizId, total, open); return ( !v && onClose()} title="Start a session" description={title} footer={ <> } > ); } /** The quiz start screen: how to take it, without showing any questions or answers. */ function StartPanel({ quizId, total, resume, onShowQuestions }: { quizId: string; total: number; resume: { id: string; answeredCount: number; questionCount: number } | null; onShowQuestions: () => void }) { const o = useStartOptions(quizId, total, true); if (total === 0) { return (
} title="No questions yet" action={}> Add questions to this quiz before taking it.
); } return (
{resume && ( } className="mb-5" title="You have a session in progress" action={ } > Pick up where you left off, or start a new session below. )}
); } /** Paste several questions (or cards) as text or JSON and add them to an existing item. */ export function BulkAddDialog({ open, onClose, kind, onAdd }: { open: boolean; onClose: () => void; kind: 'quiz' | 'deck'; onAdd: (items: unknown[]) => Promise }) { const [text, setText] = useState(''); const [busy, setBusy] = useState(false); useEffect(() => { if (open) setText(''); }, [open]); const parsed = useMemo(() => (text.trim() ? parseImport(text, { prefer: kind }) : null), [text, kind]); const all: (DraftQuestion | DraftCard)[] = (parsed?.items ?? []).flatMap((it): (DraftQuestion | DraftCard)[] => (kind === 'quiz' && it.kind === 'quiz' ? it.questions : kind === 'deck' && it.kind === 'deck' ? it.cards : [])); const valid = all.filter((x) => ('stem' in x ? validateDraftQuestion(x, 0).every((i) => i.level !== 'error') : Boolean(x.front.trim() && x.back.trim()))); const noun = kind === 'quiz' ? 'question' : 'card'; return ( !o && onClose()} title={`Add several ${noun}s`} size="lg" footer={ <> {parsed ? (all.length === 0 ? `No ${noun}s recognised yet` : `${valid.length} ready${all.length > valid.length ? `, ${all.length - valid.length} with problems will be skipped` : ''}`) : ''} } >