"use client"; import { useState, type RefObject } from "react"; import { useTranslation } from "react-i18next"; import { AlertCircle, ArrowLeft, ArrowRight, Check, Loader2, Sparkles, X, } from "lucide-react"; import { useModalDialog } from "@/hooks/useModalDialog"; import { hydrateTopicSource, toggleSourceSelection, useTopicSourceLibrary, } from "@/hooks/useTopicSourceLibrary"; import { createMasteryTopic, generateMasteryTopicDraft, type CreateTopicInput, type MasteryTopic, type TopicDraft, type TopicSourceInput, } from "@/lib/learning-api"; import type { Translate } from "./format"; import { CoverageNotice } from "./CoverageNotice"; import { RouteDraftEditor } from "./RouteDraftEditor"; import { isRouteDraftValid } from "./route-draft"; import { GoalStep, SourcesStep } from "./TopicWizardSteps"; export function CreateTopicWizard({ onClose, onCreated, returnFocusRef, }: { onClose: () => void; onCreated: (topic: MasteryTopic) => void; returnFocusRef: RefObject; }) { const { t } = useTranslation(); const [step, setStep] = useState(1); const [name, setName] = useState(""); const [goal, setGoal] = useState(""); const [emoji, setEmoji] = useState("đź§­"); const { library, loading: libraryLoading, candidates, files, loadKnowledgeBaseFiles, } = useTopicSourceLibrary(t); const [selected, setSelected] = useState>(new Set()); const [draft, setDraft] = useState(null); const [sources, setSources] = useState([]); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const dialogRef = useModalDialog(onClose, busy, returnFocusRef); const toggleSource = (key: string) => { setSelected((previous) => toggleSourceSelection(previous, key, candidates)); }; const generate = async (mustCover: string[] = []) => { setBusy(true); setError(null); try { const chosen = candidates.filter((candidate) => selected.has(candidate.key), ); const hydrated = await Promise.all(chosen.map(hydrateTopicSource)); const nextSources: TopicSourceInput[] = [ { kind: "goal", label: t("Learning goal"), excerpt: goal.trim(), }, ...hydrated, ]; const nextDraft = await generateMasteryTopicDraft({ name: name.trim(), goal: goal.trim(), sources: nextSources, ...(mustCover.length ? { must_cover: mustCover } : {}), }); setSources(nextDraft.sources ?? nextSources); setDraft(nextDraft); setStep(3); } catch (reason) { setError( reason instanceof Error ? reason.message : t("Could not generate the outline. Please retry."), ); } finally { setBusy(false); } }; const confirm = async () => { if (!draft || !isRouteDraftValid(draft)) return; setBusy(true); setError(null); try { const payload: CreateTopicInput = { name: name.trim(), goal: goal.trim(), description: draft.description.trim(), emoji, sources, modules: draft.modules, }; onCreated(await createMasteryTopic(payload)); } catch (reason) { setError( reason instanceof Error ? reason.message : t("Creation failed. Please retry."), ); } finally { setBusy(false); } }; return (
{ if (event.target === event.currentTarget && !busy) onClose(); }} >
{t("New topic")}

{t("Plan the modules")}

    {[t("Goal"), t("Materials"), t("Outline")].map((label, index) => { const number = index + 1; const active = number === step; const done = number < step; return (
  1. {done ? : number} {label}
  2. ); })}
{step === 1 && ( )} {step === 2 && ( )} {step === 3 && draft && ( <> void generate(documents)} /> )} {error && (
{error}
)}
); }