UNPKG

bigblocks

Version:

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

151 lines (150 loc) 5.72 kB
// useSocialPost Hook - Create social posts on Bitcoin import { useMutation } from "@tanstack/react-query"; import { useBitcoinAuth } from "../../../hooks/useBitcoinAuth.js"; import { useBlockchainService } from "../../../hooks/useBlockchainService.js"; import { broadcastSocialTransaction } from "../../../lib/broadcast"; import { SocialActions } from "../../../lib/protocol"; export function useSocialPost(options = {}) { const { app: _app = "bigblocks", onSuccess, onError } = options; const { user } = useBitcoinAuth(); const _blockchainService = useBlockchainService(); const mutation = useMutation({ mutationFn: async ({ content, contentType: _contentType = "text/plain", }) => { if (!user) { throw { code: "UNAUTHORIZED", message: "User must be authenticated to post", }; } if (!content.trim()) { throw { code: "INVALID_CONTENT", message: "Post content cannot be empty", }; } if (content.length > 10000) { // Reasonable limit throw { code: "CONTENT_TOO_LONG", message: "Post content exceeds maximum length", }; } try { // Get signing key from user context // Note: This would need to be implemented in the auth system // For now, we'll assume it's available or throw an error const signingKey = user.signingKey; if (!signingKey) { throw { code: "UNAUTHORIZED", message: "No signing key available", }; } // Build transaction const signedTransaction = await SocialActions.post(content); // Broadcast to Bitcoin network using social transaction broadcaster // Note: SocialTransaction needs the specialized social broadcaster const result = await broadcastSocialTransaction(signedTransaction); if (!result.success) { throw { code: "BROADCAST_FAILED", message: result.error || "Failed to broadcast transaction", }; } return result; } catch (error) { if (error.code) { throw error; // Re-throw our custom errors } throw { code: "TRANSACTION_FAILED", message: "Failed to create post transaction", details: error, }; } }, onSuccess: (data) => { onSuccess?.(data); }, onError: (error) => { console.error("Social post failed:", error); onError?.(error); }, }); // Map React Query v5 properties to our interface return { mutate: mutation.mutate, mutateAsync: mutation.mutateAsync, isLoading: mutation.isPending, isError: mutation.isError, isSuccess: mutation.isSuccess, error: mutation.error, data: mutation.data, reset: mutation.reset, }; } // Reply hook - extends post functionality export function useReplyPost(options = {}) { const { app: _app = "bigblocks", onSuccess, onError } = options; const { user } = useBitcoinAuth(); const _replyMutation = useMutation({ mutationFn: async ({ content, contentType: _contentType = "text/plain", parentTxid, }) => { if (!user) { throw { code: "UNAUTHORIZED", message: "User must be authenticated to reply", }; } if (!content.trim()) { throw { code: "INVALID_CONTENT", message: "Reply content cannot be empty", }; } if (!parentTxid) { throw { code: "INVALID_CONTENT", message: "Parent transaction ID is required for replies", }; } try { const signingKey = user.signingKey; if (!signingKey) { throw { code: "UNAUTHORIZED", message: "No signing key available", }; } // Build reply transaction const signedTransaction = await SocialActions.reply(content, parentTxid); // Broadcast to Bitcoin network const result = await broadcastSocialTransaction(signedTransaction); if (!result.success) { throw { code: "BROADCAST_FAILED", message: result.error || "Failed to broadcast reply", }; } return result; } catch (error) { if (error.code) { throw error; } throw { code: "TRANSACTION_FAILED", message: "Failed to create reply transaction", details: error, }; } }, onSuccess: (data) => { onSuccess?.(data); }, onError: (error) => { console.error("Social reply failed:", error); onError?.(error); }, }); }