import { getFollowersList, getFollowingList, getMe, getTopFriends, getUserProfile, getUserThoughts, Me, } from "@/lib/api"; import { UserAvatar } from "@/components/user-avatar"; import { Calendar, Settings } from "lucide-react"; import { Card } from "@/components/ui/card"; import { notFound } from "next/navigation"; import { cookies } from "next/headers"; import { FollowButton } from "@/components/follow-button"; import { TopFriends } from "@/components/top-friends"; import { buildThoughtThreads } from "@/lib/utils"; import { ThoughtThread } from "@/components/thought-thread"; import { Button } from "@/components/ui/button"; import Link from "next/link"; interface ProfilePageProps { params: Promise<{ username: string }>; } export default async function ProfilePage({ params }: ProfilePageProps) { const { username } = await params; const token = (await cookies()).get("auth_token")?.value ?? null; const userProfilePromise = getUserProfile(username, token); const thoughtsPromise = getUserThoughts(username, token); const mePromise = token ? getMe(token) : Promise.resolve(null); const followersPromise = getFollowersList(username, token); const followingPromise = getFollowingList(username, token); const [ userResult, thoughtsResult, meResult, followersResult, followingResult, ] = await Promise.allSettled([ userProfilePromise, thoughtsPromise, mePromise, followersPromise, followingPromise, ]); if (userResult.status === "rejected") { notFound(); } const user = userResult.value; const me = meResult.status === "fulfilled" ? (meResult.value as Me) : null; const thoughts = thoughtsResult.status === "fulfilled" ? thoughtsResult.value.items : []; const thoughtThreads = buildThoughtThreads(thoughts); const followersCount = followersResult.status === "fulfilled" ? followersResult.value.total : 0; const followingCount = followingResult.status === "fulfilled" ? followingResult.value.total : 0; const isOwnProfile = me?.username === user.username; const isFollowing = user.isFollowedByViewer; const authorDetails = new Map(); authorDetails.set(user.username, { avatarUrl: user.avatarUrl }); // Show who the profile owner follows (uses the already-fetched followingResult). const friends = followingResult.status === "fulfilled" ? followingResult.value.items.map((u) => u.username) : []; const topFriendsData = await getTopFriends(username, token).catch(() => ({ topFriends: [] })); const shouldDisplayTopFriends = topFriendsData.topFriends.length > 0; return (
{user.customCss && (