bigblocks
Version:
Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React
108 lines (107 loc) • 3.39 kB
JavaScript
import { useQuery } from "@tanstack/react-query";
import { useState } from "react";
import { useApiClient } from "./useApiClient.js";
export function useSocialFeed({ bapId, feedType = "search", limit = 20, page = 0, enabled = true, searchQuery = "bitcoin", address, } = {}) {
const apiClient = useApiClient();
const queryKey = [
"socialFeed",
feedType,
bapId,
page,
limit,
searchQuery,
address,
];
const { data, isLoading, error, refetch } = useQuery({
queryKey,
queryFn: async () => {
switch (feedType) {
case "user":
if (!bapId) {
throw new Error("bapId is required for user feed type");
}
// API client expects 1-based page numbers
return await apiClient.posts.byBapId({
bapId,
page: page + 1,
limit,
});
case "address":
if (!address) {
throw new Error("address is required for address feed type");
}
// API client expects 1-based page numbers
return await apiClient.posts.byAddress({
address,
page: page + 1,
limit,
});
default:
if (!searchQuery) {
throw new Error("searchQuery is required for search feed type");
}
return await apiClient.posts.search({
q: searchQuery,
limit,
offset: page * limit,
});
}
},
enabled,
staleTime: 1000 * 60 * 5,
refetchInterval: 1000 * 60 * 2,
});
return {
data,
isLoading,
error,
refetch,
hasError: !!error,
};
}
export function usePostSearch() {
const apiClient = useApiClient();
const [searchResults, setSearchResults] = useState(null);
const [isSearching, setIsSearching] = useState(false);
const [searchError, setSearchError] = useState(null);
const searchPosts = async (query) => {
if (!query.trim()) {
setSearchResults(null);
return {
page: 0,
limit: 0,
count: 0,
results: [],
signers: [],
meta: [],
};
}
setIsSearching(true);
setSearchError(null);
try {
const postsResponse = await apiClient.posts.search({
q: query,
limit: 20,
offset: 0,
});
setSearchResults(postsResponse);
return postsResponse;
}
catch (error) {
const errorObj = error instanceof Error ? error : new Error("Search failed");
setSearchError(errorObj);
setSearchResults(null);
throw errorObj;
}
finally {
setIsSearching(false);
}
};
return {
searchPosts,
isSearching,
searchResults,
searchError,
hasSearchError: !!searchError,
};
}