"use client" import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger, } from "@/components/ui/context-menu" import { TFile, TFolder, TTab } from "@/lib/types" import { cn } from "@/lib/utils" import { dropTargetForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter" import { AnimatePresence, motion } from "framer-motion" import { ChevronRight, Pencil, Trash2 } from "lucide-react" import Image from "next/image" import { useEffect, useRef, useState } from "react" import { getIconForFolder, getIconForOpenFolder } from "vscode-icons-js" import SidebarFile from "./file" // Note: Renaming has not been implemented in the backend yet, so UI relating to renaming is commented out export default function SidebarFolder({ data, selectFile, handleRename, handleDeleteFile, handleDeleteFolder, movingId, deletingFolderId, }: { 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 deletingFolderId: string }) { const ref = useRef(null) // drop target const [isDraggedOver, setIsDraggedOver] = useState(false) const isDeleting = deletingFolderId.length > 0 && data.id.startsWith(deletingFolderId) 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; // }, // no dropping while awaiting move canDrop: () => { return !movingId }, }) }, []) const [isOpen, setIsOpen] = useState(false) const folder = isOpen ? getIconForOpenFolder(data.name) : getIconForFolder(data.name) const inputRef = useRef(null) // const [editing, setEditing] = useState(false); // useEffect(() => { // if (editing) { // inputRef.current?.focus(); // } // }, [editing]); 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 {isDeleting ? ( <>
Deleting...
) : (
{ // e.preventDefault(); // setEditing(false); // }} > {/* { setEditing(false); }} /> */}
)}
{ // setEditing(true); // }} > Rename { handleDeleteFolder(data) }} > Delete {isOpen ? (
{data.children.map((child) => child.type === "file" ? ( ) : ( ) )}
) : null}
) }