refactor: RequestBuilder

This commit is contained in:
2023-03-28 15:34:01 +01:00
parent 1bf6c7031e
commit 465c59ea20
77 changed files with 3141 additions and 2343 deletions

View File

@ -1,9 +1,10 @@
import { useMemo } from "react";
import { useSelector } from "react-redux";
import { HexKey, TaggedRawEvent, EventKind, Subscriptions } from "@snort/nostr";
import { HexKey, TaggedRawEvent, EventKind } from "@snort/nostr";
import useSubscription from "Feed/Subscription";
import { RootState } from "State/Store";
import { PubkeyReplaceableNoteStore, RequestBuilder } from "System";
import useRequestBuilder from "Hooks/useRequestBuilder";
export default function useFollowsFeed(pubkey?: HexKey) {
const { publicKey, follows } = useSelector((s: RootState) => s.login);
@ -11,24 +12,22 @@ export default function useFollowsFeed(pubkey?: HexKey) {
const sub = useMemo(() => {
if (isMe || !pubkey) return null;
const x = new Subscriptions();
x.Id = `follows:${pubkey.slice(0, 12)}`;
x.Kinds = new Set([EventKind.ContactList]);
x.Authors = new Set([pubkey]);
return x;
const b = new RequestBuilder(`follows:${pubkey.slice(0, 12)}`);
b.withFilter().kinds([EventKind.ContactList]).authors([pubkey]);
return b;
}, [isMe, pubkey]);
const contactFeed = useSubscription(sub, { leaveOpen: false, cache: true });
const contactFeed = useRequestBuilder<PubkeyReplaceableNoteStore>(PubkeyReplaceableNoteStore, sub);
return useMemo(() => {
if (isMe) {
return follows;
}
return getFollowing(contactFeed.store.notes ?? [], pubkey);
}, [contactFeed.store, follows, pubkey]);
return getFollowing(contactFeed.data ?? [], pubkey);
}, [contactFeed, follows, pubkey]);
}
export function getFollowing(notes: TaggedRawEvent[], pubkey?: HexKey) {
export function getFollowing(notes: readonly TaggedRawEvent[], pubkey?: HexKey) {
const contactLists = notes.filter(a => a.kind === EventKind.ContactList && a.pubkey === pubkey);
const pTags = contactLists?.map(a => a.tags.filter(b => b[0] === "p").map(c => c[1]));
return [...new Set(pTags?.flat())];