"use client" import { Sandbox, TFile, TFolder, TTab } from "@/lib/types" import { FilePlus, FolderPlus, MessageSquareMore, Sparkles } from "lucide-react" import { useEffect, useMemo, useRef, useState } from "react" import { Socket } from "socket.io-client" import SidebarFile from "./file" import SidebarFolder from "./folder" import New from "./new" import { Button } from "@/components/ui/button" import { Skeleton } from "@/components/ui/skeleton" import { sortFileExplorer } from "@/lib/utils" import { dropTargetForElements, monitorForElements, } from "@atlaskit/pragmatic-drag-and-drop/element/adapter" export default function Sidebar({ sandboxData, files, selectFile, handleRename, handleDeleteFile, handleDeleteFolder, socket, setFiles, addNew, deletingFolderId, }: { sandboxData: Sandbox files: (TFile | TFolder)[] selectFile: (tab: TTab) => void handleRename: ( id: string, newName: string, oldName: string, type: "file" | "folder" ) => boolean handleDeleteFile: (file: TFile) => void handleDeleteFolder: (folder: TFolder) => void socket: Socket setFiles: (files: (TFile | TFolder)[]) => void addNew: (name: string, type: "file" | "folder") => void deletingFolderId: string }) { const ref = useRef(null) // drop target const [creatingNew, setCreatingNew] = useState<"file" | "folder" | null>(null) const [movingId, setMovingId] = useState("") const sortedFiles = useMemo(() => { return sortFileExplorer(files) }, [files]) useEffect(() => { const el = ref.current if (el) { return dropTargetForElements({ element: el, getData: () => ({ id: `projects/${sandboxData.id}` }), canDrop: ({ source }) => { const file = files.find((child) => child.id === source.data.id) return !file }, }) } }, [files]) useEffect(() => { return monitorForElements({ onDrop({ source, location }) { const destination = location.current.dropTargets[0] if (!destination) { return } const fileId = source.data.id as string const folderId = destination.data.id as string const fileFolder = fileId.split("/").slice(0, -1).join("/") if (fileFolder === folderId) { return } console.log("move file", fileId, "to folder", folderId) setMovingId(fileId) socket.emit( "moveFile", { fileId, folderId }, (response: (TFolder | TFile)[]) => { setFiles(response) setMovingId("") } ) }, }) }, []) return (
Explorer
{/* Todo: Implement file searching */} {/* */}
{/*
*/} {sortedFiles.length === 0 ? (
{new Array(6).fill(0).map((_, i) => ( ))}
) : ( <> {sortedFiles.map((child) => child.type === "file" ? ( ) : ( ) )} {creatingNew !== null ? ( { setCreatingNew(null) }} addNew={addNew} /> ) : null} )}
) }