"use client"; import { useState, useEffect, useCallback } from "react"; import { useParams, useRouter } from "next/navigation"; import { FlashcardViewer } from "@/components/flashcards/FlashcardViewer"; import { CardManager } from "@/components/flashcards/CardManager"; import { ShareMenu } from "@/components/ui/ShareMenu"; interface Card { id: string; front: string; back: string; sortOrder: number; } interface DeckData { id: string; name: string; description: string | null; cards: Card[]; class: { slug: string; name: string }; progress: Array<{ mode: "SEQUENTIAL" | "SHUFFLED"; currentIndex: number; orderJson: string; cardResultsJson: string | null; sessionId: string; revision: number; updatedAt: string; }>; } export default function DeckStudyPage() { const params = useParams(); const router = useRouter(); const classSlug = params.classSlug as string; const deckId = params.deckId as string; const [deck, setDeck] = useState(null); const [loading, setLoading] = useState(true); const [view, setView] = useState<"study" | "manage">("study"); const [restartKey, setRestartKey] = useState(0); async function handleRestart() { if (!deck) return; const responses = await Promise.all( deck.progress.map((progress) => fetch("/api/progress", { method: "DELETE", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ contentType: "DECK", contentId: deck.id, mode: progress.mode, sessionId: progress.sessionId, }), }) ) ).catch(() => null); if (!responses || responses.some((response) => !response.ok)) { window.alert("The session could not be restarted. Your saved progress was kept."); return; } // Clear locally and force remount setDeck({ ...deck, progress: [] }); setRestartKey(k => k + 1); } const fetchDeck = useCallback(async () => { try { const res = await fetch(`/api/decks/${deckId}`); if (!res.ok) throw new Error("Not found"); const data = await res.json(); setDeck(data); } catch { router.push(`/${classSlug}/flashcards`); } finally { setLoading(false); } }, [deckId, classSlug, router]); useEffect(() => { const timer = window.setTimeout(() => void fetchDeck(), 0); return () => window.clearTimeout(timer); }, [fetchDeck]); if (loading || !deck) { return (
); } return (
{/* Header */}

Flashcard session

{deck.name}

{deck.description && (

{deck.description}

)}
{/* View toggle & Share */}
{view === "study" && ( )}
{/* Content */} {view === "study" ? ( new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime())[0] : null } /> ) : ( )}
); }