UNPKG

bigblocks

Version:

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

67 lines (66 loc) 2.75 kB
"use client"; import { useQuery } from "@tanstack/react-query"; import { useApiClient } from "./useApiClient.js"; /** * Hook for fetching posts/comments in a specific context * * This enables displaying comments on albums, tracks, or any content * by providing a context type and value. * * @example * ```tsx * // Fetch comments for an album by transaction ID * const { posts } = useContextualPosts({ * contextType: 'tx', * contextValue: albumTxId * }); * * // Fetch comments for a URL * const { posts } = useContextualPosts({ * contextType: 'url', * contextValue: 'https://jamify.com/album/chromakopia' * }); * ``` */ export function useContextualPosts({ contextType, contextValue, page = 1, limit = 20, enabled = true, }) { const apiClient = useApiClient(); const { data: posts = null, isLoading, error, refetch, } = useQuery({ queryKey: ["contextual-posts", contextType, contextValue, page, limit], queryFn: async () => { if (!contextValue) return null; // The BMAP API doesn't have a direct endpoint for contextual posts yet, // so we'll use the search endpoint with context filtering // In a real implementation, you'd have an endpoint like: // GET /social/post/context/{type}/{value} // For now, we'll use the search endpoint and filter client-side // This is a placeholder - the actual API would need to support context queries const searchResults = await apiClient.posts.search({ q: contextValue, // SearchParams expects 'q' not 'query' offset: (page - 1) * limit, limit: limit * 2, // Get more to account for filtering }); if (!searchResults) return null; // Filter posts that have matching context // Note: This is a client-side filter - ideally the API would do this const filteredResults = searchResults.results.filter((post) => { const mapData = post.MAP?.[0]; if (!mapData) return false; // Check if the post has context data // The MAP protocol stores context as key-value pairs const contextKey = `context.${contextType}`; return mapData[contextKey] === contextValue; }); return { ...searchResults, results: filteredResults.slice(0, limit), count: filteredResults.length, }; }, enabled: enabled && !!contextValue, staleTime: 1000 * 60 * 2, // 2 minutes }); return { posts, isLoading, error: error, refetch }; }