2024-04-11 04:24:36 -04:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
import Image from "next/image"
|
2024-04-27 14:23:09 -04:00
|
|
|
import { useEffect, useRef, useState } from "react"
|
2024-04-11 04:24:36 -04:00
|
|
|
import { getIconForFolder, getIconForOpenFolder } from "vscode-icons-js"
|
2024-04-27 11:08:19 -04:00
|
|
|
import { TFile, TFolder, TTab } from "./types"
|
2024-04-11 04:24:36 -04:00
|
|
|
import SidebarFile from "./file"
|
|
|
|
|
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-26 00:10:53 -04:00
|
|
|
}: {
|
|
|
|
data: TFolder
|
2024-04-27 11:08:19 -04:00
|
|
|
selectFile: (file: TTab) => void
|
2024-04-27 14:23:09 -04:00
|
|
|
handleRename: (
|
|
|
|
id: string,
|
|
|
|
newName: string,
|
|
|
|
oldName: string,
|
|
|
|
type: "file" | "folder"
|
|
|
|
) => boolean
|
2024-04-26 00:10:53 -04:00
|
|
|
}) {
|
2024-04-11 04:24:36 -04:00
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
|
|
const folder = isOpen
|
|
|
|
? getIconForOpenFolder(data.name)
|
|
|
|
: getIconForFolder(data.name)
|
|
|
|
|
2024-04-27 14:23:09 -04:00
|
|
|
const [editing, setEditing] = useState(false)
|
|
|
|
const inputRef = useRef<HTMLInputElement>(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (editing) {
|
|
|
|
inputRef.current?.focus()
|
|
|
|
}
|
|
|
|
}, [editing])
|
|
|
|
|
2024-04-11 04:24:36 -04:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div
|
|
|
|
onClick={() => setIsOpen((prev) => !prev)}
|
2024-04-27 14:23:09 -04:00
|
|
|
onDoubleClick={() => {
|
|
|
|
setEditing(true)
|
|
|
|
}}
|
2024-04-11 04:24:36 -04:00
|
|
|
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()
|
|
|
|
console.log("file renamed")
|
|
|
|
setEditing(false)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
ref={inputRef}
|
|
|
|
className={`bg-transparent outline-foreground w-full ${
|
|
|
|
editing ? "" : "pointer-events-none"
|
|
|
|
}`}
|
|
|
|
disabled={!editing}
|
|
|
|
defaultValue={data.name}
|
|
|
|
onBlur={() => {
|
|
|
|
console.log("file renamed")
|
|
|
|
setEditing(false)
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</form>
|
2024-04-11 04:24:36 -04:00
|
|
|
</div>
|
|
|
|
{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-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-26 00:10:53 -04:00
|
|
|
/>
|
2024-04-11 04:24:36 -04:00
|
|
|
)
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|