"use client"; import Image from "next/image"; import { useEffect, useRef, useState } from "react"; import { getIconForFolder, getIconForOpenFolder } from "vscode-icons-js"; import { TFile, TFolder, TTab } from "@/lib/types"; import SidebarFile from "./file"; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger, } from "@/components/ui/context-menu"; import { Pencil, Trash2 } from "lucide-react"; import { dropTargetForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter"; export default function SidebarFolder({ data, selectFile, handleRename, handleDeleteFile, handleDeleteFolder, movingId, }: { data: TFolder; selectFile: (file: TTab) => void; handleRename: ( id: string, newName: string, oldName: string, type: "file" | "folder" ) => boolean; handleDeleteFile: (file: TFile) => void; handleDeleteFolder: (folder: TFolder) => void; movingId: string; }) { const ref = useRef(null); // drop target const [isDraggedOver, setIsDraggedOver] = useState(false); useEffect(() => { const el = ref.current; if (el) return dropTargetForElements({ element: el, onDragEnter: () => setIsDraggedOver(true), onDragLeave: () => setIsDraggedOver(false), onDrop: () => setIsDraggedOver(false), getData: () => ({ id: data.id }), // Commented out to avoid propagating drop event downwards // Todo: Make this logic more elegant, the current implementation is just checking at the end in index.tsx // canDrop: ({ source }) => { // const file = data.children.find( // (child) => child.id === source.data.id // ); // return !file; // }, canDrop: () => { return !movingId; }, // no dropping while awaiting move }); }, []); const [isOpen, setIsOpen] = useState(false); const folder = isOpen ? getIconForOpenFolder(data.name) : getIconForFolder(data.name); const [editing, setEditing] = useState(false); const inputRef = useRef(null); useEffect(() => { if (editing) { inputRef.current?.focus(); } }, [editing]); // return ( //
//
// ) return ( setIsOpen((prev) => !prev)} className={`${ isDraggedOver ? "bg-secondary/50 rounded-t-sm" : "rounded-sm" } w-full flex items-center h-7 px-1 transition-colors hover:bg-secondary cursor-pointer`} > Folder icon
{ e.preventDefault(); setEditing(false); }} > { setEditing(false); }} />
{ setEditing(true); }} > Rename { console.log("delete"); // setPendingDelete(true) // handleDeleteFile(data) }} > Delete {isOpen ? (
{data.children.map((child) => child.type === "file" ? ( ) : ( ) )}
) : null}
); }