UNPKG

bigblocks

Version:

Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React

17 lines 4.75 kB
{ "name": "social-hooks-usefriendactions", "type": "registry:hook", "dependencies": [ "@tanstack/react-query" ], "devDependencies": [], "registryDependencies": [], "files": [ { "path": "components/social/hooks/useFriendActions.ts", "type": "registry:hook", "content": "// useFriendActions Hook - Accept/reject friend requests with real API calls\nimport { useMutation, useQueryClient } from \"@tanstack/react-query\";\nimport { useBitcoinAuth } from \"../../../hooks/useBitcoinAuth.js\";\nimport { ApiError } from \"../../../lib/api-error.js\";\nimport { broadcastSocialTransaction } from \"../../../lib/broadcast\";\nimport { SocialActions } from \"../../../lib/protocol.js\";\nimport type {\n\tAuthUserWithSigning,\n\tBroadcastResult,\n\tSocialError,\n} from \"../types/social.js\";\nimport { useApiClient } from \"./useApiClient.js\";\n\ninterface UseFriendActionsOptions {\n\tonSuccess?: (result: BroadcastResult) => void;\n\tonError?: (error: SocialError) => void;\n}\n\ninterface UseFriendActionsResult {\n\tacceptRequest: (bapId: string) => void;\n\trejectRequest: (bapId: string) => void;\n\tisLoading: boolean;\n\terror: SocialError | null;\n}\n\nexport function useFriendActions(\n\toptions: UseFriendActionsOptions = {},\n): UseFriendActionsResult {\n\tconst { onSuccess, onError } = options;\n\tconst { user } = useBitcoinAuth();\n\tconst queryClient = useQueryClient();\n\tconst apiClient = useApiClient();\n\n\tconst acceptMutation = useMutation<BroadcastResult, SocialError, string>({\n\t\tmutationFn: async (bapId: string) => {\n\t\t\tif (!user) {\n\t\t\t\tthrow ApiError.unauthorized(\"User must be authenticated\");\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\t// First try to use the API endpoint if available\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await apiClient.friends.accept(bapId);\n\t\t\t\t\treturn result;\n\t\t\t\t} catch (_apiError) {\n\t\t\t\t\t// If API call fails, fallback to direct transaction\n\t\t\t\t}\n\n\t\t\t\t// Fallback to creating transaction directly\n\t\t\t\tconst signingKey = (user as AuthUserWithSigning).signingKey;\n\t\t\t\tif (!signingKey) {\n\t\t\t\t\tthrow ApiError.unauthorized(\"No signing key available\");\n\t\t\t\t}\n\n\t\t\t\t// Create friend acceptance transaction\n\t\t\t\tconst signedTransaction = (await SocialActions.acceptFriend?.(bapId)) ||\n\t\t\t\t\tSocialActions.friend?.(bapId) || {\n\t\t\t\t\t\ttype: \"friend\",\n\t\t\t\t\t\tcontent: bapId,\n\t\t\t\t\t\tapp: \"bigblocks\",\n\t\t\t\t\t};\n\n\t\t\t\tconst result = await broadcastSocialTransaction(signedTransaction);\n\n\t\t\t\tif (!result.success) {\n\t\t\t\t\tthrow ApiError.broadcastFailed(\n\t\t\t\t\t\tresult.error || \"Failed to accept friend request\",\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\treturn result;\n\t\t\t} catch (error) {\n\t\t\t\tif (ApiError.isApiError(error)) {\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\n\t\t\t\tthrow ApiError.transactionFailed(\"Failed to accept friend request\", {\n\t\t\t\t\toriginalError: error,\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t\tonSuccess: (data) => {\n\t\t\tqueryClient.invalidateQueries({ queryKey: [\"friends\"] });\n\t\t\tonSuccess?.(data);\n\t\t},\n\t\tonError: (error) => {\n\t\t\tconsole.error(\"Accept friend request failed:\", error);\n\t\t\tonError?.(error);\n\t\t},\n\t});\n\n\tconst rejectMutation = useMutation<BroadcastResult, SocialError, string>({\n\t\tmutationFn: async (bapId: string) => {\n\t\t\tif (!user) {\n\t\t\t\tthrow ApiError.unauthorized(\"User must be authenticated\");\n\t\t\t}\n\n\t\t\ttry {\n\t\t\t\t// First try to use the API endpoint if available\n\t\t\t\ttry {\n\t\t\t\t\tconst result = await apiClient.friends.reject(bapId);\n\t\t\t\t\treturn result;\n\t\t\t\t} catch (_apiError) {\n\t\t\t\t\t// If API call fails, fallback to local rejection\n\t\t\t\t}\n\n\t\t\t\t// For rejection, we might just ignore the request locally\n\t\t\t\t// since blockchain transactions are immutable\n\t\t\t\treturn {\n\t\t\t\t\tsuccess: true,\n\t\t\t\t\ttxid: `reject-${bapId}-${Date.now()}`,\n\t\t\t\t};\n\t\t\t} catch (error) {\n\t\t\t\tthrow ApiError.networkError(\"Failed to reject friend request\", {\n\t\t\t\t\toriginalError: error,\n\t\t\t\t});\n\t\t\t}\n\t\t},\n\t\tonSuccess: (data) => {\n\t\t\tqueryClient.invalidateQueries({ queryKey: [\"friends\"] });\n\t\t\tonSuccess?.(data);\n\t\t},\n\t\tonError: (error) => {\n\t\t\tconsole.error(\"Reject friend request failed:\", error);\n\t\t\tonError?.(error);\n\t\t},\n\t});\n\n\treturn {\n\t\tacceptRequest: acceptMutation.mutate,\n\t\trejectRequest: rejectMutation.mutate,\n\t\tisLoading: acceptMutation.isPending || rejectMutation.isPending,\n\t\terror: acceptMutation.error || rejectMutation.error,\n\t};\n}\n", "target": "<%- config.aliases.hooks %>/usefriendactions.ts" } ] }