import { useState } from 'react'; import { Plus, Trash2, GripVertical, ChevronsUpDown, BookOpen, Layers, FileText, Link2, Unlink } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Card, CardContent } from '@/components/ui/card'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { Command, CommandInput, CommandList, CommandEmpty, CommandGroup, CommandItem } from '@/components/ui/command'; // ─── Prerequisite type config ────────────────────────────────────────────────── // idKey is the numeric FK field each flat list carries (course_id/unit_id/ // lesson_id) — CoursePrerequisite.ref_id is a BIGINT FK, not a uuid, so the // picker must select on the numeric id even though the list is keyed/searched // by uuid for React purposes. const REF_TYPES = [ { value: 'course', label: 'Course', icon: BookOpen, idKey: 'course_id' }, { value: 'unit', label: 'Unit', icon: Layers, idKey: 'unit_id' }, { value: 'lesson', label: 'Lesson', icon: FileText, idKey: 'lesson_id' }, ]; const TYPE_MAP = Object.fromEntries(REF_TYPES.map((t) => [t.value, t])); // ─── Course binding indicator (units/lessons may sit under 0..N courses) ────── function BindingLine({ courses = [] }) { if (!courses.length) { return ( Standalone ); } return ( {courses.map((c) => c.title).join(', ')} ); } // ─── Custom content picker — own Dialog rather than an anchored Popover ─────── function ContentPicker({ value, options, idKey, placeholder = 'Select…', dialogTitle, onSelect, renderTrigger, renderItem }) { const [open, setOpen] = useState(false); const selected = options.find((o) => String(o[idKey]) === String(value)); const searchValue = (o) => { const courseNames = (o.courses ?? []).map((c) => c.title).join(' '); return `${o.title ?? ''} ${courseNames}`.trim() || String(o[idKey]); }; return ( <> {dialogTitle ?? placeholder} No results found. {options.map((o) => ( { onSelect(o); setOpen(false); }} className="p-0" > {renderItem(o)} ))} ); } function createPrereq(type = 'course') { return { _key: crypto.randomUUID(), ref_type: type, ref_id: '', title: '' }; } // ─── CoursePrerequisiteBuilder ───────────────────────────────────────────────── // value: [{ prereq_id?, ref_type, ref_id, title? }] export default function CoursePrerequisiteBuilder({ value = [], onChange, courses = [], units = [], lessons = [], excludeCourseId }) { const [items, setItems] = useState( value.length > 0 ? value.map((v) => ({ _key: crypto.randomUUID(), ...v })) : [] ); const filteredCourses = courses.filter((c) => String(c.course_id) !== String(excludeCourseId)); const optionsFor = { course: filteredCourses, unit: units, lesson: lessons }; const emit = (next) => { setItems(next); onChange?.(next.map(({ _key, ...r }) => r)); }; const addItem = () => emit([...items, createPrereq('course')]); const removeItem = (key) => emit(items.filter((i) => i._key !== key)); const updateItem = (key, patch) => emit(items.map((i) => (i._key === key ? { ...i, ...patch } : i))); return (
{items.length === 0 && (

No prerequisites added. Learners can start this course freely.

)} {items.map((item, idx) => { const typeDef = TYPE_MAP[item.ref_type]; const Icon = typeDef?.icon ?? BookOpen; const options = optionsFor[item.ref_type] ?? []; return (
{idx + 1}
updateItem(item._key, { ref_id: o[typeDef.idKey], title: o.title })} renderTrigger={(o) => (
{item.ref_type !== 'course' && (
{o.title}
)} {item.ref_type === 'course' && ( {o.title} )}
)} renderItem={(o) => (
{o.title} {item.ref_type !== 'course' && }
)} />
); })}
); }