65 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-04-16 16:25:21 -04:00
import Navbar from "@/components/editor/navbar"
2024-05-03 14:27:45 -07:00
import { Room } from "@/components/editor/live/room"
2024-05-01 01:53:49 -04:00
import { Sandbox, User, UsersToSandboxes } from "@/lib/types"
2024-04-18 15:32:27 -04:00
import { currentUser } from "@clerk/nextjs"
import dynamic from "next/dynamic"
2024-04-26 22:34:56 -04:00
import { redirect } from "next/navigation"
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}`
)
2024-04-26 00:10:53 -04:00
const userData: User = await userRes.json()
return userData
}
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}`
)
2024-04-27 00:28:00 -04:00
const sandboxData: Sandbox = await sandboxRes.json()
return sandboxData
}
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}`
2024-05-01 01:53:49 -04:00
)
const userData: User = await userRes.json()
return { id: userData.id, name: userData.name }
})
)
return shared
}
2024-04-26 00:10:53 -04:00
export default async function CodePage({ params }: { params: { id: string } }) {
2024-04-18 15:32:27 -04:00
const user = await currentUser()
2024-04-26 00:10:53 -04:00
const sandboxId = params.id
2024-04-18 15:32:27 -04:00
if (!user) {
redirect("/")
}
2024-04-26 00:10:53 -04:00
const userData = await getUserData(user.id)
2024-04-27 00:28:00 -04:00
const sandboxData = await getSandboxData(sandboxId)
2024-05-01 01:53:49 -04:00
const shared = await getSharedUsers(sandboxData.usersToSandboxes)
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-03 14:27:45 -07:00
<Room id={sandboxId}>
<Navbar userData={userData} sandboxData={sandboxData} shared={shared} />
<div className="w-screen flex grow">
2024-05-05 00:06:10 -07:00
<CodeEditor userData={userData} sandboxData={sandboxData} />
2024-05-03 14:27:45 -07:00
</div>
</Room>
</div>
)
}