bigblocks
Version:
Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React
17 lines • 2.4 kB
JSON
{
"name": "social-hooks-usefetchpost",
"type": "registry:hook",
"dependencies": [
"@tanstack/react-query"
],
"devDependencies": [],
"registryDependencies": [],
"files": [
{
"path": "components/social/hooks/useFetchPost.ts",
"type": "registry:hook",
"content": "// useFetchPost Hook - Fetch a single post by transaction ID\nimport { useQuery } from \"@tanstack/react-query\";\nimport type {\n\tPostResponse,\n\tPostsResponse,\n\tRepliesParams,\n} from \"bmap-api-types\";\nimport { useApiClient } from \"./useApiClient.js\";\n\ninterface UseFetchPostOptions {\n\ttxid: string;\n\tenabled?: boolean;\n}\n\ninterface UseFetchPostResult {\n\tpost: PostResponse | null;\n\tisLoading: boolean;\n\terror: Error | null;\n\trefetch: () => void;\n}\n\nexport function useFetchPost({\n\ttxid,\n\tenabled = true,\n}: UseFetchPostOptions): UseFetchPostResult {\n\tconst apiClient = useApiClient();\n\n\tconst {\n\t\tdata: post = null,\n\t\tisLoading,\n\t\terror,\n\t\trefetch,\n\t} = useQuery<PostResponse | null, Error>({\n\t\tqueryKey: [\"post\", txid],\n\t\tqueryFn: async () => {\n\t\t\tif (!txid) return null;\n\t\t\treturn await apiClient.posts.single(txid);\n\t\t},\n\t\tenabled: enabled && !!txid,\n\t\tstaleTime: 1000 * 60 * 5, // 5 minutes\n\t});\n\n\treturn {\n\t\tpost,\n\t\tisLoading,\n\t\terror,\n\t\trefetch,\n\t};\n}\n\n// Hook for fetching post replies using RepliesParams\n\ninterface UseFetchRepliesOptions extends Partial<RepliesParams> {\n\tenabled?: boolean;\n}\n\ninterface UseFetchRepliesResult {\n\treplies: PostsResponse | null;\n\tisLoading: boolean;\n\terror: Error | null;\n\trefetch: () => void;\n}\n\nexport function useFetchReplies({\n\ttxid = \"\",\n\tpage = 1,\n\tlimit = 20,\n\tenabled = true,\n}: UseFetchRepliesOptions): UseFetchRepliesResult {\n\tconst apiClient = useApiClient();\n\n\tconst {\n\t\tdata: replies = null,\n\t\tisLoading,\n\t\terror,\n\t\trefetch,\n\t} = useQuery<PostsResponse | null, Error>({\n\t\tqueryKey: [\"replies\", txid, page, limit],\n\t\tqueryFn: async () => {\n\t\t\tif (!txid) return null;\n\t\t\treturn await apiClient.posts.replies(txid, page, limit);\n\t\t},\n\t\tenabled: enabled && !!txid,\n\t\tstaleTime: 1000 * 60 * 2, // 2 minutes\n\t});\n\n\treturn {\n\t\treplies,\n\t\tisLoading,\n\t\terror,\n\t\trefetch,\n\t};\n}\n",
"target": "<%- config.aliases.hooks %>/usefetchpost.ts"
}
]
}