Files
Fastchat/app/(auth)/friends/page.tsx
T

56 lines
1.7 KiB
TypeScript

import { createClient } from "@/utils/supabase/server";
import { listFriends, listRequests } from "@/utils/friends/friends";
import { AddFriendForm } from "./add-friend-form";
import { RequestsList, type FriendRequest } from "./requests-list";
import { FriendsList, type FriendEntry } from "./friends-list";
export default async function FriendsPage() {
const supabase = await createClient();
// Fetch incoming friend requests with sender names
const rawRequests = await listRequests();
let requests: FriendRequest[] = [];
if (rawRequests && rawRequests.length > 0) {
const senderIds = rawRequests.map((r) => r.sender_id);
const { data: senders } = await supabase
.from("users")
.select("id, name")
.in("id", senderIds);
const senderMap = new Map(senders?.map((s) => [s.id, s.name]));
requests = rawRequests.map((r) => ({
id: r.id,
sender_id: r.sender_id,
sender_name: senderMap.get(r.sender_id) ?? "Unknown",
}));
}
// Fetch friends list
const rawFriends = await listFriends();
const friends: FriendEntry[] = rawFriends ?? [];
return (
<div className="mx-auto max-w-3xl px-6 py-10">
<h1 className="text-2xl font-semibold text-neutral-900">Friends</h1>
<p className="mt-1 text-sm text-neutral-500">
Manage your friends and requests.
</p>
<div className="mt-8 space-y-8">
{/* Add friend */}
<div className="rounded-xl border border-neutral-200 bg-white p-5 shadow-sm">
<AddFriendForm />
</div>
{/* Incoming requests */}
<RequestsList requests={requests} />
{/* Friends list */}
<FriendsList friends={friends} />
</div>
</div>
);
}