|
| 1 | +import { |
| 2 | + Collapsible, |
| 3 | + CollapsibleContent, |
| 4 | + CollapsibleTrigger, |
| 5 | +} from "@/components/ui"; |
| 6 | +import { memo, ReactNode, useState } from "react"; |
| 7 | +import { useNoteItem } from "../hooks/use-note-item"; |
| 8 | +import { cn, getNoteIcon } from "@/lib/utils"; |
| 9 | +import { ChevronRight, Plus } from "lucide-react"; |
| 10 | + |
| 11 | +export function NoteListItem({ |
| 12 | + id, |
| 13 | + children, |
| 14 | +}: { |
| 15 | + id: string; |
| 16 | + children: ReactNode[] | ReactNode; |
| 17 | +}) { |
| 18 | + return ( |
| 19 | + <> |
| 20 | + <NoteItem id={id}>{children}</NoteItem> |
| 21 | + <NoteDropZone id={id} /> |
| 22 | + </> |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +function NoteItem({ |
| 27 | + id, |
| 28 | + children, |
| 29 | +}: { |
| 30 | + id: string; |
| 31 | + children: ReactNode[] | ReactNode; |
| 32 | +}) { |
| 33 | + const [open, setOpen] = useState(false); |
| 34 | + const note = useNoteItem(id); |
| 35 | + |
| 36 | + if (!note) return null; |
| 37 | + |
| 38 | + return ( |
| 39 | + <Collapsible open={open} onOpenChange={setOpen}> |
| 40 | + <div className="group flex w-full items-center gap-2 rounded-md px-2 py-1.5 text-sm hover:bg-accent hover:text-accent-foreground"> |
| 41 | + <CollapsibleTrigger asChild> |
| 42 | + <button className="flex items-center gap-1 rounded-sm p-0.5 hover:bg-muted/50"> |
| 43 | + <ChevronRight |
| 44 | + className={cn( |
| 45 | + "size-4 transition-transform duration-200", |
| 46 | + open && "rotate-90", |
| 47 | + )} |
| 48 | + /> |
| 49 | + {getNoteIcon(note.icon, "size-4")} |
| 50 | + </button> |
| 51 | + </CollapsibleTrigger> |
| 52 | + <span className="flex-1 truncate text-left select-none"> |
| 53 | + {note.title || "Untitled"} |
| 54 | + </span> |
| 55 | + <button className="opacity-0 transition-opacity hover:bg-muted/50 group-hover:opacity-100 rounded-sm p-0.5"> |
| 56 | + <Plus className="size-4" /> |
| 57 | + </button> |
| 58 | + </div> |
| 59 | + <CollapsibleContent className="pl-2">{children}</CollapsibleContent> |
| 60 | + </Collapsible> |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +const NoteDropZone = memo(function ({ id }: { id: string }) { |
| 65 | + return <div className="h-1 bg-primary/20"></div>; |
| 66 | +}); |
0 commit comments