228 lines
7.8 KiB
TypeScript
Raw Permalink Normal View History

2024-10-21 13:57:45 -06:00
"use client"
2024-04-26 00:10:53 -04:00
2024-10-21 13:57:45 -06:00
import { Sandbox, TFile, TFolder, TTab } from "@/lib/types"
import { FilePlus, FolderPlus, MessageSquareMore, Sparkles } from "lucide-react"
2024-10-14 12:09:17 +01:00
import { useEffect, useMemo, useRef, useState } from "react"
2024-10-21 13:57:45 -06:00
import { Socket } from "socket.io-client"
import SidebarFile from "./file"
import SidebarFolder from "./folder"
import New from "./new"
2024-04-11 04:24:36 -04:00
import { Button } from "@/components/ui/button"
2024-10-21 13:57:17 -06:00
import { Skeleton } from "@/components/ui/skeleton"
2024-10-27 16:58:17 -04:00
import { cn, sortFileExplorer } from "@/lib/utils"
2024-05-10 00:12:41 -07:00
import {
dropTargetForElements,
monitorForElements,
2024-10-21 13:57:45 -06:00
} from "@atlaskit/pragmatic-drag-and-drop/element/adapter"
2024-04-26 00:10:53 -04:00
export default function Sidebar({
2024-05-10 00:12:41 -07:00
sandboxData,
2024-04-26 00:10:53 -04:00
files,
selectFile,
2024-04-27 14:23:09 -04:00
handleRename,
2024-04-30 01:56:43 -04:00
handleDeleteFile,
handleDeleteFolder,
2024-04-29 00:50:25 -04:00
socket,
2024-05-10 00:12:41 -07:00
setFiles,
2024-05-11 17:23:45 -07:00
deletingFolderId,
2024-10-27 16:58:17 -04:00
toggleAIChat,
isAIChatOpen,
2024-04-26 00:10:53 -04:00
}: {
2024-10-21 13:57:45 -06:00
sandboxData: Sandbox
files: (TFile | TFolder)[]
selectFile: (tab: TTab) => void
2024-04-27 14:23:09 -04:00
handleRename: (
id: string,
newName: string,
oldName: string,
type: "file" | "folder"
2024-10-21 13:57:45 -06:00
) => boolean
handleDeleteFile: (file: TFile) => void
handleDeleteFolder: (folder: TFolder) => void
socket: Socket
setFiles: (files: (TFile | TFolder)[]) => void
deletingFolderId: string
2024-10-27 16:58:17 -04:00
toggleAIChat: () => void
isAIChatOpen: boolean
2024-04-26 00:10:53 -04:00
}) {
2024-10-21 13:57:45 -06:00
const ref = useRef(null) // drop target
2024-05-10 00:12:41 -07:00
2024-10-21 13:57:45 -06:00
const [creatingNew, setCreatingNew] = useState<"file" | "folder" | null>(null)
const [movingId, setMovingId] = useState("")
2024-10-14 12:09:17 +01:00
const sortedFiles = useMemo(() => {
return sortFileExplorer(files)
}, [files])
2024-05-10 00:12:41 -07:00
useEffect(() => {
2024-10-21 13:57:45 -06:00
const el = ref.current
2024-05-10 00:12:41 -07:00
if (el) {
return dropTargetForElements({
element: el,
getData: () => ({ id: `projects/${sandboxData.id}` }),
canDrop: ({ source }) => {
2024-10-21 13:57:45 -06:00
const file = files.find((child) => child.id === source.data.id)
return !file
2024-05-10 00:12:41 -07:00
},
2024-10-21 13:57:45 -06:00
})
2024-05-10 00:12:41 -07:00
}
2024-10-21 13:57:45 -06:00
}, [files])
2024-05-10 00:12:41 -07:00
useEffect(() => {
return monitorForElements({
onDrop({ source, location }) {
2024-10-21 13:57:45 -06:00
const destination = location.current.dropTargets[0]
2024-05-10 00:12:41 -07:00
if (!destination) {
2024-10-21 13:57:45 -06:00
return
2024-05-10 00:12:41 -07:00
}
2024-10-21 13:57:45 -06:00
const fileId = source.data.id as string
const folderId = destination.data.id as string
2024-05-10 00:12:41 -07:00
2024-10-21 13:57:45 -06:00
const fileFolder = fileId.split("/").slice(0, -1).join("/")
2024-05-10 00:12:41 -07:00
if (fileFolder === folderId) {
2024-10-21 13:57:45 -06:00
return
2024-05-10 00:12:41 -07:00
}
2024-10-21 13:57:45 -06:00
console.log("move file", fileId, "to folder", folderId)
2024-05-10 00:12:41 -07:00
2024-10-21 13:57:45 -06:00
setMovingId(fileId)
2024-05-10 00:12:41 -07:00
socket.emit(
"moveFile",
{
fileId,
2024-11-17 12:35:56 -05:00
folderId,
},
2024-05-10 00:12:41 -07:00
(response: (TFolder | TFile)[]) => {
2024-10-21 13:57:45 -06:00
setFiles(response)
setMovingId("")
2024-05-10 00:12:41 -07:00
}
2024-10-21 13:57:45 -06:00
)
2024-05-10 00:12:41 -07:00
},
2024-10-21 13:57:45 -06:00
})
}, [])
2024-04-28 01:33:28 -04:00
2024-04-11 04:24:36 -04:00
return (
<div className="h-full w-56 select-none flex flex-col text-sm">
<div className="flex-grow overflow-auto p-2 pb-[84px]">
<div className="flex w-full items-center justify-between h-8 mb-1">
2024-04-28 20:06:47 -04:00
<div className="text-muted-foreground">Explorer</div>
<div className="flex space-x-1">
<button
2024-05-10 00:12:41 -07:00
disabled={!!creatingNew}
2024-04-28 20:06:47 -04:00
onClick={() => setCreatingNew("file")}
2024-05-10 00:12:41 -07:00
className="h-6 w-6 text-muted-foreground ml-0.5 flex items-center justify-center translate-x-1 bg-transparent hover:bg-muted-foreground/25 cursor-pointer rounded-sm transition-all focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:opacity-50 disabled:hover:bg-background"
2024-04-28 20:06:47 -04:00
>
<FilePlus className="w-4 h-4" />
</button>
<button
2024-05-10 00:12:41 -07:00
disabled={!!creatingNew}
2024-04-28 20:06:47 -04:00
onClick={() => setCreatingNew("folder")}
2024-05-10 00:12:41 -07:00
className="h-6 w-6 text-muted-foreground ml-0.5 flex items-center justify-center translate-x-1 bg-transparent hover:bg-muted-foreground/25 cursor-pointer rounded-sm transition-all focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:opacity-50 disabled:hover:bg-background"
2024-04-28 20:06:47 -04:00
>
<FolderPlus className="w-4 h-4" />
</button>
{/* Todo: Implement file searching */}
{/* <button className="h-6 w-6 text-muted-foreground ml-0.5 flex items-center justify-center translate-x-1 bg-transparent hover:bg-muted-foreground/25 cursor-pointer rounded-sm transition-all focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring">
2024-04-11 04:24:36 -04:00
<Search className="w-4 h-4" />
2024-04-28 01:33:28 -04:00
</button> */}
2024-04-26 22:34:56 -04:00
</div>
2024-04-28 20:06:47 -04:00
</div>
2024-05-10 00:12:41 -07:00
<div ref={ref} className="rounded-sm w-full mt-1 flex flex-col">
{/* <div
ref={ref}
className={`${
isDraggedOver ? "bg-secondary/50" : ""
} rounded-sm w-full mt-1 flex flex-col`}
> */}
2024-10-14 12:09:17 +01:00
{sortedFiles.length === 0 ? (
<div className="w-full flex flex-col justify-center">
{new Array(6).fill(0).map((_, i) => (
<Skeleton key={i} className="h-[1.625rem] mb-0.5 rounded-sm" />
))}
2024-04-28 20:06:47 -04:00
</div>
) : (
<>
2024-10-14 12:09:17 +01:00
{sortedFiles.map((child) =>
2024-04-28 20:06:47 -04:00
child.type === "file" ? (
<SidebarFile
key={child.id}
data={child}
selectFile={selectFile}
handleRename={handleRename}
2024-04-30 01:56:43 -04:00
handleDeleteFile={handleDeleteFile}
2024-05-10 00:12:41 -07:00
movingId={movingId}
2024-05-11 17:23:45 -07:00
deletingFolderId={deletingFolderId}
2024-04-28 20:06:47 -04:00
/>
) : (
<SidebarFolder
key={child.id}
data={child}
selectFile={selectFile}
handleRename={handleRename}
2024-04-30 01:56:43 -04:00
handleDeleteFile={handleDeleteFile}
handleDeleteFolder={handleDeleteFolder}
2024-05-10 00:12:41 -07:00
movingId={movingId}
2024-05-11 17:23:45 -07:00
deletingFolderId={deletingFolderId}
2024-04-28 20:06:47 -04:00
/>
)
)}
{creatingNew !== null ? (
<New
2024-04-29 00:50:25 -04:00
socket={socket}
2024-04-28 20:06:47 -04:00
type={creatingNew}
2024-04-29 00:50:25 -04:00
stopEditing={() => {
2024-10-21 13:57:45 -06:00
setCreatingNew(null)
2024-04-29 00:50:25 -04:00
}}
2024-04-28 01:33:28 -04:00
/>
2024-04-28 20:06:47 -04:00
) : null}
</>
)}
</div>
</div>
<div className="fixed bottom-0 w-48 flex flex-col p-2 bg-background">
2024-10-21 13:57:45 -06:00
<Button
variant="ghost"
className="w-full justify-start text-sm text-muted-foreground font-normal h-8 px-2 mb-2"
disabled
aria-disabled="true"
style={{ opacity: 1 }}
>
<Sparkles className="h-4 w-4 mr-2 text-indigo-500 opacity-70" />
2024-10-27 16:58:17 -04:00
AI Editor
<div className="ml-auto">
<kbd className="pointer-events-none inline-flex h-5 select-none items-center gap-1 rounded border bg-muted px-1.5 font-mono text-[10px] font-medium text-muted-foreground">
<span className="text-xs"></span>G
</kbd>
</div>
</Button>
2024-10-21 13:57:45 -06:00
<Button
variant="ghost"
2024-10-27 16:58:17 -04:00
className={cn(
"w-full justify-start text-sm font-normal h-8 px-2 mb-2 border-t",
2024-11-17 12:35:56 -05:00
isAIChatOpen
? "bg-muted-foreground/25 text-foreground"
2024-10-27 16:58:17 -04:00
: "text-muted-foreground"
)}
onClick={toggleAIChat}
aria-disabled={false}
2024-10-21 13:57:45 -06:00
style={{ opacity: 1 }}
>
2024-11-17 12:35:56 -05:00
<MessageSquareMore
2024-10-27 16:58:17 -04:00
className={cn(
"h-4 w-4 mr-2",
2024-11-17 12:35:56 -05:00
isAIChatOpen ? "text-indigo-500" : "text-indigo-500 opacity-70"
2024-10-27 16:58:17 -04:00
)}
/>
2024-10-21 13:57:45 -06:00
AI Chat
<div className="ml-auto">
<kbd className="pointer-events-none inline-flex h-5 select-none items-center gap-1 rounded border bg-muted px-1.5 font-mono text-[10px] font-medium text-muted-foreground">
<span className="text-xs"></span>L
2024-10-21 13:57:45 -06:00
</kbd>
</div>
</Button>
2024-05-05 14:33:09 -07:00
</div>
2024-04-11 04:24:36 -04:00
</div>
2024-10-21 13:57:45 -06:00
)
2024-04-11 04:24:36 -04:00
}