103 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-05-26 12:18:09 -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-26 17:28:52 -07:00
import Loading from "@/components/editor/loading"
import dynamic from "next/dynamic"
2024-05-26 21:41:20 -07:00
import fs from "fs"
2024-05-26 12:18:09 -07:00
export const revalidate = 0
2024-05-20 09:49:47 -07:00
2024-04-26 00:10:53 -04:00
const getUserData = async (id: string) => {
const userRes = await fetch(
2024-05-26 18:37:36 -07:00
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${id}`,
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
2024-05-26 12:18:09 -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) => {
const sandboxRes = await fetch(
2024-05-26 18:37:36 -07:00
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/sandbox?id=${id}`,
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
2024-05-26 12:18:09 -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) {
2024-05-26 12:18:09 -07:00
return []
2024-05-13 13:22:11 -07:00
}
2024-05-01 01:53:49 -04:00
const shared = await Promise.all(
usersToSandboxes.map(async (user) => {
const userRes = await fetch(
2024-05-26 18:37:36 -07:00
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${user.userId}`,
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
2024-05-26 12:18:09 -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-26 12:18:09 -07:00
)
2024-05-01 01:53:49 -04:00
2024-05-26 12:18:09 -07:00
return shared
}
2024-05-01 01:53:49 -04:00
2024-05-26 17:28:52 -07:00
const CodeEditor = dynamic(() => import("@/components/editor"), {
ssr: false,
loading: () => <Loading />,
})
2024-04-26 00:10:53 -04:00
export default async function CodePage({ params }: { params: { id: string } }) {
2024-05-26 12:18:09 -07:00
const user = await currentUser()
const sandboxId = params.id
2024-04-18 15:32:27 -04:00
if (!user) {
2024-05-26 12:18:09 -07:00
redirect("/")
2024-04-18 15:32:27 -04:00
}
2024-05-26 12:18:09 -07:00
const userData = await getUserData(user.id)
const sandboxData = await getSandboxData(sandboxId)
const shared = await getSharedUsers(sandboxData.usersToSandboxes)
2024-05-26 12:18:09 -07:00
const isOwner = sandboxData.userId === user.id
const isSharedUser = shared.some((uts) => uts.id === user.id)
if (!isOwner && !isSharedUser) {
2024-05-26 12:18:09 -07:00
return notFound()
}
2024-04-11 03:34:58 -04:00
2024-05-26 19:02:47 -07:00
if (isSharedUser && sandboxData.visibility === "private") {
return notFound()
}
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
<Room id={sandboxId}>
<Navbar userData={userData} sandboxData={sandboxData} shared={shared} />
<div className="w-screen flex grow">
2024-05-26 21:41:20 -07:00
<CodeEditor
userData={userData}
sandboxData={sandboxData}
/>
2024-05-07 21:19:32 -07:00
</div>
</Room>
</div>
2024-05-26 12:18:09 -07:00
)
}