// AttachLessonsDialog — pick existing library Lessons and attach them to a Unit. // Junction revamp: attaching creates unit_lessons rows; the Lessons stay standalone. // // Clicking a row previews that lesson's existing Page Builder blocks on the right, // independent of the checkbox — so admins can see what they're attaching before // committing. Preview fetches go through `api` directly rather than the shared // AdminLibraryContext `fetchLesson`, since that setter writes into a single global // `lesson` slot that other mounted pages may depend on. import { useEffect, useMemo, useState } from "react"; import { Search, Link2, Eye } from "lucide-react"; import { useLibrary } from "@/contexts/AdminLibraryContext"; import api from "@/utils/api.util"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Checkbox } from "@/components/ui/checkbox"; import { Badge } from "@/components/ui/badge"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Spinner } from "@/components/ui/spinner"; import { formatDuration } from "@/utils/timestamp.util"; import { PreviewChrome, PreviewContent } from "../courses/LessonsPreview"; export default function AttachLessonsDialog({ open, onOpenChange, attachedLessonIds = [], onAttach, loading }) { const { lessonsFlat, fetchLessonsFlat, loading: libraryLoading } = useLibrary(); const [query, setQuery] = useState(""); const [selected, setSelected] = useState([]); const [previewLessonId, setPreviewLessonId] = useState(null); const [previewLoading, setPreviewLoading] = useState(false); const [previewCache, setPreviewCache] = useState({}); useEffect(() => { if (open) { setSelected([]); setQuery(""); setPreviewLessonId(null); setPreviewCache({}); fetchLessonsFlat(); } }, [open, fetchLessonsFlat]); useEffect(() => { if (!previewLessonId || previewLessonId in previewCache) return; let cancelled = false; setPreviewLoading(true); api.get(`/admin/lessons/${previewLessonId}`) .then((resp) => { if (cancelled) return; setPreviewCache((prev) => ({ ...prev, [previewLessonId]: resp.data?.data?.data ?? null })); }) .catch(() => { if (cancelled) return; setPreviewCache((prev) => ({ ...prev, [previewLessonId]: null })); }) .finally(() => { if (!cancelled) setPreviewLoading(false); }); return () => { cancelled = true; }; }, [previewLessonId, previewCache]); const previewLesson = previewLessonId ? previewCache[previewLessonId] : null; const previewBlocks = previewLesson?.page?.blocks ?? []; const attachedSet = useMemo( () => new Set(attachedLessonIds.map(String)), [attachedLessonIds] ); const candidates = useMemo(() => { const q = query.trim().toLowerCase(); return (lessonsFlat ?? []) .filter((l) => !attachedSet.has(String(l.lesson_id))) .filter((l) => !q || l.title?.toLowerCase().includes(q)); }, [lessonsFlat, attachedSet, query]); const toggle = (lessonId) => setSelected((prev) => prev.includes(lessonId) ? prev.filter((id) => id !== lessonId) : [...prev, lessonId] ); const handleAttach = async () => { if (!selected.length) return; await onAttach(selected); onOpenChange(false); }; return ( Select Lessons Lessons live independently in the library — attaching adds them to this unit without copying. Click a lesson to preview its content.
setQuery(e.target.value)} />
{libraryLoading ? (
) : candidates.length === 0 ? (

{query ? "No lessons match your search." : "Every library lesson is already attached."}

) : (
{candidates.map((l) => (
{ toggle(l.lesson_id); setPreviewLessonId(l.lesson_id); }} className={`flex items-center gap-3 px-3 py-2.5 cursor-pointer select-none hover:bg-muted/60 ${ previewLessonId === l.lesson_id ? "bg-muted" : "" }`} > { toggle(l.lesson_id); setPreviewLessonId(l.lesson_id); }} onClick={(e) => e.stopPropagation()} />

{l.title}

{formatDuration(l.duration_seconds ?? 0)}

{Number(l.unit_count) > 0 ? ( {l.unit_count} ) : ( new )}
))}
)}
{!previewLessonId ? (

Click a lesson to preview its content.

) : previewLoading ? (
) : ( )}
); }