nostr-hooks
Version:
React hooks for developing Nostr clients
60 lines • 2.22 kB
JavaScript
import { useEffect } from 'react';
import { useSubscription } from '../../../hooks';
import { useNip29Store } from '../../../nip29/store';
const addGroupThread = useNip29Store.getState().addGroupThread;
export const useGroupThreads = (relay, groupId, filter) => {
const subId = relay && groupId ? `${relay}-${groupId}-threads-${JSON.stringify(filter)}` : undefined;
const threads = useNip29Store((state) => subId && groupId ? state.groups[subId]?.[groupId]?.threads : undefined);
const { events, hasMore, isLoading, createSubscription, loadMore } = useSubscription(subId);
useEffect(() => {
if (!relay || !groupId || !subId)
return;
if (filter?.byPubkey?.waitForPubkey && !filter.byPubkey.pubkey)
return;
if (filter?.byId?.waitForId && !filter.byId.id)
return;
let f = { kinds: [11], limit: filter?.limit || 10 };
if (groupId)
f['#h'] = [groupId];
if (filter?.byPubkey?.pubkey)
f.authors = [filter?.byPubkey?.pubkey];
if (filter?.byId?.id)
f.ids = [filter?.byId?.id];
if (filter?.since)
f.since = filter.since;
if (filter?.until)
f.until = filter.until;
const filters = [f];
const relayUrls = [relay];
const onEvent = (event) => {
const thread = {
id: event.id,
pubkey: event.pubkey,
content: event.content,
subject: event.getMatchingTags('subject')?.[0]?.[1] || '',
timestamp: event.created_at || 0,
};
addGroupThread(subId, groupId, thread);
};
createSubscription({ filters, relayUrls, onEvent });
}, [
subId,
relay,
groupId,
filter?.byPubkey?.pubkey,
filter?.byPubkey?.waitForPubkey,
filter?.byId?.id,
filter?.byId?.waitForId,
filter?.since,
filter?.until,
createSubscription,
]);
return {
threads,
isLoadingThreads: isLoading,
hasMoreThreads: hasMore,
loadMoreThreads: loadMore,
threadsEvents: events,
};
};
//# sourceMappingURL=index.js.map