import type { Metadata } from "next"; import { cookies } from "next/headers"; import { getFeed, getFriends, getMe, getTopFriends, getUserProfile, Me, User, } from "@/lib/api"; import { PostThoughtForm } from "@/components/post-thought-form"; import { Button } from "@/components/ui/button"; import Link from "next/link"; import { PopularTags } from "@/components/popular-tags"; import { ThoughtThread } from "@/components/thought-thread"; import { buildThoughtThreads } from "@/lib/utils"; import { TopFriends } from "@/components/top-friends"; import { UsersCount } from "@/components/users-count"; import { PaginationNav } from "@/components/pagination-nav"; import { redirect } from "next/navigation"; export const metadata: Metadata = { title: "Home", description: "Your home timeline — thoughts from people you follow", }; export default async function Home({ searchParams, }: { searchParams: Promise<{ page?: string }>; }) { const token = (await cookies()).get("auth_token")?.value ?? null; const resolvedSearchParams = await searchParams; if (token) { return ; } else { return ; } } async function FeedPage({ token, searchParams, }: { token: string; searchParams: { page?: string }; }) { const page = parseInt(searchParams.page ?? "1", 10); const [feedData, me] = await Promise.all([ getFeed(token, page).catch(() => null), getMe(token).catch(() => null) as Promise, ]); if (!feedData || !me) { redirect("/login"); } const { items: allThoughts, totalPages } = feedData!; const thoughtThreads = buildThoughtThreads(allThoughts); const authors = [...new Set(allThoughts.map((t) => t.author.username))]; const userProfiles = await Promise.all( authors.map((username) => getUserProfile(username, token).catch(() => null)) ); const authorDetails = new Map( userProfiles .filter((u): u is User => !!u) .map((user) => [user.username, { avatarUrl: user.avatarUrl }]) ); const friends = (await getFriends(token)).users.map((user) => user.username); const topFriendsData = me ? await getTopFriends(me.username, token).catch(() => ({ topFriends: [] })) : { topFriends: [] }; const shouldDisplayTopFriends = topFriendsData.topFriends.length > 0; return (

Your Feed

{shouldDisplayTopFriends && ( )} {!shouldDisplayTopFriends && token && friends.length > 0 && ( )}
{thoughtThreads.map((thought) => ( ))} {thoughtThreads.length === 0 && (

Your feed is empty. Follow some users to see their thoughts!

)}
`/?page=${p}`} />
); } function LandingPage() { return ( <>

Welcome to Thoughts

Throwback to the golden age of microblogging.

); }