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) {
|
2023-02-07 20:04:50 +00:00
|
|
|
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();
|
2023-02-07 20:04:50 +00:00
|
|
|
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
|
|
|
}
|