file saving

This commit is contained in:
Ishaan Dey
2024-04-27 19:12:25 -04:00
parent d6769f3407
commit c4e1a894c3
6 changed files with 62 additions and 28 deletions

View File

@ -5,7 +5,7 @@ import { Server } from "socket.io"
import { z } from "zod"
import { User } from "./types"
import { getSandboxFiles, renameFile } from "./utils"
import { getSandboxFiles, renameFile, saveFile } from "./utils"
dotenv.config()
@ -71,25 +71,30 @@ io.on("connection", async (socket) => {
const sandboxFiles = await getSandboxFiles(data.id)
socket.emit("loaded", sandboxFiles.files)
socket.on("getFile", (fileId: string, callback) => {
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
console.log("file " + file.id + ": ", file.data)
callback(file.data)
})
socket.on("saveFile", (activeId: string, body: string, callback) => {
// const file = sandboxFiles.fileData.find((f) => f.id === fileId)
// if (!file) return
// console.log("file " + file.id + ": ", file.data)
// callback(file.data)
// todo: send diffs + debounce for efficiency
socket.on("saveFile", async (fileId: string, body: string) => {
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
file.data = body
console.log("save file " + file.id + ": ", file.data)
await saveFile(fileId, body)
})
socket.on("renameFile", async (fileId: string, newName: string) => {
const file = sandboxFiles.fileData.find((f) => f.id === fileId)
if (!file) return
await renameFile(fileId, newName, file.data)
file.id = newName
await renameFile(fileId, newName, file.data)
})
})

View File

@ -50,7 +50,7 @@ const processFiles = async (paths: string[], id: string) => {
fileData.push({ id: path, data: "" })
} else {
const folder: TFolder = {
id: path, // issue todo: for example, folder "src" ID is: projects/a7vgttfqbgy403ratp7du3ln/src/App.css
id: path, // todo: wrong id. for example, folder "src" ID is: projects/a7vgttfqbgy403ratp7du3ln/src/App.css
type: "folder",
name: part,
children: [],
@ -104,3 +104,14 @@ export const renameFile = async (
})
return res.ok
}
export const saveFile = async (fileId: string, data: string) => {
const res = await fetch(`https://storage.ishaan1013.workers.dev/api/save`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ fileId, data }),
})
return res.ok
}