import { useEffect, useState } from "react";
import { useNavigate, useParams, Link } from "react-router-dom";
import {
ArrowLeft, House, Plus, Link2, Unlink, Pencil,
ChevronUp, ChevronDown, Clock, ClipboardList, PlusCircle,
LayoutTemplate, BookOpen,
} from "lucide-react";
import { useLibrary } from "@/contexts/AdminLibraryContext";
import { PageMeta } from "@/contexts/MetadataContext";
import AppBreadcrumb from "@/components/generic/Breadcrumb/AppBreadcrumb";
import AttachLessonsDialog from "../../../components/library/AttachLessonsDialog";
import AttachCoursesDialog from "../../../components/library/AttachCoursesDialog";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Spinner } from "@/components/ui/spinner";
import { formatDuration } from "@/utils/timestamp.util";
export default function ViewLibraryUnit() {
const navigate = useNavigate();
const { unitId } = useParams();
const {
fetchUnit, unit, loading,
attachLessonsToUnit, detachLessonFromUnit, detachUnitFromCourse, attachUnitToCourses, reorderUnitLessons,
} = useLibrary();
const [initializing, setInitializing] = useState(true);
const [attachOpen, setAttachOpen] = useState(false);
const [attachCourseOpen, setAttachCourseOpen] = useState(false);
const [descExpanded, setDescExpanded] = useState(false);
useEffect(() => {
(async () => {
await fetchUnit(unitId);
setInitializing(false);
})();
}, [unitId]);
const items = [
{ label: "Home", icon: , to: "/admin" },
{ label: "Units", to: "/admin/units" },
{ label: unit?.title ?? "..." },
];
const lessons = unit?.lessons ?? [];
const courses = unit?.courses ?? [];
const moveLesson = async (index, direction) => {
const target = index + direction;
if (target < 0 || target >= lessons.length) return;
const reordered = [...lessons];
[reordered[index], reordered[target]] = [reordered[target], reordered[index]];
await reorderUnitLessons(unitId, reordered.map((l) => l.lesson_id));
fetchUnit(unitId);
};
const handleDetach = async (lessonId) => {
await detachLessonFromUnit(unitId, lessonId);
fetchUnit(unitId);
};
const handleDetachCourse = async (courseId) => {
await detachUnitFromCourse(unitId, courseId);
fetchUnit(unitId);
};
const handleAttachCourses = async (courseIds) => {
await attachUnitToCourses(unitId, courseIds);
fetchUnit(unitId);
};
const handleAttach = async (lessonIds) => {
await attachLessonsToUnit(unitId, lessonIds);
fetchUnit(unitId);
};
if (initializing) {
return (
);
}
return (
{/* ── Unit header ── */}
{unit?.title}
{unit?.description && (
{unit.description}
)}
{courses.map((c) => (
))}
{unit?.quiz ? (
) : (
)}
{/* Stats row */}
Duration
{formatDuration(unit?.duration_seconds ?? 0)}
Quiz
{unit?.quiz ? unit.quiz.title || "Unit Quiz" : "None"}
Used In
{courses.length > 0 ? `${courses.length} course${courses.length === 1 ? "" : "s"}` : "Standalone"}
Subscription
{unit?.subscription ?? "No subscription gate"}
{/* Attached courses */}
{courses.length > 0 && (
Courses:
{courses.map((c) => (
{c.title}
))}
)}
{/* ── Lessons manager ── */}
Lessons
It can be standalone too attach existing ones or create new.
{lessons.length === 0 ? (
No lessons attached yet — attach one from the library or create a new one.
) : (
{lessons.map((l, i) => (
{i + 1}.
{l.title}
{l.description && (
{l.description}
)}
{formatDuration(l.duration_seconds ?? 0)}
))}
)}
l.lesson_id)}
onAttach={handleAttach}
loading={loading}
/>
c.course_id)}
onAttach={handleAttachCourses}
loading={loading}
/>
);
}