108 lines
2.9 KiB
TypeScript
Raw Normal View History

// import { Room } from "@/components/editor/live/room"
2024-10-21 13:57:45 -06:00
import Loading from "@/components/editor/loading"
import Navbar from "@/components/editor/navbar"
import { TerminalProvider } from "@/context/TerminalContext"
2024-05-26 12:18:09 -07:00
import { Sandbox, User, UsersToSandboxes } from "@/lib/types"
import { currentUser } from "@clerk/nextjs"
2024-05-26 17:28:52 -07:00
import dynamic from "next/dynamic"
2024-10-21 13:57:45 -06:00
import { notFound, redirect } from "next/navigation"
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()
2024-11-17 12:35:56 -05:00
return {
id: userData.id,
name: userData.name,
avatarUrl: userData.avatarUrl,
}
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 (
<TerminalProvider>
{/* <Room id={sandboxId}> */}
<div className="overflow-hidden overscroll-none w-screen h-screen grid [grid-template-rows:3.5rem_auto] bg-background">
<Navbar
userData={userData}
sandboxData={sandboxData}
shared={shared as { id: string; name: string; avatarUrl: string }[]}
/>
<CodeEditor userData={userData} sandboxData={sandboxData} />
2024-10-21 13:57:45 -06:00
</div>
{/* </Room> */}
</TerminalProvider>
2024-05-26 12:18:09 -07:00
)
}