bigblocks
Version:
Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React
106 lines (105 loc) • 4.24 kB
JavaScript
// useFriendActions Hook - Accept/reject friend requests with real API calls
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useBitcoinAuth } from "../../../hooks/useBitcoinAuth.js";
import { ApiError } from "../../../lib/api-error.js";
import { broadcastSocialTransaction } from "../../../lib/broadcast";
import { SocialActions } from "../../../lib/protocol.js";
import { useApiClient } from "./useApiClient.js";
export function useFriendActions(options = {}) {
const { onSuccess, onError } = options;
const { user } = useBitcoinAuth();
const queryClient = useQueryClient();
const apiClient = useApiClient();
const acceptMutation = useMutation({
mutationFn: async (bapId) => {
if (!user) {
throw ApiError.unauthorized("User must be authenticated");
}
try {
// First try to use the API endpoint if available
try {
const result = await apiClient.friends.accept(bapId);
return result;
}
catch (_apiError) {
// If API call fails, fallback to direct transaction
}
// Fallback to creating transaction directly
const signingKey = user.signingKey;
if (!signingKey) {
throw ApiError.unauthorized("No signing key available");
}
// Create friend acceptance transaction
const signedTransaction = (await SocialActions.acceptFriend?.(bapId)) ||
SocialActions.friend?.(bapId) || {
type: "friend",
content: bapId,
app: "bigblocks",
};
const result = await broadcastSocialTransaction(signedTransaction);
if (!result.success) {
throw ApiError.broadcastFailed(result.error || "Failed to accept friend request");
}
return result;
}
catch (error) {
if (ApiError.isApiError(error)) {
throw error;
}
throw ApiError.transactionFailed("Failed to accept friend request", {
originalError: error,
});
}
},
onSuccess: (data) => {
queryClient.invalidateQueries({ queryKey: ["friends"] });
onSuccess?.(data);
},
onError: (error) => {
console.error("Accept friend request failed:", error);
onError?.(error);
},
});
const rejectMutation = useMutation({
mutationFn: async (bapId) => {
if (!user) {
throw ApiError.unauthorized("User must be authenticated");
}
try {
// First try to use the API endpoint if available
try {
const result = await apiClient.friends.reject(bapId);
return result;
}
catch (_apiError) {
// If API call fails, fallback to local rejection
}
// For rejection, we might just ignore the request locally
// since blockchain transactions are immutable
return {
success: true,
txid: `reject-${bapId}-${Date.now()}`,
};
}
catch (error) {
throw ApiError.networkError("Failed to reject friend request", {
originalError: error,
});
}
},
onSuccess: (data) => {
queryClient.invalidateQueries({ queryKey: ["friends"] });
onSuccess?.(data);
},
onError: (error) => {
console.error("Reject friend request failed:", error);
onError?.(error);
},
});
return {
acceptRequest: acceptMutation.mutate,
rejectRequest: rejectMutation.mutate,
isLoading: acceptMutation.isPending || rejectMutation.isPending,
error: acceptMutation.error || rejectMutation.error,
};
}