mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
@@ -0,0 +1,237 @@
|
||||
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 { 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, reorderUnitLessons,
|
||||
} = useLibrary();
|
||||
|
||||
const [initializing, setInitializing] = useState(true);
|
||||
const [attachOpen, setAttachOpen] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
await fetchUnit(unitId);
|
||||
setInitializing(false);
|
||||
})();
|
||||
}, [unitId]);
|
||||
|
||||
const items = [
|
||||
{ label: "Home", icon: <House className="size-4" />, 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 handleAttach = async (lessonIds) => {
|
||||
await attachLessonsToUnit(unitId, lessonIds);
|
||||
fetchUnit(unitId);
|
||||
};
|
||||
|
||||
if (initializing) {
|
||||
return (
|
||||
<div className="flex items-center justify-center h-64">
|
||||
<Spinner className="h-6 w-6" />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<section className="bg-muted h-full">
|
||||
<PageMeta title={unit ? `${unit.title} - Units - STARR` : undefined} />
|
||||
<div className="lg:container lg:mx-auto lg:px-0 flex flex-col items-start px-4">
|
||||
<div className="flex flex-col gap-2 my-6">
|
||||
<AppBreadcrumb items={items} />
|
||||
</div>
|
||||
|
||||
<div className="w-full space-y-6 pb-10">
|
||||
|
||||
{/* ── Unit header ── */}
|
||||
<div className="bg-card rounded-xl border p-6 space-y-4">
|
||||
<div className="flex items-start justify-between gap-4">
|
||||
<div>
|
||||
<h6 className="text-xs tracking-widest mb-1">STANDALONE UNIT</h6>
|
||||
<h1 className="text-xl font-semibold">{unit?.title}</h1>
|
||||
{unit?.description && (
|
||||
<p className="text-sm text-muted-foreground mt-0.5">{unit.description}</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex gap-2 shrink-0">
|
||||
<Button size="sm" variant="outline" onClick={() => navigate(`/admin/units/${unitId}/edit`)}>
|
||||
<Pencil className="h-3.5 w-3.5 mr-1.5" /> Edit
|
||||
</Button>
|
||||
{unit?.quiz ? (
|
||||
<Button size="sm" variant="outline" onClick={() => navigate(`/admin/units/${unitId}/quiz/view`)}>
|
||||
<ClipboardList className="h-3.5 w-3.5 mr-1.5" /> View Quiz
|
||||
</Button>
|
||||
) : (
|
||||
<Button size="sm" variant="outline" onClick={() => navigate(`/admin/units/${unitId}/quiz/edit`)}>
|
||||
<PlusCircle className="h-3.5 w-3.5 mr-1.5" /> Create Quiz
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Stats row */}
|
||||
<div className="grid grid-cols-2 lg:grid-cols-4 gap-3">
|
||||
<div className="bg-muted/60 rounded-lg p-3 space-y-1">
|
||||
<p className="text-xs text-muted-foreground uppercase tracking-wide font-medium">Lessons</p>
|
||||
<p className="font-semibold text-sm">{lessons.length}</p>
|
||||
</div>
|
||||
<div className="bg-muted/60 rounded-lg p-3 space-y-1">
|
||||
<p className="text-xs text-muted-foreground uppercase tracking-wide font-medium">Duration</p>
|
||||
<p className="font-semibold text-sm">{formatDuration(unit?.duration_seconds ?? 0)}</p>
|
||||
</div>
|
||||
<div className="bg-muted/60 rounded-lg p-3 space-y-1">
|
||||
<p className="text-xs text-muted-foreground uppercase tracking-wide font-medium">Quiz</p>
|
||||
<p className="font-semibold text-sm">{unit?.quiz ? unit.quiz.title || "Unit Quiz" : "None"}</p>
|
||||
</div>
|
||||
<div className="bg-muted/60 rounded-lg p-3 space-y-1">
|
||||
<p className="text-xs text-muted-foreground uppercase tracking-wide font-medium">Used In</p>
|
||||
<p className="font-semibold text-sm">
|
||||
{courses.length > 0 ? `${courses.length} course${courses.length === 1 ? "" : "s"}` : "Standalone"}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Attached courses */}
|
||||
{courses.length > 0 && (
|
||||
<div className="flex flex-wrap items-center gap-2 pt-1">
|
||||
<span className="text-xs text-muted-foreground uppercase tracking-wide font-medium">Courses:</span>
|
||||
{courses.map((c) => (
|
||||
<Link key={c.course_id} to={`/admin/courses/${c.course_id}/view`}>
|
||||
<Badge variant="secondary" className="hover:bg-muted cursor-pointer">
|
||||
<BookOpen className="h-3 w-3 mr-1" /> {c.title}
|
||||
</Badge>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* ── Lessons manager ── */}
|
||||
<div className="bg-card rounded-xl border">
|
||||
<div className="flex items-center justify-between p-4 border-b">
|
||||
<div>
|
||||
<h2 className="font-semibold">Lessons</h2>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Lessons are standalone too — attach existing ones or create new. Order here is this unit's order.
|
||||
</p>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<Button size="sm" variant="outline" onClick={() => setAttachOpen(true)}>
|
||||
<Link2 className="h-3.5 w-3.5 mr-1.5" /> Attach Existing
|
||||
</Button>
|
||||
<Button size="sm" onClick={() => navigate(`/admin/lessons/add?unit_id=${unitId}`)}>
|
||||
<Plus className="h-3.5 w-3.5 mr-1.5" /> New Lesson
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{lessons.length === 0 ? (
|
||||
<p className="text-sm text-muted-foreground text-center py-10">
|
||||
No lessons attached yet — attach one from the library or create a new one.
|
||||
</p>
|
||||
) : (
|
||||
<div className="divide-y">
|
||||
{lessons.map((l, i) => (
|
||||
<div key={l.lesson_id} className="flex items-center gap-3 px-4 py-3">
|
||||
<span className="text-xs font-mono text-muted-foreground w-6 text-right shrink-0">
|
||||
{i + 1}.
|
||||
</span>
|
||||
<div className="flex flex-col gap-0.5 shrink-0">
|
||||
<button
|
||||
type="button"
|
||||
className="text-muted-foreground hover:text-foreground disabled:opacity-30"
|
||||
disabled={i === 0 || loading}
|
||||
onClick={() => moveLesson(i, -1)}
|
||||
aria-label="Move up"
|
||||
>
|
||||
<ChevronUp className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="text-muted-foreground hover:text-foreground disabled:opacity-30"
|
||||
disabled={i === lessons.length - 1 || loading}
|
||||
onClick={() => moveLesson(i, 1)}
|
||||
aria-label="Move down"
|
||||
>
|
||||
<ChevronDown className="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</div>
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="text-sm font-medium truncate">{l.title}</p>
|
||||
{l.description && (
|
||||
<p className="text-xs text-muted-foreground truncate">{l.description}</p>
|
||||
)}
|
||||
</div>
|
||||
<Badge variant="secondary" className="text-xs shrink-0 tabular-nums">
|
||||
<Clock className="h-3 w-3 mr-1" /> {formatDuration(l.duration_seconds ?? 0)}
|
||||
</Badge>
|
||||
<Button
|
||||
size="sm" variant="ghost"
|
||||
onClick={() => navigate(`/admin/lessons/${l.lesson_id}/page`)}
|
||||
title="Page Builder"
|
||||
>
|
||||
<LayoutTemplate className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
<Button
|
||||
size="sm" variant="ghost"
|
||||
className="text-destructive hover:text-destructive"
|
||||
onClick={() => handleDetach(l.lesson_id)}
|
||||
disabled={loading}
|
||||
title="Detach from unit (lesson stays in library)"
|
||||
>
|
||||
<Unlink className="h-3.5 w-3.5" />
|
||||
</Button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<AttachLessonsDialog
|
||||
open={attachOpen}
|
||||
onOpenChange={setAttachOpen}
|
||||
attachedLessonIds={lessons.map((l) => l.lesson_id)}
|
||||
onAttach={handleAttach}
|
||||
loading={loading}
|
||||
/>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user