"use client" import { useState } from "react" import { ChevronRight, FileText, Folder, MoreHorizontal, Plus, Trash2 } from "lucide-react" import { toast } from "sonner" import { Chapter } from "../types" import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "@/shared/components/ui/collapsible" import { Button } from "@/shared/components/ui/button" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/shared/components/ui/dropdown-menu" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/shared/components/ui/alert-dialog" import { cn } from "@/shared/lib/utils" import { CreateChapterDialog } from "./create-chapter-dialog" import { deleteChapterAction } from "../actions" interface ChapterItemProps { chapter: Chapter level?: number selectedId?: string onSelect: (chapter: Chapter) => void textbookId: string } function ChapterItem({ chapter, level = 0, selectedId, onSelect, textbookId }: ChapterItemProps) { const [isOpen, setIsOpen] = useState(false) const [showCreateDialog, setShowCreateDialog] = useState(false) const [showDeleteDialog, setShowDeleteDialog] = useState(false) const [isDeleting, setIsDeleting] = useState(false) const hasChildren = chapter.children && chapter.children.length > 0 const isSelected = chapter.id === selectedId const handleDelete = async () => { setIsDeleting(true) const res = await deleteChapterAction(chapter.id, textbookId) setIsDeleting(false) if (res.success) { toast.success(res.message) setShowDeleteDialog(false) } else { toast.error(res.message) } } return (
0 && "ml-4 border-l pl-2")}>
{hasChildren ? ( ) : (
)}
onSelect(chapter)} > {hasChildren ? ( ) : ( )} {chapter.title} setShowCreateDialog(true)} > Add Subchapter setShowDeleteDialog(true)}> Delete
{hasChildren && (
{chapter.children!.map((child) => ( ))}
)} Delete chapter? This will delete this chapter and all its subchapters and linked knowledge points. Cancel {isDeleting ? "Deleting..." : "Delete"}
) } export function ChapterSidebarList({ chapters, selectedChapterId, onSelectChapter, textbookId, }: { chapters: Chapter[], selectedChapterId?: string, onSelectChapter: (chapter: Chapter) => void textbookId: string }) { return (
{chapters.map((chapter) => ( ))}
) }