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