bigblocks
Version:
Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React
39 lines (38 loc) • 1.44 kB
JavaScript
// useFriends Hook - Fetch and manage friends list
import { useQuery } from "@tanstack/react-query";
import { useBitcoinAuth } from "../../../hooks/useBitcoinAuth.js";
import { API_ENDPOINTS, DEFAULT_SOCIAL_API_URL } from "../constants.js";
export function useFriends() {
const { user, isAuthenticated, config } = useBitcoinAuth();
const { data: friendData, isLoading, error, refetch, } = useQuery({
queryKey: ["friends", user?.idKey],
queryFn: async () => {
if (!user?.idKey) {
throw {
code: "UNAUTHORIZED",
message: "User must be authenticated",
};
}
const apiUrl = config?.apiUrl || DEFAULT_SOCIAL_API_URL;
const response = await fetch(`${apiUrl}${API_ENDPOINTS.FRIENDS(user.idKey)}`);
if (!response.ok) {
throw {
code: "NETWORK_ERROR",
message: "Failed to fetch friends",
};
}
return response.json();
},
enabled: isAuthenticated && !!user?.idKey,
staleTime: 5 * 60 * 1000, // 5 minutes
refetchOnWindowFocus: false,
});
return {
friends: friendData?.friends || [],
incoming: friendData?.incoming || [],
outgoing: friendData?.outgoing || [],
isLoading,
error: error || null,
refetch,
};
}