import { notFound } from "next/navigation"; import { headers } from "next/headers"; import Link from "next/link"; import { auth } from "@/lib/auth/server"; import { db, users, recipes, userFollows, eq, and, desc, count, } from "@epicure/db"; import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar"; import { Badge } from "@/components/ui/badge"; import { FollowButton } from "@/components/social/follow-button"; import { getPublicUrl } from "@/lib/storage"; type Params = { params: Promise<{ username: string }> }; export async function generateMetadata({ params }: Params) { const { username } = await params; const user = await db.query.users.findFirst({ where: eq(users.username, username) }); return { title: user ? `${user.name} (@${user.username})` : "Profile" }; } export default async function UserProfilePage({ params }: Params) { const { username } = await params; const session = await auth.api.getSession({ headers: await headers() }); const user = await db.query.users.findFirst({ where: eq(users.username, username) }); if (!user) notFound(); const [followerCountRow, followingCountRow, recipeCountRow, publicRecipes] = await Promise.all([ db .select({ count: count() }) .from(userFollows) .where(eq(userFollows.followingId, user.id)), db .select({ count: count() }) .from(userFollows) .where(eq(userFollows.followerId, user.id)), db .select({ count: count() }) .from(recipes) .where(and(eq(recipes.authorId, user.id), eq(recipes.visibility, "public"))), db.query.recipes.findMany({ where: and(eq(recipes.authorId, user.id), eq(recipes.visibility, "public")), orderBy: desc(recipes.createdAt), limit: 24, with: { photos: { orderBy: (t, { asc }) => asc(t.order), limit: 1 }, }, }), ]); const followerCount = followerCountRow[0]?.count ?? 0; const followingCount = followingCountRow[0]?.count ?? 0; const recipeCount = recipeCountRow[0]?.count ?? 0; let isFollowing = false; const isOwnProfile = session?.user.id === user.id; if (session && !isOwnProfile) { const followRow = await db.query.userFollows.findFirst({ where: and( eq(userFollows.followerId, session.user.id), eq(userFollows.followingId, user.id), ), }); isFollowing = !!followRow; } const initials = user.name .split(" ") .slice(0, 2) .map((w) => w[0]) .join("") .toUpperCase(); return (
@{user.username}
{user.bio}
)}{recipe.title}
No public recipes yet.