merge new_starr_app@qas history into apps/web
git-subtree-dir: apps/web git-subtree-mainline:eaa6d2276agit-subtree-split:459af9d46a
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
// 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 (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="sm:max-w-3xl">
|
||||
<DialogHeader>
|
||||
<DialogTitle className="flex items-center gap-2">
|
||||
<Link2 className="h-4 w-4" /> Select Lessons
|
||||
</DialogTitle>
|
||||
<DialogDescription>
|
||||
Lessons live independently in the library — attaching adds them to this unit without copying.
|
||||
Click a lesson to preview its content.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="grid gap-4 sm:grid-cols-[280px_1fr]">
|
||||
<div className="space-y-2 min-w-0">
|
||||
<div className="relative">
|
||||
<Search className="absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground" />
|
||||
<Input
|
||||
placeholder="Search lessons..."
|
||||
className="pl-8"
|
||||
value={query}
|
||||
onChange={(e) => setQuery(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<ScrollArea className="h-80 rounded-md border">
|
||||
{libraryLoading ? (
|
||||
<div className="flex items-center justify-center h-full py-10">
|
||||
<Spinner className="h-5 w-5" />
|
||||
</div>
|
||||
) : candidates.length === 0 ? (
|
||||
<p className="text-sm text-muted-foreground text-center py-10 px-3">
|
||||
{query ? "No lessons match your search." : "Every library lesson is already attached."}
|
||||
</p>
|
||||
) : (
|
||||
<div className="divide-y">
|
||||
{candidates.map((l) => (
|
||||
<div
|
||||
key={l.lesson_id}
|
||||
onClick={() => {
|
||||
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" : ""
|
||||
}`}
|
||||
>
|
||||
<Checkbox
|
||||
checked={selected.includes(l.lesson_id)}
|
||||
onCheckedChange={() => {
|
||||
toggle(l.lesson_id);
|
||||
setPreviewLessonId(l.lesson_id);
|
||||
}}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
<div className="flex-1 min-w-0">
|
||||
<p className="text-sm font-medium truncate">{l.title}</p>
|
||||
<p className="text-xs text-muted-foreground truncate">
|
||||
{formatDuration(l.duration_seconds ?? 0)}
|
||||
</p>
|
||||
</div>
|
||||
{Number(l.unit_count) > 0 ? (
|
||||
<Badge variant="secondary" className="text-xs shrink-0">
|
||||
{l.unit_count}
|
||||
</Badge>
|
||||
) : (
|
||||
<Badge variant="outline" className="text-xs shrink-0 text-muted-foreground">new</Badge>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</ScrollArea>
|
||||
</div>
|
||||
|
||||
<div className="min-w-0">
|
||||
<ScrollArea className="h-80 rounded-md border">
|
||||
<div className="p-3">
|
||||
{!previewLessonId ? (
|
||||
<div className="flex flex-col items-center justify-center gap-2 h-64 text-sm text-muted-foreground">
|
||||
<Eye className="h-6 w-6 opacity-20" />
|
||||
<p>Click a lesson to preview its content.</p>
|
||||
</div>
|
||||
) : previewLoading ? (
|
||||
<div className="flex items-center justify-center h-64">
|
||||
<Spinner className="h-5 w-5" />
|
||||
</div>
|
||||
) : (
|
||||
<PreviewChrome title={previewLesson?.title}>
|
||||
<PreviewContent
|
||||
lesson={previewLesson}
|
||||
blocks={previewBlocks}
|
||||
showHeader={false}
|
||||
empty="No content blocks yet."
|
||||
/>
|
||||
</PreviewChrome>
|
||||
)}
|
||||
</div>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => onOpenChange(false)} disabled={loading}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleAttach} disabled={loading || !selected.length}>
|
||||
{loading && <Spinner className="h-4 w-4 mr-2" />}
|
||||
Select {selected.length > 0 ? `(${selected.length})` : ""}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user