snort/packages/app/src/Feed/FollowersFeed.ts

27 lines
858 B
TypeScript
Raw Normal View History

2023-01-10 10:30:33 +00:00
import { useMemo } from "react";
2023-02-11 20:05:46 +00:00
import { HexKey } from "@snort/nostr";
import { EventKind, Subscriptions } from "@snort/nostr";
2023-01-20 11:11:50 +00:00
import useSubscription from "Feed/Subscription";
2023-01-10 10:30:33 +00:00
2023-01-15 19:40:47 +00:00
export default function useFollowersFeed(pubkey: HexKey) {
const sub = useMemo(() => {
2023-02-07 19:47:57 +00:00
const x = new Subscriptions();
x.Id = `followers:${pubkey.slice(0, 12)}`;
x.Kinds = new Set([EventKind.ContactList]);
x.PTags = new Set([pubkey]);
2023-01-10 10:30:33 +00:00
return x;
}, [pubkey]);
2023-01-10 10:30:33 +00:00
2023-02-10 11:12:11 +00:00
const followersFeed = useSubscription(sub, { leaveOpen: false, cache: true });
const followers = useMemo(() => {
const contactLists = followersFeed?.store.notes.filter(
a => a.kind === EventKind.ContactList && a.tags.some(b => b[0] === "p" && b[1] === pubkey)
);
return [...new Set(contactLists?.map(a => a.pubkey))];
}, [followersFeed, pubkey]);
return followers;
}