2024-05-06 22:59:49 -07:00
|
|
|
import Navbar from "@/components/editor/navbar";
|
|
|
|
import { Room } from "@/components/editor/live/room";
|
|
|
|
import { Sandbox, User, UsersToSandboxes } from "@/lib/types";
|
|
|
|
import { currentUser } from "@clerk/nextjs";
|
|
|
|
import { notFound, redirect } from "next/navigation";
|
2024-05-12 02:10:31 -07:00
|
|
|
import Editor from "@/components/editor";
|
2024-04-09 00:26:22 -04:00
|
|
|
|
2024-04-26 00:10:53 -04:00
|
|
|
const getUserData = async (id: string) => {
|
2024-05-05 22:33:24 -07:00
|
|
|
const userRes = await fetch(
|
|
|
|
`https://database.ishaan1013.workers.dev/api/user?id=${id}`
|
2024-05-06 22:59:49 -07:00
|
|
|
);
|
|
|
|
const userData: User = await userRes.json();
|
|
|
|
return userData;
|
|
|
|
};
|
2024-04-26 00:10:53 -04:00
|
|
|
|
2024-04-27 00:28:00 -04:00
|
|
|
const getSandboxData = async (id: string) => {
|
2024-05-05 22:33:24 -07:00
|
|
|
const sandboxRes = await fetch(
|
|
|
|
`https://database.ishaan1013.workers.dev/api/sandbox?id=${id}`
|
2024-05-06 22:59:49 -07:00
|
|
|
);
|
|
|
|
const sandboxData: Sandbox = await sandboxRes.json();
|
|
|
|
return sandboxData;
|
|
|
|
};
|
2024-04-27 00:28:00 -04:00
|
|
|
|
2024-05-01 01:53:49 -04:00
|
|
|
const getSharedUsers = async (usersToSandboxes: UsersToSandboxes[]) => {
|
2024-05-13 13:22:11 -07:00
|
|
|
if (!usersToSandboxes) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-05-01 01:53:49 -04:00
|
|
|
const shared = await Promise.all(
|
|
|
|
usersToSandboxes.map(async (user) => {
|
|
|
|
const userRes = await fetch(
|
2024-05-05 22:33:24 -07:00
|
|
|
`https://database.ishaan1013.workers.dev/api/user?id=${user.userId}`
|
2024-05-06 22:59:49 -07:00
|
|
|
);
|
|
|
|
const userData: User = await userRes.json();
|
|
|
|
return { id: userData.id, name: userData.name };
|
2024-05-01 01:53:49 -04:00
|
|
|
})
|
2024-05-06 22:59:49 -07:00
|
|
|
);
|
2024-05-01 01:53:49 -04:00
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
return shared;
|
|
|
|
};
|
2024-05-01 01:53:49 -04:00
|
|
|
|
2024-04-26 00:10:53 -04:00
|
|
|
export default async function CodePage({ params }: { params: { id: string } }) {
|
2024-05-06 22:59:49 -07:00
|
|
|
const user = await currentUser();
|
|
|
|
const sandboxId = params.id;
|
2024-04-18 15:32:27 -04:00
|
|
|
|
|
|
|
if (!user) {
|
2024-05-06 22:59:49 -07:00
|
|
|
redirect("/");
|
2024-04-18 15:32:27 -04:00
|
|
|
}
|
|
|
|
|
2024-05-06 22:59:49 -07:00
|
|
|
const userData = await getUserData(user.id);
|
|
|
|
const sandboxData = await getSandboxData(sandboxId);
|
|
|
|
const shared = await getSharedUsers(sandboxData.usersToSandboxes);
|
|
|
|
|
|
|
|
const isOwner = sandboxData.userId === user.id;
|
|
|
|
const isSharedUser = shared.some((uts) => uts.id === user.id);
|
|
|
|
|
|
|
|
if (!isOwner && !isSharedUser) {
|
|
|
|
return notFound();
|
|
|
|
}
|
2024-04-11 03:34:58 -04:00
|
|
|
|
2024-04-09 00:26:22 -04:00
|
|
|
return (
|
2024-04-09 00:50:48 -04:00
|
|
|
<div className="overflow-hidden overscroll-none w-screen flex flex-col h-screen bg-background">
|
2024-05-07 21:19:32 -07:00
|
|
|
{/* <Suspense fallback={<Loading />}> */}
|
|
|
|
<Room id={sandboxId}>
|
|
|
|
<Navbar userData={userData} sandboxData={sandboxData} shared={shared} />
|
|
|
|
<div className="w-screen flex grow">
|
2024-05-12 02:10:31 -07:00
|
|
|
<Editor userData={userData} sandboxData={sandboxData} />
|
2024-05-07 21:19:32 -07:00
|
|
|
</div>
|
|
|
|
</Room>
|
|
|
|
{/* </Suspense> */}
|
2024-04-09 00:26:22 -04:00
|
|
|
</div>
|
2024-05-06 22:59:49 -07:00
|
|
|
);
|
2024-04-09 00:26:22 -04:00
|
|
|
}
|