210 lines
6.8 KiB
TypeScript
Raw Permalink Normal View History

"use client";
2024-04-26 00:10:53 -04:00
2024-05-11 19:28:37 -07:00
import {
FilePlus,
FolderPlus,
Loader2,
MonitorPlay,
Search,
Sparkles,
} from "lucide-react";
import SidebarFile from "./file";
import SidebarFolder from "./folder";
2024-05-10 00:12:41 -07:00
import { Sandbox, TFile, TFolder, TTab } from "@/lib/types";
import { useEffect, useRef, useState } from "react";
import New from "./new";
import { Socket } from "socket.io-client";
import { Switch } from "@/components/ui/switch";
2024-04-11 04:24:36 -04:00
2024-05-10 00:12:41 -07:00
import {
dropTargetForElements,
monitorForElements,
} from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
2024-05-11 19:28:37 -07:00
import Button from "@/components/ui/customButton";
2024-05-10 00:12:41 -07:00
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-04-29 00:50:25 -04:00
addNew,
2024-05-04 01:50:33 -07:00
ai,
setAi,
2024-05-11 17:23:45 -07:00
deletingFolderId,
2024-04-26 00:10:53 -04:00
}: {
2024-05-10 00:12:41 -07: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"
) => boolean;
handleDeleteFile: (file: TFile) => void;
handleDeleteFolder: (folder: TFolder) => void;
socket: Socket;
2024-05-10 00:12:41 -07:00
setFiles: (files: (TFile | TFolder)[]) => void;
addNew: (name: string, type: "file" | "folder") => void;
ai: boolean;
setAi: React.Dispatch<React.SetStateAction<boolean>>;
2024-05-11 17:23:45 -07:00
deletingFolderId: string;
2024-04-26 00:10:53 -04:00
}) {
2024-05-10 00:12:41 -07:00
const ref = useRef(null); // drop target
const [creatingNew, setCreatingNew] = useState<"file" | "folder" | null>(
null
);
2024-05-10 00:12:41 -07:00
const [movingId, setMovingId] = useState("");
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("");
}
);
},
});
}, []);
2024-04-28 01:33:28 -04:00
2024-04-11 04:24:36 -04:00
return (
2024-04-28 20:06:47 -04:00
<div className="h-full w-56 select-none flex flex-col text-sm items-start justify-between p-2">
<div className="w-full flex flex-col items-start">
<div className="flex w-full items-center justify-between h-8 mb-1 ">
<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-04-28 20:06:47 -04:00
{files.length === 0 ? (
<div className="w-full flex justify-center">
<Loader2 className="w-4 h-4 animate-spin" />
</div>
) : (
<>
{files.map((child) =>
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={() => {
setCreatingNew(null);
2024-04-29 00:50:25 -04:00
}}
addNew={addNew}
2024-04-28 01:33:28 -04:00
/>
2024-04-28 20:06:47 -04:00
) : null}
</>
)}
</div>
</div>
2024-05-11 19:28:37 -07:00
<div className="w-full space-y-4">
<div className="flex items-center justify-between w-full">
<div className="flex items-center">
<Sparkles
className={`h-4 w-4 mr-2 ${
ai ? "text-indigo-500" : "text-muted-foreground"
}`}
/>
Copilot{" "}
<span className="font-mono text-muted-foreground inline-block ml-1.5 text-xs leading-none border border-b-2 border-muted-foreground py-1 px-1.5 rounded-md">
G
</span>
</div>
<Switch checked={ai} onCheckedChange={setAi} />
2024-05-05 14:33:09 -07:00
</div>
2024-05-13 15:31:48 -07:00
{/* <Button className="w-full">
2024-05-11 19:28:37 -07:00
<MonitorPlay className="w-4 h-4 mr-2" /> Run
2024-05-13 15:31:48 -07:00
</Button> */}
2024-05-05 14:33:09 -07:00
</div>
2024-04-11 04:24:36 -04:00
</div>
);
2024-04-11 04:24:36 -04:00
}