157 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-05-26 12:18:09 -07:00
import { type ClassValue, clsx } from "clsx"
2024-04-30 01:56:43 -04:00
// import { toast } from "sonner"
2024-05-26 12:18:09 -07:00
import { twMerge } from "tailwind-merge"
import { Sandbox, TFile, TFolder } from "./types"
import { Service } from "@aws-sdk/client-ecs"
import {
describeService,
doesServiceExist,
getTaskIp,
startServer,
} from "./actions"
2024-04-06 19:03:04 -04:00
export function cn(...inputs: ClassValue[]) {
2024-05-26 12:18:09 -07:00
return twMerge(clsx(inputs))
2024-04-06 19:03:04 -04:00
}
2024-04-27 00:28:00 -04:00
export function processFileType(file: string) {
2024-05-26 12:18:09 -07:00
const ending = file.split(".").pop()
2024-04-27 00:28:00 -04:00
2024-05-26 12:18:09 -07:00
if (ending === "ts" || ending === "tsx") return "typescript"
if (ending === "js" || ending === "jsx") return "javascript"
2024-04-27 00:28:00 -04:00
2024-05-26 12:18:09 -07:00
if (ending) return ending
return "plaintext"
2024-04-27 00:28:00 -04:00
}
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) {
2024-05-26 12:18:09 -07:00
return { status: false, message: "" }
2024-05-05 12:55:34 -07:00
}
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-26 12:18:09 -07:00
return { status: false, message: "Invalid file name." }
2024-04-29 00:50:25 -04:00
}
2024-05-26 12:18:09 -07:00
return { status: true, message: "" }
2024-04-29 00:50:25 -04:00
}
2024-05-11 18:03:42 -07:00
2024-05-13 22:04:00 -07:00
export function addNew(
name: string,
type: "file" | "folder",
setFiles: React.Dispatch<React.SetStateAction<(TFolder | TFile)[]>>,
sandboxData: Sandbox
) {
2024-05-11 18:03:42 -07:00
if (type === "file") {
setFiles((prev) => [
...prev,
{ id: `projects/${sandboxData.id}/${name}`, name, type: "file" },
2024-05-26 12:18:09 -07:00
])
2024-05-11 18:03:42 -07:00
} else {
2024-05-26 12:18:09 -07:00
console.log("adding folder")
2024-05-13 22:04:00 -07:00
setFiles((prev) => [
...prev,
{
id: `projects/${sandboxData.id}/${name}`,
name,
type: "folder",
children: [],
},
2024-05-26 12:18:09 -07:00
])
2024-05-11 18:03:42 -07:00
}
}
2024-05-23 01:35:08 -07:00
export function checkServiceStatus(serviceName: string): Promise<Service> {
2024-05-21 00:57:52 -07:00
return new Promise((resolve, reject) => {
2024-05-26 12:18:09 -07:00
let tries = 0
2024-05-21 00:57:52 -07:00
const interval = setInterval(async () => {
try {
2024-05-26 12:18:09 -07:00
tries++
2024-05-21 00:57:52 -07:00
2024-05-23 01:35:08 -07:00
if (tries > 40) {
2024-05-26 12:18:09 -07:00
clearInterval(interval)
reject(new Error("Timed out."))
2024-05-23 01:35:08 -07:00
}
2024-05-26 12:18:09 -07:00
const response = await describeService(serviceName)
2024-05-23 01:35:08 -07:00
const activeServices = response.services?.filter(
(service) => service.status === "ACTIVE"
2024-05-26 12:18:09 -07:00
)
console.log("Checking activeServices status", activeServices)
2024-05-23 01:35:08 -07:00
if (activeServices?.length === 1) {
2024-05-26 12:18:09 -07:00
const service = activeServices?.[0]
2024-05-21 00:57:52 -07:00
if (
service.runningCount === service.desiredCount &&
2024-05-23 01:35:08 -07:00
service.deployments?.length === 1
2024-05-21 00:57:52 -07:00
) {
2024-05-23 01:35:08 -07:00
if (service.deployments[0].rolloutState === "COMPLETED") {
2024-05-26 12:18:09 -07:00
clearInterval(interval)
resolve(service)
2024-05-23 01:35:08 -07:00
} else if (service.deployments[0].rolloutState === "FAILED") {
2024-05-26 12:18:09 -07:00
clearInterval(interval)
reject(new Error("Deployment failed."))
2024-05-23 01:35:08 -07:00
}
2024-05-21 00:57:52 -07:00
}
}
2024-05-23 01:35:08 -07:00
} catch (error: any) {
2024-05-26 12:18:09 -07:00
clearInterval(interval)
reject(error)
2024-05-21 00:57:52 -07:00
}
2024-05-26 12:18:09 -07:00
}, 3000)
})
}
export async function setupServer({
sandboxId,
setIsServiceRunning,
setIsDeploymentActive,
setTaskIp,
setDidFail,
toast,
}: {
sandboxId: string
setIsServiceRunning: React.Dispatch<React.SetStateAction<boolean>>
setIsDeploymentActive: React.Dispatch<React.SetStateAction<boolean>>
setTaskIp: React.Dispatch<React.SetStateAction<string | undefined>>
setDidFail: React.Dispatch<React.SetStateAction<boolean>>
toast: any
}) {
const doesExist = await doesServiceExist(sandboxId)
if (!doesExist) {
const response = await startServer(sandboxId)
if (!response.success) {
toast.error(response.message)
setDidFail(true)
return
}
}
setIsServiceRunning(true)
try {
if (!doesExist) {
await checkServiceStatus(sandboxId)
}
setIsDeploymentActive(true)
const taskIp = await getTaskIp(sandboxId)
setTaskIp(taskIp)
} catch (error) {
toast.error("An error occurred while initializing your server.")
setDidFail(true)
}
2024-05-23 01:35:08 -07:00
}