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

26 lines
913 B
TypeScript
Raw Normal View History

2023-01-10 10:30:33 +00:00
import { useMemo } from "react";
2023-03-28 14:34:01 +00:00
import { HexKey, EventKind } from "@snort/nostr";
import { PubkeyReplaceableNoteStore, RequestBuilder } from "System";
import useRequestBuilder from "Hooks/useRequestBuilder";
2023-01-10 10:30:33 +00:00
2023-02-27 13:17:13 +00:00
export default function useFollowersFeed(pubkey?: HexKey) {
const sub = useMemo(() => {
2023-02-27 13:17:13 +00:00
if (!pubkey) return null;
2023-03-28 14:34:01 +00:00
const b = new RequestBuilder(`followers:${pubkey.slice(0, 12)}`);
b.withFilter().kinds([EventKind.ContactList]).tag("p", [pubkey]);
return b;
}, [pubkey]);
2023-01-10 10:30:33 +00:00
2023-03-28 14:34:01 +00:00
const followersFeed = useRequestBuilder<PubkeyReplaceableNoteStore>(PubkeyReplaceableNoteStore, sub);
2023-02-10 11:12:11 +00:00
const followers = useMemo(() => {
2023-03-28 14:34:01 +00:00
const contactLists = followersFeed.data?.filter(
2023-02-10 11:12:11 +00:00
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;
}