131 lines
3.6 KiB
TypeScript
Raw Normal View History

"use client";
2024-04-11 04:24:36 -04:00
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";
2024-05-05 00:06:10 -07:00
import {
ContextMenu,
ContextMenuContent,
ContextMenuItem,
ContextMenuTrigger,
} from "@/components/ui/context-menu";
import { Pencil, Trash2 } from "lucide-react";
2024-04-11 04:24:36 -04:00
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-04-26 00:10:53 -04: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"
) => boolean;
handleDeleteFile: (file: TFile) => void;
handleDeleteFolder: (folder: TFolder) => void;
2024-04-26 00:10:53 -04:00
}) {
const [isOpen, setIsOpen] = useState(false);
2024-04-11 04:24:36 -04:00
const folder = isOpen
? getIconForOpenFolder(data.name)
: getIconForFolder(data.name);
2024-04-11 04:24:36 -04:00
const [editing, setEditing] = useState(false);
const inputRef = useRef<HTMLInputElement>(null);
2024-04-27 14:23:09 -04:00
useEffect(() => {
if (editing) {
inputRef.current?.focus();
2024-04-27 14:23:09 -04:00
}
}, [editing]);
2024-04-27 14:23:09 -04:00
2024-04-11 04:24:36 -04:00
return (
2024-05-05 00:06:10 -07:00
<ContextMenu>
<ContextMenuTrigger
2024-04-11 04:24:36 -04:00
onClick={() => setIsOpen((prev) => !prev)}
className="w-full flex items-center h-7 px-1 transition-colors hover:bg-secondary rounded-sm cursor-pointer"
>
<Image
src={`/icons/${folder}`}
alt="Folder icon"
width={18}
height={18}
className="mr-2"
/>
2024-04-27 14:23:09 -04:00
<form
onSubmit={(e) => {
e.preventDefault();
setEditing(false);
2024-04-27 14:23:09 -04:00
}}
>
<input
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
}}
/>
</form>
2024-05-05 00:06:10 -07:00
</ContextMenuTrigger>
<ContextMenuContent>
<ContextMenuItem
onClick={() => {
setEditing(true);
2024-05-05 00:06:10 -07:00
}}
>
<Pencil className="w-4 h-4 mr-2" />
Rename
</ContextMenuItem>
<ContextMenuItem
// disabled={pendingDelete}
onClick={() => {
console.log("delete");
2024-05-05 00:06:10 -07:00
// setPendingDelete(true)
// handleDeleteFile(data)
}}
>
<Trash2 className="w-4 h-4 mr-2" />
Delete
</ContextMenuItem>
</ContextMenuContent>
2024-04-11 04:24:36 -04:00
{isOpen ? (
<div className="flex w-full items-stretch">
<div className="w-[1px] bg-border mx-2 h-full"></div>
<div className="flex flex-col grow">
{data.children.map((child) =>
child.type === "file" ? (
2024-04-26 00:10:53 -04:00
<SidebarFile
key={child.id}
data={child}
selectFile={selectFile}
2024-04-27 14:23:09 -04:00
handleRename={handleRename}
2024-04-30 01:56:43 -04:00
handleDeleteFile={handleDeleteFile}
2024-04-26 00:10:53 -04:00
/>
2024-04-11 04:24:36 -04:00
) : (
2024-04-26 00:10:53 -04:00
<SidebarFolder
key={child.id}
data={child}
selectFile={selectFile}
2024-04-27 14:23:09 -04:00
handleRename={handleRename}
2024-04-30 01:56:43 -04:00
handleDeleteFile={handleDeleteFile}
handleDeleteFolder={handleDeleteFolder}
2024-04-26 00:10:53 -04:00
/>
2024-04-11 04:24:36 -04:00
)
)}
</div>
</div>
) : null}
2024-05-05 00:06:10 -07:00
</ContextMenu>
);
2024-04-11 04:24:36 -04:00
}