222 lines
6.6 KiB
TypeScript
Raw Normal View History

2024-09-24 13:57:40 +01:00
"use client"
2024-04-11 04:24:36 -04:00
2024-05-05 00:06:10 -07:00
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuTrigger,
2024-09-24 13:57:40 +01:00
} from "@/components/ui/context-menu"
2024-10-21 13:57:45 -06:00
import { TFile, TFolder, TTab } from "@/lib/types"
2024-09-24 13:57:40 +01:00
import { cn } from "@/lib/utils"
2024-10-21 13:57:45 -06:00
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"
2024-04-11 04:24:36 -04:00
2024-05-11 17:23:45 -07:00
// Note: Renaming has not been implemented in the backend yet, so UI relating to renaming is commented out
2024-04-26 00:10:53 -04:00
export default function SidebarFolder({
data,
selectFile,
2024-04-27 14:23:09 -04:00
handleRename,
2024-04-30 01:56:43 -04:00
handleDeleteFile,
handleDeleteFolder,
2024-05-10 00:12:41 -07:00
movingId,
2024-05-11 17:23:45 -07:00
deletingFolderId,
2024-04-26 00:10:53 -04:00
}: {
2024-09-24 13:57:40 +01:00
data: TFolder
selectFile: (file: TTab) => void
2024-04-27 14:23:09 -04:00
handleRename: (
id: string,
newName: string,
oldName: string,
type: "file" | "folder"
2024-09-24 13:57:40 +01:00
) => boolean
handleDeleteFile: (file: TFile) => void
handleDeleteFolder: (folder: TFolder) => void
movingId: string
deletingFolderId: string
2024-04-26 00:10:53 -04:00
}) {
2024-09-24 13:57:40 +01:00
const ref = useRef(null) // drop target
const [isDraggedOver, setIsDraggedOver] = useState(false)
2024-05-10 00:12:41 -07:00
2024-05-11 17:23:45 -07:00
const isDeleting =
2024-09-24 13:57:40 +01:00
deletingFolderId.length > 0 && data.id.startsWith(deletingFolderId)
2024-05-11 17:23:45 -07:00
2024-05-10 00:12:41 -07:00
useEffect(() => {
2024-09-24 13:57:40 +01:00
const el = ref.current
2024-05-10 00:12:41 -07:00
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;
// },
2024-05-11 17:23:45 -07:00
// no dropping while awaiting move
2024-05-10 00:12:41 -07:00
canDrop: () => {
2024-09-24 13:57:40 +01:00
return !movingId
2024-05-11 17:23:45 -07:00
},
2024-09-24 13:57:40 +01:00
})
}, [])
2024-05-10 00:12:41 -07:00
2024-09-24 13:57:40 +01:00
const [isOpen, setIsOpen] = useState(false)
2024-04-11 04:24:36 -04:00
const folder = isOpen
? getIconForOpenFolder(data.name)
2024-09-24 13:57:40 +01:00
: getIconForFolder(data.name)
2024-04-11 04:24:36 -04:00
2024-09-24 13:57:40 +01:00
const inputRef = useRef<HTMLInputElement>(null)
2024-05-11 17:23:45 -07:00
// const [editing, setEditing] = useState(false);
2024-04-27 14:23:09 -04:00
2024-05-11 17:23:45 -07:00
// useEffect(() => {
// if (editing) {
// inputRef.current?.focus();
// }
// }, [editing]);
2024-05-10 00:12:41 -07:00
2024-04-11 04:24:36 -04:00
return (
2024-05-05 00:06:10 -07:00
<ContextMenu>
<ContextMenuTrigger
2024-05-10 00:12:41 -07:00
ref={ref}
2024-05-11 17:23:45 -07:00
disabled={isDeleting}
2024-04-11 04:24:36 -04:00
onClick={() => setIsOpen((prev) => !prev)}
2024-05-10 00:12:41 -07:00
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`}
2024-04-11 04:24:36 -04:00
>
2024-09-24 13:57:40 +01:00
<ChevronRight
className={cn(
"min-w-3 min-h-3 mr-1 ml-auto transition-all duration-300",
isOpen ? "transform rotate-90" : ""
)}
/>
2024-04-11 04:24:36 -04:00
<Image
src={`/icons/${folder}`}
alt="Folder icon"
width={18}
height={18}
className="mr-2"
/>
2024-05-11 17:23:45 -07:00
{isDeleting ? (
<>
<div className="text-muted-foreground animate-pulse">
Deleting...
</div>
</>
) : (
<form
// onSubmit={(e) => {
// e.preventDefault();
// setEditing(false);
// }}
>
<input
ref={inputRef}
disabled
className={`pointer-events-none bg-transparent transition-all focus-visible:outline-none focus-visible:ring-offset-2 focus-visible:ring-offset-background focus-visible:ring-2 focus-visible:ring-ring rounded-sm w-full`}
defaultValue={data.name}
/>
{/* <input
2024-04-27 14:23:09 -04:00
ref={inputRef}
2024-04-28 01:33:28 -04:00
className={`bg-transparent transition-all focus-visible:outline-none focus-visible:ring-offset-2 focus-visible:ring-offset-background focus-visible:ring-2 focus-visible:ring-ring rounded-sm w-full ${
2024-04-27 14:23:09 -04:00
editing ? "" : "pointer-events-none"
}`}
disabled={!editing}
defaultValue={data.name}
onBlur={() => {
setEditing(false);
2024-04-27 14:23:09 -04:00
}}
2024-05-11 17:23:45 -07:00
/> */}
</form>
)}
2024-05-05 00:06:10 -07:00
</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem
2024-05-11 17:23:45 -07:00
disabled
// onClick={() => {
// setEditing(true);
// }}
2024-05-05 00:06:10 -07:00
>
<Pencil className="w-4 h-4 mr-2" />
Rename
</ContextMenuItem>
<ContextMenuItem
2024-05-11 17:23:45 -07:00
disabled={isDeleting}
2024-05-05 00:06:10 -07:00
onClick={() => {
2024-09-24 13:57:40 +01:00
handleDeleteFolder(data)
2024-05-05 00:06:10 -07:00
}}
>
<Trash2 className="w-4 h-4 mr-2" />
Delete
</ContextMenuItem>
</ContextMenuContent>
2024-09-24 13:57:40 +01:00
<AnimatePresence>
{isOpen ? (
<motion.div
className="overflow-y-hidden"
initial={{
height: 0,
opacity: 0,
}}
animate={{
height: "auto",
opacity: 1,
}}
exit={{
height: 0,
opacity: 0,
}}
>
<div
className={cn(
isDraggedOver ? "rounded-b-sm bg-secondary/50" : ""
)}
>
<div className="flex flex-col grow ml-2 pl-2 border-l border-border">
{data.children.map((child) =>
child.type === "file" ? (
<SidebarFile
key={child.id}
data={child}
selectFile={selectFile}
handleRename={handleRename}
handleDeleteFile={handleDeleteFile}
movingId={movingId}
deletingFolderId={deletingFolderId}
/>
) : (
<SidebarFolder
key={child.id}
data={child}
selectFile={selectFile}
handleRename={handleRename}
handleDeleteFile={handleDeleteFile}
handleDeleteFolder={handleDeleteFolder}
movingId={movingId}
deletingFolderId={deletingFolderId}
/>
)
)}
</div>
</div>
</motion.div>
) : null}
</AnimatePresence>
2024-05-05 00:06:10 -07:00
</ContextMenu>
2024-09-24 13:57:40 +01:00
)
2024-04-11 04:24:36 -04:00
}