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

28 lines
864 B
TypeScript
Raw Normal View History

2023-02-03 21:38:14 +00:00
import { useMemo } from "react";
2023-02-10 11:12:11 +00:00
import { HexKey, EventKind, Subscriptions } from "@snort/nostr";
import { parseZap } from "Element/Zap";
2023-02-03 21:38:14 +00:00
import useSubscription from "./Subscription";
2023-02-27 13:17:13 +00:00
export default function useZapsFeed(pubkey?: HexKey) {
const sub = useMemo(() => {
2023-02-27 13:17:13 +00:00
if (!pubkey) return null;
2023-02-07 19:47:57 +00:00
const x = new Subscriptions();
x.Id = `zaps:${pubkey.slice(0, 12)}`;
x.Kinds = new Set([EventKind.ZapReceipt]);
x.PTags = new Set([pubkey]);
return x;
}, [pubkey]);
2023-02-03 21:38:14 +00:00
2023-02-10 11:12:11 +00:00
const zapsFeed = useSubscription(sub, { leaveOpen: false, cache: true });
const zaps = useMemo(() => {
const profileZaps = zapsFeed.store.notes
.map(parseZap)
2023-03-05 16:58:34 +00:00
.filter(z => z.valid && z.receiver === pubkey && z.sender !== pubkey && !z.event);
2023-02-10 11:12:11 +00:00
profileZaps.sort((a, b) => b.amount - a.amount);
return profileZaps;
}, [zapsFeed]);
return zaps;
2023-02-03 21:38:14 +00:00
}