refactor: simplify useEventReactions hook
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Kieran 2023-11-14 12:55:46 +00:00
parent c794a9e393
commit 7174ed2502
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
8 changed files with 14 additions and 28 deletions

View File

@ -1,7 +1,7 @@
import "./LongFormText.css";
import { CSSProperties, useCallback, useRef, useState } from "react";
import { FormattedMessage, FormattedNumber } from "react-intl";
import { TaggedNostrEvent } from "@snort/system";
import { NostrLink, TaggedNostrEvent } from "@snort/system";
import { useEventReactions } from "@snort/system-react";
import { findTag } from "SnortUtils";
@ -26,7 +26,7 @@ export function LongFormText(props: LongFormTextProps) {
const { proxy } = useImgProxy();
const [reading, setReading] = useState(false);
const ref = useRef<HTMLDivElement>(null);
const { reactions, reposts, zaps } = useEventReactions(props.ev, props.related);
const { reactions, reposts, zaps } = useEventReactions(NostrLink.fromEvent(props.ev), props.related);
function previewText() {
return (

View File

@ -36,7 +36,7 @@ export function NoteInner(props: NoteProps) {
const { isEventMuted } = useModeration();
const { ref, inView } = useInView({ triggerOnce: true });
const { reactions, reposts, deletions, zaps } = useEventReactions(ev, related);
const { reactions, reposts, deletions, zaps } = useEventReactions(NostrLink.fromEvent(ev), related);
const login = useLogin();
const { pinned, bookmarked } = login;
const { publisher, system } = useEventPublisher();

View File

@ -1,7 +1,6 @@
import { useMemo } from "react";
import { EventKind, RequestBuilder, parseZap, NostrLink, NoteCollection } from "@snort/system";
import { useRequestBuilder } from "@snort/system-react";
import { UserCache } from "Cache";
export default function useZapsFeed(link?: NostrLink) {
const sub = useMemo(() => {
@ -15,7 +14,7 @@ export default function useZapsFeed(link?: NostrLink) {
const zaps = useMemo(() => {
if (zapsFeed.data) {
const profileZaps = zapsFeed.data.map(a => parseZap(a, UserCache)).filter(z => z.valid);
const profileZaps = zapsFeed.data.map(a => parseZap(a)).filter(z => z.valid);
profileZaps.sort((a, b) => b.amount - a.amount);
return profileZaps;
}

View File

@ -295,7 +295,7 @@ function NotificationGroup({ evs, onClick }: { evs: Array<TaggedNostrEvent>; onC
const navigate = useNavigate();
const zaps = useMemo(() => {
return evs.filter(a => a.kind === EventKind.ZapReceipt).map(a => parseZap(a, UserCache));
return evs.filter(a => a.kind === EventKind.ZapReceipt).map(a => parseZap(a));
}, [evs]);
const pubkeys = dedupe(
evs.map(a => {

View File

@ -1,6 +1,6 @@
{
"name": "@snort/system-react",
"version": "1.1.1",
"version": "1.1.2",
"description": "React hooks for @snort/system",
"main": "dist/index.js",
"types": "dist/index.d.ts",
@ -16,7 +16,7 @@
],
"dependencies": {
"@snort/shared": "^1.0.9",
"@snort/system": "^1.1.1",
"@snort/system": "^1.1.2",
"react": "^18.2.0"
},
"devDependencies": {

View File

@ -1,20 +1,15 @@
import { useContext, useMemo } from "react";
import { useMemo } from "react";
import { normalizeReaction, Reaction } from "@snort/shared";
import { EventKind, NostrLink, parseZap, TaggedNostrEvent } from "@snort/system";
import { SnortContext } from "./context";
/**
* Parse reactions to a given event from a set of related events
* @param ev
* @param link Reactions to linked event
* @param related
* @returns
*/
export function useEventReactions(ev: TaggedNostrEvent, related: ReadonlyArray<TaggedNostrEvent>) {
const system = useContext(SnortContext);
export function useEventReactions(link: NostrLink, related: ReadonlyArray<TaggedNostrEvent>) {
return useMemo(() => {
const link = NostrLink.fromEvent(ev);
const reactionKinds = related.reduce(
(acc, v) => {
if (link.isReplyToThis(v)) {
@ -41,7 +36,7 @@ export function useEventReactions(ev: TaggedNostrEvent, related: ReadonlyArray<T
);
const zaps = (reactionKinds[EventKind.ZapReceipt] ?? [])
.map(a => parseZap(a, system.ProfileLoader.Cache, ev))
.map(a => parseZap(a))
.filter(a => a.valid)
.sort((a, b) => b.amount - a.amount);
@ -61,5 +56,5 @@ export function useEventReactions(ev: TaggedNostrEvent, related: ReadonlyArray<T
),
),
};
}, [ev, related]);
}, [link, related]);
}

View File

@ -1,6 +1,6 @@
{
"name": "@snort/system",
"version": "1.1.1",
"version": "1.1.2",
"description": "Snort nostr system package",
"main": "dist/index.js",
"types": "dist/index.d.ts",

View File

@ -1,8 +1,6 @@
import { FeedCache } from "@snort/shared";
import { decodeInvoice, InvoiceDetails } from "@snort/shared";
import { NostrEvent } from "./nostr";
import { findTag } from "./utils";
import { MetadataCache } from "./cache";
import { EventExt } from "./event-ext";
import { NostrLink } from "./nostr-link";
import debug from "debug";
@ -18,7 +16,7 @@ function getInvoice(zap: NostrEvent): InvoiceDetails | undefined {
return decodeInvoice(bolt11);
}
export function parseZap(zapReceipt: NostrEvent, userCache: FeedCache<MetadataCache>, refNote?: NostrEvent): ParsedZap {
export function parseZap(zapReceipt: NostrEvent): ParsedZap {
const existing = ParsedZapCache.get(zapReceipt.id);
if (existing) {
return existing;
@ -39,7 +37,6 @@ export function parseZap(zapReceipt: NostrEvent, userCache: FeedCache<MetadataCa
const zapRequestThread = EventExt.extractThread(zapRequest);
const requestContext = zapRequestThread?.root;
const isForwardedZap = refNote?.tags.some(a => a[0] === "zap") ?? false;
const anonZap = zapRequest.tags.find(a => a[0] === "anon");
const pollOpt = zapRequest.tags.find(a => a[0] === "poll_option")?.[1];
const ret: ParsedZap = {
@ -63,11 +60,6 @@ export function parseZap(zapReceipt: NostrEvent, userCache: FeedCache<MetadataCa
ret.valid = false;
ret.errors.push("amount tag does not match invoice amount");
}
/*if (userCache.getFromCache(ret.receiver)?.zapService !== ret.zapService && !isForwardedZap) {
ret.valid = false;
ret.errors.push("zap service pubkey doesn't match");
}*/
if (!ret.valid) {
Log("Invalid zap %O", ret);
}