feat(ui): improve folder structure UI

This commit is contained in:
Hamzat Victor 2024-09-24 13:57:40 +01:00
parent c2a23fcbcb
commit af45df28d5

View File

@ -1,18 +1,20 @@
"use client"; "use client"
import Image from "next/image"; import Image from "next/image"
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react"
import { getIconForFolder, getIconForOpenFolder } from "vscode-icons-js"; import { getIconForFolder, getIconForOpenFolder } from "vscode-icons-js"
import { TFile, TFolder, TTab } from "@/lib/types"; import { TFile, TFolder, TTab } from "@/lib/types"
import SidebarFile from "./file"; import SidebarFile from "./file"
import { import {
ContextMenu, ContextMenu,
ContextMenuContent, ContextMenuContent,
ContextMenuItem, ContextMenuItem,
ContextMenuTrigger, ContextMenuTrigger,
} from "@/components/ui/context-menu"; } from "@/components/ui/context-menu"
import { Loader2, Pencil, Trash2 } from "lucide-react"; import { ChevronRight, Loader2, Pencil, Trash2 } from "lucide-react"
import { dropTargetForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter"; import { dropTargetForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter"
import { cn } from "@/lib/utils"
import { motion, AnimatePresence } from "framer-motion"
// Note: Renaming has not been implemented in the backend yet, so UI relating to renaming is commented out // Note: Renaming has not been implemented in the backend yet, so UI relating to renaming is commented out
@ -25,27 +27,27 @@ export default function SidebarFolder({
movingId, movingId,
deletingFolderId, deletingFolderId,
}: { }: {
data: TFolder; data: TFolder
selectFile: (file: TTab) => void; selectFile: (file: TTab) => void
handleRename: ( handleRename: (
id: string, id: string,
newName: string, newName: string,
oldName: string, oldName: string,
type: "file" | "folder" type: "file" | "folder"
) => boolean; ) => boolean
handleDeleteFile: (file: TFile) => void; handleDeleteFile: (file: TFile) => void
handleDeleteFolder: (folder: TFolder) => void; handleDeleteFolder: (folder: TFolder) => void
movingId: string; movingId: string
deletingFolderId: string; deletingFolderId: string
}) { }) {
const ref = useRef(null); // drop target const ref = useRef(null) // drop target
const [isDraggedOver, setIsDraggedOver] = useState(false); const [isDraggedOver, setIsDraggedOver] = useState(false)
const isDeleting = const isDeleting =
deletingFolderId.length > 0 && data.id.startsWith(deletingFolderId); deletingFolderId.length > 0 && data.id.startsWith(deletingFolderId)
useEffect(() => { useEffect(() => {
const el = ref.current; const el = ref.current
if (el) if (el)
return dropTargetForElements({ return dropTargetForElements({
@ -67,17 +69,17 @@ export default function SidebarFolder({
// no dropping while awaiting move // no dropping while awaiting move
canDrop: () => { canDrop: () => {
return !movingId; return !movingId
}, },
}); })
}, []); }, [])
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false)
const folder = isOpen const folder = isOpen
? getIconForOpenFolder(data.name) ? getIconForOpenFolder(data.name)
: getIconForFolder(data.name); : getIconForFolder(data.name)
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null)
// const [editing, setEditing] = useState(false); // const [editing, setEditing] = useState(false);
// useEffect(() => { // useEffect(() => {
@ -96,6 +98,12 @@ export default function SidebarFolder({
isDraggedOver ? "bg-secondary/50 rounded-t-sm" : "rounded-sm" 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`} } w-full flex items-center h-7 px-1 transition-colors hover:bg-secondary cursor-pointer`}
> >
<ChevronRight
className={cn(
"min-w-3 min-h-3 mr-1 ml-auto transition-all duration-300",
isOpen ? "transform rotate-90" : ""
)}
/>
<Image <Image
src={`/icons/${folder}`} src={`/icons/${folder}`}
alt="Folder icon" alt="Folder icon"
@ -149,21 +157,36 @@ export default function SidebarFolder({
<ContextMenuItem <ContextMenuItem
disabled={isDeleting} disabled={isDeleting}
onClick={() => { onClick={() => {
handleDeleteFolder(data); handleDeleteFolder(data)
}} }}
> >
<Trash2 className="w-4 h-4 mr-2" /> <Trash2 className="w-4 h-4 mr-2" />
Delete Delete
</ContextMenuItem> </ContextMenuItem>
</ContextMenuContent> </ContextMenuContent>
<AnimatePresence>
{isOpen ? ( {isOpen ? (
<div <motion.div
className={`flex w-full items-stretch ${ className="overflow-y-hidden"
isDraggedOver ? "rounded-b-sm bg-secondary/50" : "" initial={{
}`} height: 0,
opacity: 0,
}}
animate={{
height: "auto",
opacity: 1,
}}
exit={{
height: 0,
opacity: 0,
}}
> >
<div className="w-[1px] bg-border mx-2 h-full"></div> <div
<div className="flex flex-col grow"> 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) => {data.children.map((child) =>
child.type === "file" ? ( child.type === "file" ? (
<SidebarFile <SidebarFile
@ -190,7 +213,9 @@ export default function SidebarFolder({
)} )}
</div> </div>
</div> </div>
</motion.div>
) : null} ) : null}
</AnimatePresence>
</ContextMenu> </ContextMenu>
); )
} }