add folder logic
This commit is contained in:
parent
9a5a0e13d3
commit
b496ab193d
@ -23,6 +23,7 @@ import { IDisposable, IPty, spawn } from "node-pty"
|
|||||||
import {
|
import {
|
||||||
MAX_BODY_SIZE,
|
MAX_BODY_SIZE,
|
||||||
createFileRL,
|
createFileRL,
|
||||||
|
createFolderRL,
|
||||||
deleteFileRL,
|
deleteFileRL,
|
||||||
renameFileRL,
|
renameFileRL,
|
||||||
saveFileRL,
|
saveFileRL,
|
||||||
@ -222,6 +223,22 @@ io.on("connection", async (socket) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
socket.on("createFolder", async (name: string, callback) => {
|
||||||
|
try {
|
||||||
|
await createFolderRL.consume(data.userId, 1)
|
||||||
|
|
||||||
|
const id = `projects/${data.sandboxId}/${name}`
|
||||||
|
|
||||||
|
fs.mkdir(path.join(dirName, id), { recursive: true }, function (err) {
|
||||||
|
if (err) throw err
|
||||||
|
})
|
||||||
|
|
||||||
|
callback()
|
||||||
|
} catch (e) {
|
||||||
|
io.emit("rateLimit", "Rate limited: folder creation. Please slow down.")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
socket.on("renameFile", async (fileId: string, newName: string) => {
|
socket.on("renameFile", async (fileId: string, newName: string) => {
|
||||||
try {
|
try {
|
||||||
await renameFileRL.consume(data.userId, 1)
|
await renameFileRL.consume(data.userId, 1)
|
||||||
@ -277,8 +294,6 @@ io.on("connection", async (socket) => {
|
|||||||
socket.on("deleteFolder", async (folderId: string, callback) => {
|
socket.on("deleteFolder", async (folderId: string, callback) => {
|
||||||
const files = await getFolder(folderId)
|
const files = await getFolder(folderId)
|
||||||
|
|
||||||
console.log("deleting folder", folderId, files)
|
|
||||||
|
|
||||||
await Promise.all(files.map(async (file) => {
|
await Promise.all(files.map(async (file) => {
|
||||||
fs.unlink(path.join(dirName, file), function (err) {
|
fs.unlink(path.join(dirName, file), function (err) {
|
||||||
if (err) throw err
|
if (err) throw err
|
||||||
|
@ -12,6 +12,11 @@ export const createFileRL = new RateLimiterMemory({
|
|||||||
duration: 2,
|
duration: 2,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const createFolderRL = new RateLimiterMemory({
|
||||||
|
points: 1,
|
||||||
|
duration: 2,
|
||||||
|
})
|
||||||
|
|
||||||
export const renameFileRL = new RateLimiterMemory({
|
export const renameFileRL = new RateLimiterMemory({
|
||||||
points: 1,
|
points: 1,
|
||||||
duration: 2,
|
duration: 2,
|
||||||
|
@ -23,7 +23,7 @@ import Tab from "../ui/tab";
|
|||||||
import Sidebar from "./sidebar";
|
import Sidebar from "./sidebar";
|
||||||
import GenerateInput from "./generate";
|
import GenerateInput from "./generate";
|
||||||
import { Sandbox, User, TFile, TFolder, TTab } from "@/lib/types";
|
import { Sandbox, User, TFile, TFolder, TTab } from "@/lib/types";
|
||||||
import { processFileType, validateName } from "@/lib/utils";
|
import { addNew, processFileType, validateName } from "@/lib/utils";
|
||||||
import { Cursors } from "./live/cursors";
|
import { Cursors } from "./live/cursors";
|
||||||
import { Terminal } from "@xterm/xterm";
|
import { Terminal } from "@xterm/xterm";
|
||||||
import DisableAccessModal from "./live/disableModal";
|
import DisableAccessModal from "./live/disableModal";
|
||||||
@ -615,17 +615,7 @@ export default function CodeEditor({
|
|||||||
handleDeleteFolder={handleDeleteFolder}
|
handleDeleteFolder={handleDeleteFolder}
|
||||||
socket={socket}
|
socket={socket}
|
||||||
setFiles={setFiles}
|
setFiles={setFiles}
|
||||||
addNew={(name, type) => {
|
addNew={(name, type) => addNew(name, type, setFiles, sandboxData)}
|
||||||
if (type === "file") {
|
|
||||||
setFiles((prev) => [
|
|
||||||
...prev,
|
|
||||||
{ id: `projects/${sandboxData.id}/${name}`, name, type: "file" },
|
|
||||||
]);
|
|
||||||
} else {
|
|
||||||
console.log("adding folder");
|
|
||||||
// setFiles(prev => [...prev, { id, name, type: "folder", children: [] }])
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
deletingFolderId={deletingFolderId}
|
deletingFolderId={deletingFolderId}
|
||||||
// AI Copilot Toggle
|
// AI Copilot Toggle
|
||||||
ai={ai}
|
ai={ai}
|
||||||
|
@ -81,7 +81,6 @@ export default function Sidebar({
|
|||||||
|
|
||||||
const fileFolder = fileId.split("/").slice(0, -1).join("/");
|
const fileFolder = fileId.split("/").slice(0, -1).join("/");
|
||||||
if (fileFolder === folderId) {
|
if (fileFolder === folderId) {
|
||||||
console.log("NO");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ export default function New({
|
|||||||
}) {
|
}) {
|
||||||
const inputRef = useRef<HTMLInputElement>(null);
|
const inputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
const createNew = () => {
|
function createNew() {
|
||||||
const name = inputRef.current?.value;
|
const name = inputRef.current?.value;
|
||||||
|
|
||||||
if (name) {
|
if (name) {
|
||||||
@ -34,11 +34,15 @@ export default function New({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
socket.emit("createFolder", name, () => {
|
||||||
|
addNew(name, type);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stopEditing();
|
stopEditing();
|
||||||
};
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
inputRef.current?.focus();
|
inputRef.current?.focus();
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { type ClassValue, clsx } from "clsx"
|
import { type ClassValue, clsx } from "clsx"
|
||||||
// import { toast } from "sonner"
|
// import { toast } from "sonner"
|
||||||
import { twMerge } from "tailwind-merge"
|
import { twMerge } from "tailwind-merge"
|
||||||
|
import { Sandbox, TFile, TFolder } from "./types"
|
||||||
|
|
||||||
export function cn(...inputs: ClassValue[]) {
|
export function cn(...inputs: ClassValue[]) {
|
||||||
return twMerge(clsx(inputs))
|
return twMerge(clsx(inputs))
|
||||||
@ -35,3 +36,15 @@ export function validateName(
|
|||||||
}
|
}
|
||||||
return { status: true, message: "" }
|
return { status: true, message: "" }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function addNew(name: string, type: "file" | "folder", setFiles: React.Dispatch<React.SetStateAction<(TFolder | TFile)[]>>, sandboxData: Sandbox) {
|
||||||
|
if (type === "file") {
|
||||||
|
setFiles((prev) => [
|
||||||
|
...prev,
|
||||||
|
{ id: `projects/${sandboxData.id}/${name}`, name, type: "file" },
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
console.log("adding folder");
|
||||||
|
setFiles(prev => [...prev, { id: `projects/${sandboxData.id}/${name}`, name, type: "folder", children: [] }])
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user