"use client"; import { useState } from "react"; import Link from "next/link"; import { useStudentWorkspace } from "./StudentWorkspaceProvider"; type Question = { id: string; question: string; answer: string }; export function ChapterPractice({ chapterId, title, subject, questions }: { chapterId: string; title: string; subject: string; questions: Question[]; }) { const { updateWorkspace, status } = useStudentWorkspace(); const [index, setIndex] = useState(0); const [drafts, setDrafts] = useState>({}); const [revealed, setRevealed] = useState>({}); const [saving, setSaving] = useState(false); const [message, setMessage] = useState(null); const [error, setError] = useState(null); const question = questions[index]; if (!question) return

Practice questions for this chapter are being prepared. You can study the notes and formulas above.

; const draft = drafts[question.id] ?? ""; const shown = revealed[question.id] ?? false; async function saveAttempt() { if (saving || !draft.trim()) return; setSaving(true); setError(null); setMessage(null); const now = new Date().toISOString(); const id = `chapter-practice:${chapterId}:${question.id}`; try { await updateWorkspace((current) => { const existing = current.notes.find((note) => note.id === id); const note = { id, title: `${title} · Practice ${index + 1}`, body: `Question\n${question.question}\n\nMy answer\n${draft.trim()}\n\nReference answer\n${question.answer}\n\nSelf-check practice. Not automatically graded.`, subject, topic: title, source_id: null, source_label: "DocDoe chapter sample practice", favorite: existing?.favorite ?? false, created_at: existing?.created_at ?? now, updated_at: now, }; return { ...current, notes: [note, ...current.notes.filter((item) => item.id !== id)] }; }); setMessage("Attempt saved to Notes. Saving again updates this question's note."); } catch { setError("Could not sync this attempt. Your answer is still here; retry when connected."); } finally { setSaving(false); } } function move(next: number) { setIndex(next); setMessage(null); setError(null); } return

Question {index + 1} of {questions.length}

Self-check · not graded

{question.question}