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

31 lines
948 B
TypeScript
Raw Normal View History

2023-02-03 21:38:14 +00:00
import { useMemo } from "react";
2023-03-28 14:34:01 +00:00
import { HexKey, EventKind } from "@snort/nostr";
2023-02-10 11:12:11 +00:00
import { parseZap } from "Element/Zap";
2023-03-28 14:34:01 +00:00
import { FlatNoteStore, RequestBuilder } from "System";
import useRequestBuilder from "Hooks/useRequestBuilder";
2023-02-03 21:38:14 +00:00
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-03-28 14:34:01 +00:00
const b = new RequestBuilder(`zaps:${pubkey.slice(0, 12)}`);
b.withFilter().tag("p", [pubkey]).kinds([EventKind.ZapReceipt]);
return b;
}, [pubkey]);
2023-02-03 21:38:14 +00:00
2023-03-28 14:34:01 +00:00
const zapsFeed = useRequestBuilder<FlatNoteStore>(FlatNoteStore, sub);
2023-02-10 11:12:11 +00:00
const zaps = useMemo(() => {
2023-03-28 14:34:01 +00:00
if (zapsFeed.data) {
const profileZaps = zapsFeed.data
.map(a => parseZap(a))
2023-03-28 14:34:01 +00:00
.filter(z => z.valid && z.receiver === pubkey && z.sender !== pubkey && !z.event);
profileZaps.sort((a, b) => b.amount - a.amount);
return profileZaps;
}
return [];
2023-02-10 11:12:11 +00:00
}, [zapsFeed]);
return zaps;
2023-02-03 21:38:14 +00:00
}