84 lines
2.3 KiB
TypeScript
Raw Normal View History

"use client";
2024-04-06 19:03:04 -04:00
import dynamic from "next/dynamic";
import Loading from "@/components/editor/loading";
import { Sandbox, User } from "@/lib/types";
import { useEffect, useState } from "react";
import { toast } from "sonner";
2024-05-23 01:35:08 -07:00
import { getTaskIp, startServer } from "@/lib/actions";
import { checkServiceStatus } from "@/lib/utils";
2024-05-03 13:53:21 -07:00
const CodeEditor = dynamic(() => import("@/components/editor/editor"), {
ssr: false,
loading: () => <Loading />,
});
2024-04-06 19:03:04 -04:00
export default function Editor({
2024-05-23 01:35:08 -07:00
isOwner,
2024-05-01 01:53:49 -04:00
userData,
2024-05-05 00:06:10 -07:00
sandboxData,
}: {
2024-05-23 01:35:08 -07:00
isOwner: boolean;
userData: User;
sandboxData: Sandbox;
2024-04-26 22:34:56 -04:00
}) {
2024-05-23 01:35:08 -07:00
const [isServiceRunning, setIsServiceRunning] = useState(false);
const [isDeploymentActive, setIsDeploymentActive] = useState(false);
const [taskIp, setTaskIp] = useState<string>();
const [didFail, setDidFail] = useState(false);
useEffect(() => {
2024-05-23 01:35:08 -07:00
if (!isOwner) {
toast.error("You are not the owner of this sandbox. (TEMPORARY)");
setDidFail(true);
return;
}
2024-05-25 20:13:31 -07:00
startServer(sandboxData.id).then((response) => {
if (!response.success) {
toast.error(response.message);
setDidFail(true);
} else {
setIsServiceRunning(true);
2024-05-23 01:35:08 -07:00
2024-05-25 20:13:31 -07:00
checkServiceStatus(sandboxData.id)
.then(() => {
setIsDeploymentActive(true);
2024-05-23 01:35:08 -07:00
2024-05-25 20:13:31 -07:00
getTaskIp(sandboxData.id)
.then((ip) => {
setTaskIp(ip);
})
.catch(() => {
setDidFail(true);
toast.error("An error occurred while getting your server IP.");
});
})
.catch(() => {
toast.error("An error occurred while initializing your server.");
setDidFail(true);
});
}
});
}, []);
2024-05-25 20:13:31 -07:00
if (didFail) return <Loading didFail={didFail} />;
if (!isServiceRunning || !isDeploymentActive || !taskIp)
return (
<Loading
text="Creating sandbox resources"
description={
isDeploymentActive
? "Preparing server networking..."
: isServiceRunning
? "Initializing server, this could take a minute..."
: "Requesting your server creation..."
}
/>
);
2024-05-07 21:19:32 -07:00
2024-05-23 01:35:08 -07:00
return (
2024-05-25 20:13:31 -07:00
<CodeEditor ip={taskIp} userData={userData} sandboxData={sandboxData} />
2024-05-23 01:35:08 -07:00
);
2024-04-06 19:03:04 -04:00
}