66 lines
2.0 KiB
TypeScript
Raw Permalink Normal View History

2024-11-11 22:02:34 +01:00
import ProfilePage from "@/components/profile"
import ProfileNavbar from "@/components/profile/navbar"
import { SandboxWithLiked, User } from "@/lib/types"
2024-11-11 22:02:34 +01:00
import { currentUser } from "@clerk/nextjs"
import { notFound } from "next/navigation"
2024-11-11 22:02:34 +01:00
export default async function Page({
params: { username: rawUsername },
}: {
params: { username: string }
}) {
const username = decodeURIComponent(rawUsername).replace("@", "")
const loggedInClerkUser = await currentUser()
const [profileOwnerResponse, loggedInUserResponse] = await Promise.all([
2024-11-11 22:02:34 +01:00
fetch(
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?username=${username}&currentUserId=${loggedInClerkUser?.id}`,
2024-11-11 22:02:34 +01:00
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
),
fetch(
`${process.env.NEXT_PUBLIC_DATABASE_WORKER_URL}/api/user?id=${loggedInClerkUser?.id}`,
2024-11-11 22:02:34 +01:00
{
headers: {
Authorization: `${process.env.NEXT_PUBLIC_WORKERS_KEY}`,
},
}
),
])
const profileOwner = (await profileOwnerResponse.json()) as User
const loggedInUser = (await loggedInUserResponse.json()) as User
if (!Boolean(profileOwner?.id)) {
notFound()
}
const publicSandboxes: SandboxWithLiked[] = []
const privateSandboxes: SandboxWithLiked[] = []
2024-11-11 22:02:34 +01:00
profileOwner?.sandbox?.forEach((sandbox) => {
2024-11-11 22:02:34 +01:00
if (sandbox.visibility === "public") {
publicSandboxes.push(sandbox as SandboxWithLiked)
2024-11-11 22:02:34 +01:00
} else if (sandbox.visibility === "private") {
privateSandboxes.push(sandbox as SandboxWithLiked)
2024-11-11 22:02:34 +01:00
}
})
const isUserLoggedIn = Boolean(loggedInUser?.id)
2024-11-11 22:02:34 +01:00
return (
<section>
<ProfileNavbar userData={loggedInUser} />
2024-11-11 22:02:34 +01:00
<ProfilePage
publicSandboxes={publicSandboxes}
privateSandboxes={
profileOwner?.id === loggedInUser.id ? privateSandboxes : []
2024-11-11 22:02:34 +01:00
}
profileOwner={profileOwner}
loggedInUser={isUserLoggedIn ? loggedInUser : null}
2024-11-11 22:02:34 +01:00
/>
</section>
2024-11-11 22:02:34 +01:00
)
}