From e86f07ef342d6b475a5c6979f0367a40805c3597 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Sat, 16 May 2026 02:11:05 +0200 Subject: [PATCH] =?UTF-8?q?refactor(frontend):=20TopFriends=20self-contain?= =?UTF-8?q?ed=20=E2=80=94=20fetches=20own=20data,=20no=20N+1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- thoughts-frontend/components/top-friends.tsx | 42 ++++---------------- 1 file changed, 8 insertions(+), 34 deletions(-) diff --git a/thoughts-frontend/components/top-friends.tsx b/thoughts-frontend/components/top-friends.tsx index b7aed43..9d4f23a 100644 --- a/thoughts-frontend/components/top-friends.tsx +++ b/thoughts-frontend/components/top-friends.tsx @@ -1,51 +1,25 @@ import Link from "next/link"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { UserAvatar } from "./user-avatar"; -import { getUserProfile, User } from "@/lib/api"; +import { getTopFriends } from "@/lib/api"; import { cookies } from "next/headers"; interface TopFriendsProps { - mode: "friends" | "top-friends"; - usernames: string[]; + username: string; } -export async function TopFriends({ - mode = "top-friends", - usernames, -}: TopFriendsProps) { +export async function TopFriends({ username }: TopFriendsProps) { const token = (await cookies()).get("auth_token")?.value ?? null; + const data = await getTopFriends(username, token).catch(() => ({ topFriends: [] })); + const friends = data.topFriends; - if (usernames.length === 0) { - return ( - - - Top Friends - - -

- No top friends to display. -

-
-
- ); - } - - const friendsResults = await Promise.allSettled( - usernames.map((username) => getUserProfile(username, token)) - ); - - const friends = friendsResults - .filter( - (result): result is PromiseFulfilledResult => - result.status === "fulfilled" - ) - .map((result) => result.value); + if (friends.length === 0) return null; return ( - {mode === "top-friends" ? "Top Friends" : "Friends"} + Top Friends @@ -59,7 +33,7 @@ export async function TopFriends({ {friend.displayName || friend.username}