2024-04-06 19:03:04 -04:00
|
|
|
import { type ClassValue, clsx } from "clsx"
|
2024-04-30 01:56:43 -04:00
|
|
|
// import { toast } from "sonner"
|
2024-04-06 19:03:04 -04:00
|
|
|
import { twMerge } from "tailwind-merge"
|
2024-05-11 18:03:42 -07:00
|
|
|
import { Sandbox, TFile, TFolder } from "./types"
|
2024-04-06 19:03:04 -04:00
|
|
|
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
|
|
return twMerge(clsx(inputs))
|
|
|
|
}
|
2024-04-27 00:28:00 -04:00
|
|
|
|
|
|
|
export function processFileType(file: string) {
|
|
|
|
const ending = file.split(".").pop()
|
|
|
|
|
|
|
|
if (ending === "ts" || ending === "tsx") return "typescript"
|
|
|
|
if (ending === "js" || ending === "jsx") return "javascript"
|
|
|
|
|
|
|
|
if (ending) return ending
|
|
|
|
return "plaintext"
|
|
|
|
}
|
2024-04-28 20:06:47 -04:00
|
|
|
|
2024-04-29 00:50:25 -04:00
|
|
|
export function validateName(
|
|
|
|
newName: string,
|
|
|
|
oldName: string,
|
|
|
|
type: "file" | "folder"
|
|
|
|
) {
|
2024-05-05 12:55:34 -07:00
|
|
|
if (newName === oldName || newName.length === 0) {
|
|
|
|
return { status: false, message: "" }
|
|
|
|
}
|
2024-04-29 00:50:25 -04:00
|
|
|
if (
|
|
|
|
newName.includes("/") ||
|
|
|
|
newName.includes("\\") ||
|
|
|
|
newName.includes(" ") ||
|
|
|
|
(type === "file" && !newName.includes(".")) ||
|
|
|
|
(type === "folder" && newName.includes("."))
|
|
|
|
) {
|
2024-05-05 12:55:34 -07:00
|
|
|
return { status: false, message: "Invalid file name." }
|
2024-04-29 00:50:25 -04:00
|
|
|
}
|
2024-05-05 12:55:34 -07:00
|
|
|
return { status: true, message: "" }
|
2024-04-29 00:50:25 -04:00
|
|
|
}
|
2024-05-11 18:03:42 -07:00
|
|
|
|
|
|
|
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: [] }])
|
|
|
|
}
|
2024-05-12 02:10:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function startServer(sandboxId: string, userId: string, callback: (success: boolean) => void) {
|
|
|
|
try {
|
|
|
|
await fetch("http://localhost:4001/start", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
sandboxId,
|
|
|
|
userId
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
|
|
|
|
callback(true)
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to start server", error)
|
|
|
|
|
|
|
|
callback(false)
|
|
|
|
}
|
|
|
|
|
2024-05-11 18:03:42 -07:00
|
|
|
}
|