"use client"; import { useCallback, useEffect, useMemo, useState } from "react"; import Link from "next/link"; import { BookOpenCheck, Clapperboard, FileText, Layers3, LoaderCircle, RotateCw, UploadCloud, } from "lucide-react"; import { listDocuments } from "@/lib/api/documents"; import type { ApiDocument } from "@/lib/api/types"; import { buildDemoSeedDocuments } from "@/lib/demo-seed-documents"; import { getPublicDemoMode } from "@/lib/env"; import { buildStudyIntentUrl, type StudyIntent } from "@/lib/study-intents"; import { cn } from "@/lib/utils"; function sourceStatusLabel(status: string | null | undefined) { if (status === "ready" || status === "complete") return "Ready"; if (status === "processing" || status === "uploaded" || status === "running") return "Reading"; if (status === "failed") return "Needs clearer file"; return "Saved"; } function studyIntentHref(doc: ApiDocument, intent: StudyIntent, prompt: string) { return buildStudyIntentUrl({ intent, sourceId: doc.id, prompt, run: true, }); } export function FirstHourShell() { const [documents, setDocuments] = useState([]); const [selectedId, setSelectedId] = useState(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const loadSources = useCallback(async () => { setIsLoading(true); setError(null); try { let docs = await listDocuments(); if (getPublicDemoMode() !== "live" && docs.length === 0) { docs = buildDemoSeedDocuments(); } setDocuments(docs); setSelectedId((current) => current ?? docs.find((doc) => doc.status === "ready")?.id ?? docs[0]?.id ?? null); } catch { if (getPublicDemoMode() !== "live") { const docs = buildDemoSeedDocuments(); setDocuments(docs); setSelectedId(docs[0]?.id ?? null); } else { setError("DocDoe could not load your sources. Upload still works, and you can retry this panel after the connection recovers."); } } finally { setIsLoading(false); } }, []); useEffect(() => { const timeoutId = window.setTimeout(() => { void loadSources(); }, 0); return () => window.clearTimeout(timeoutId); }, [loadSources]); const readyDocuments = useMemo( () => documents.filter((doc) => doc.status === "ready"), [documents], ); const selectedDocument = documents.find((doc) => doc.id === selectedId) ?? readyDocuments[0] ?? documents[0] ?? null; const selectedIsReady = selectedDocument?.status === "ready"; const kitActions = selectedDocument ? [ { label: "Teach source", icon: BookOpenCheck, href: studyIntentHref(selectedDocument, "explain_source", `Explain ${selectedDocument.title} in simple exam language`), disabled: !selectedIsReady, }, { label: "Scoring notes", icon: FileText, href: studyIntentHref(selectedDocument, "generate_notes", `Make exam-ready notes from ${selectedDocument.title}`), disabled: !selectedIsReady, }, { label: "Quiz", icon: Layers3, href: studyIntentHref(selectedDocument, "generate_quiz", `Make a quiz from ${selectedDocument.title}`), disabled: !selectedIsReady, }, { label: "Flashcards", icon: Layers3, href: studyIntentHref(selectedDocument, "generate_flashcards", `Make flashcards from ${selectedDocument.title}`), disabled: !selectedIsReady, }, { label: "Study video plan", icon: Clapperboard, href: `/video-generator?source_id=${encodeURIComponent(selectedDocument.id)}`, disabled: !selectedIsReady, }, ] : []; return (

Study Generator

Upload your material. Generate a complete study kit.

Video, quiz, flashcards, notes, and questions from one upload. Pick a ready source below or add fresh material first.

Upload material Open sources
{error ? (

{error}

) : null}

Choose source

The selected source is passed into every study-kit action.

{isLoading ? : null}
{isLoading ? (
{Array.from({ length: 3 }).map((_, index) => (
))}
) : documents.length > 0 ? (
{documents.slice(0, 8).map((doc) => { const selected = doc.id === selectedDocument?.id; return ( ); })}
) : (

No source uploaded yet

Upload a selectable-text PDF or TXT export of your notes, chapter, syllabus, or question paper to unlock the study kit.

Upload first material
)}

Generate study kit

{selectedDocument ? `Using: ${selectedDocument.title || "selected source"}` : "Upload or choose a source first."}

{selectedDocument && !selectedIsReady ? (
This source is still being read or needs a clearer upload. The original source stays visible; retry these actions once it is ready.
) : null}
{kitActions.map((action) => { const Icon = action.icon; const className = cn( "inline-flex min-h-14 items-center justify-center gap-2 rounded-lg border px-4 text-sm font-semibold transition", action.disabled ? "pointer-events-none border-white/[0.07] bg-white/[0.02] text-white/30" : "border-white/[0.12] bg-white/[0.045] text-white/78 hover:border-white/25 hover:bg-white/[0.075] hover:text-white", ); return action.disabled ? ( {action.label} ) : ( {action.label} ); })}
{!selectedDocument ? (
Study Generator is ready as soon as you add material. No fake generated kit is shown before a real source exists.
) : null}
); }