Files
roblox/hooks/roblox/useFriends.ts
2025-12-27 16:57:19 +02:00

75 lines
1.9 KiB
TypeScript

"use client";
import { useQuery } from "@tanstack/react-query";
import { useCurrentAccount } from "./useCurrentAccount";
import { proxyFetch } from "@/lib/utils";
import { loadThumbnails } from "@/lib/thumbnailLoader";
import assert from "assert";
export function useFriendsHome(targetId?: string) {
const acct = useCurrentAccount();
const target = targetId || (acct ? acct.id : "acctId");
const { data: friends } = useQuery({
queryKey: ["friends", target],
queryFn: async () => {
if (target === "acctId") return [];
const friendsAPICall = await proxyFetch(
`https://friends.roblox.com/v1/users/${target}/friends`
);
assert(friendsAPICall.ok);
const j = (await friendsAPICall.json()) as {
data: { id: number }[];
};
const friendsAPICall2 = await proxyFetch(
`https://users.roblox.com/v1/users`,
{
method: "POST",
body: JSON.stringify({
userIds: j.data.map((a) => a.id),
excludeBannedUsers: true
})
}
);
assert(friendsAPICall2.ok);
const j2 = (await friendsAPICall2.json()) as {
data: {
hasVerifiedBadge: boolean;
id: number;
name: string;
displayName: string;
}[];
};
loadThumbnails(
j2.data.map((a) => ({
type: "AvatarHeadShot",
size: "420x420",
targetId: a.id,
format: "webp"
}))
).catch(() => {});
const friendsList = j.data
.map((a) => {
const x = j2.data.find((b) => b.id === a.id);
return !!x
? {
id: a.id,
hasVerifiedBadge: x?.hasVerifiedBadge || false,
name: x?.name || "?",
displayName: x?.displayName || "?"
}
: null;
})
.filter((a) => !!a)
.filter((a) => a.id.toString() !== "-1");
return friendsList;
},
enabled: !!acct,
staleTime: 300000, // 5 minutes
refetchOnWindowFocus: false,
refetchOnMount: false,
refetchOnReconnect: false
});
return friends;
}