80 lines
2.3 KiB
TypeScript
Raw Normal View History

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 dynamic from "next/dynamic";
import { notFound, redirect } from "next/navigation";
import Loading from "@/components/editor/loading";
import { Suspense } from "react";
const CodeEditor = dynamic(() => import("@/components/editor"), {
ssr: false,
});
2024-04-26 00:10:53 -04:00
const getUserData = async (id: string) => {
const userRes = await fetch(
`https://database.ishaan1013.workers.dev/api/user?id=${id}`
);
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) => {
const sandboxRes = await fetch(
`https://database.ishaan1013.workers.dev/api/sandbox?id=${id}`
);
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[]) => {
const shared = await Promise.all(
usersToSandboxes.map(async (user) => {
const userRes = await fetch(
`https://database.ishaan1013.workers.dev/api/user?id=${user.userId}`
);
const userData: User = await userRes.json();
return { id: userData.id, name: userData.name };
2024-05-01 01:53:49 -04:00
})
);
2024-05-01 01:53:49 -04: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 } }) {
const user = await currentUser();
const sandboxId = params.id;
2024-04-18 15:32:27 -04:00
if (!user) {
redirect("/");
2024-04-18 15:32:27 -04: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
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">
<CodeEditor
userData={userData}
sandboxData={sandboxData}
2024-05-07 21:19:32 -07:00
isSharedUser={isSharedUser}
/>
2024-05-07 21:19:32 -07:00
</div>
</Room>
{/* </Suspense> */}
</div>
);
}