refactor: simplify useEventReactions hook
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
c794a9e393
commit
7174ed2502
@ -1,7 +1,7 @@
|
|||||||
import "./LongFormText.css";
|
import "./LongFormText.css";
|
||||||
import { CSSProperties, useCallback, useRef, useState } from "react";
|
import { CSSProperties, useCallback, useRef, useState } from "react";
|
||||||
import { FormattedMessage, FormattedNumber } from "react-intl";
|
import { FormattedMessage, FormattedNumber } from "react-intl";
|
||||||
import { TaggedNostrEvent } from "@snort/system";
|
import { NostrLink, TaggedNostrEvent } from "@snort/system";
|
||||||
import { useEventReactions } from "@snort/system-react";
|
import { useEventReactions } from "@snort/system-react";
|
||||||
|
|
||||||
import { findTag } from "SnortUtils";
|
import { findTag } from "SnortUtils";
|
||||||
@ -26,7 +26,7 @@ export function LongFormText(props: LongFormTextProps) {
|
|||||||
const { proxy } = useImgProxy();
|
const { proxy } = useImgProxy();
|
||||||
const [reading, setReading] = useState(false);
|
const [reading, setReading] = useState(false);
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
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() {
|
function previewText() {
|
||||||
return (
|
return (
|
||||||
|
@ -36,7 +36,7 @@ export function NoteInner(props: NoteProps) {
|
|||||||
|
|
||||||
const { isEventMuted } = useModeration();
|
const { isEventMuted } = useModeration();
|
||||||
const { ref, inView } = useInView({ triggerOnce: true });
|
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 login = useLogin();
|
||||||
const { pinned, bookmarked } = login;
|
const { pinned, bookmarked } = login;
|
||||||
const { publisher, system } = useEventPublisher();
|
const { publisher, system } = useEventPublisher();
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
import { EventKind, RequestBuilder, parseZap, NostrLink, NoteCollection } from "@snort/system";
|
import { EventKind, RequestBuilder, parseZap, NostrLink, NoteCollection } from "@snort/system";
|
||||||
import { useRequestBuilder } from "@snort/system-react";
|
import { useRequestBuilder } from "@snort/system-react";
|
||||||
import { UserCache } from "Cache";
|
|
||||||
|
|
||||||
export default function useZapsFeed(link?: NostrLink) {
|
export default function useZapsFeed(link?: NostrLink) {
|
||||||
const sub = useMemo(() => {
|
const sub = useMemo(() => {
|
||||||
@ -15,7 +14,7 @@ export default function useZapsFeed(link?: NostrLink) {
|
|||||||
|
|
||||||
const zaps = useMemo(() => {
|
const zaps = useMemo(() => {
|
||||||
if (zapsFeed.data) {
|
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);
|
profileZaps.sort((a, b) => b.amount - a.amount);
|
||||||
return profileZaps;
|
return profileZaps;
|
||||||
}
|
}
|
||||||
|
@ -295,7 +295,7 @@ function NotificationGroup({ evs, onClick }: { evs: Array<TaggedNostrEvent>; onC
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const zaps = useMemo(() => {
|
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]);
|
}, [evs]);
|
||||||
const pubkeys = dedupe(
|
const pubkeys = dedupe(
|
||||||
evs.map(a => {
|
evs.map(a => {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@snort/system-react",
|
"name": "@snort/system-react",
|
||||||
"version": "1.1.1",
|
"version": "1.1.2",
|
||||||
"description": "React hooks for @snort/system",
|
"description": "React hooks for @snort/system",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
@ -16,7 +16,7 @@
|
|||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@snort/shared": "^1.0.9",
|
"@snort/shared": "^1.0.9",
|
||||||
"@snort/system": "^1.1.1",
|
"@snort/system": "^1.1.2",
|
||||||
"react": "^18.2.0"
|
"react": "^18.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -1,20 +1,15 @@
|
|||||||
import { useContext, useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
import { normalizeReaction, Reaction } from "@snort/shared";
|
import { normalizeReaction, Reaction } from "@snort/shared";
|
||||||
import { EventKind, NostrLink, parseZap, TaggedNostrEvent } from "@snort/system";
|
import { EventKind, NostrLink, parseZap, TaggedNostrEvent } from "@snort/system";
|
||||||
|
|
||||||
import { SnortContext } from "./context";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse reactions to a given event from a set of related events
|
* Parse reactions to a given event from a set of related events
|
||||||
* @param ev
|
* @param link Reactions to linked event
|
||||||
* @param related
|
* @param related
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export function useEventReactions(ev: TaggedNostrEvent, related: ReadonlyArray<TaggedNostrEvent>) {
|
export function useEventReactions(link: NostrLink, related: ReadonlyArray<TaggedNostrEvent>) {
|
||||||
const system = useContext(SnortContext);
|
|
||||||
|
|
||||||
return useMemo(() => {
|
return useMemo(() => {
|
||||||
const link = NostrLink.fromEvent(ev);
|
|
||||||
const reactionKinds = related.reduce(
|
const reactionKinds = related.reduce(
|
||||||
(acc, v) => {
|
(acc, v) => {
|
||||||
if (link.isReplyToThis(v)) {
|
if (link.isReplyToThis(v)) {
|
||||||
@ -41,7 +36,7 @@ export function useEventReactions(ev: TaggedNostrEvent, related: ReadonlyArray<T
|
|||||||
);
|
);
|
||||||
|
|
||||||
const zaps = (reactionKinds[EventKind.ZapReceipt] ?? [])
|
const zaps = (reactionKinds[EventKind.ZapReceipt] ?? [])
|
||||||
.map(a => parseZap(a, system.ProfileLoader.Cache, ev))
|
.map(a => parseZap(a))
|
||||||
.filter(a => a.valid)
|
.filter(a => a.valid)
|
||||||
.sort((a, b) => b.amount - a.amount);
|
.sort((a, b) => b.amount - a.amount);
|
||||||
|
|
||||||
@ -61,5 +56,5 @@ export function useEventReactions(ev: TaggedNostrEvent, related: ReadonlyArray<T
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
}, [ev, related]);
|
}, [link, related]);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@snort/system",
|
"name": "@snort/system",
|
||||||
"version": "1.1.1",
|
"version": "1.1.2",
|
||||||
"description": "Snort nostr system package",
|
"description": "Snort nostr system package",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
import { FeedCache } from "@snort/shared";
|
|
||||||
import { decodeInvoice, InvoiceDetails } from "@snort/shared";
|
import { decodeInvoice, InvoiceDetails } from "@snort/shared";
|
||||||
import { NostrEvent } from "./nostr";
|
import { NostrEvent } from "./nostr";
|
||||||
import { findTag } from "./utils";
|
import { findTag } from "./utils";
|
||||||
import { MetadataCache } from "./cache";
|
|
||||||
import { EventExt } from "./event-ext";
|
import { EventExt } from "./event-ext";
|
||||||
import { NostrLink } from "./nostr-link";
|
import { NostrLink } from "./nostr-link";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
@ -18,7 +16,7 @@ function getInvoice(zap: NostrEvent): InvoiceDetails | undefined {
|
|||||||
return decodeInvoice(bolt11);
|
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);
|
const existing = ParsedZapCache.get(zapReceipt.id);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
return existing;
|
return existing;
|
||||||
@ -39,7 +37,6 @@ export function parseZap(zapReceipt: NostrEvent, userCache: FeedCache<MetadataCa
|
|||||||
const zapRequestThread = EventExt.extractThread(zapRequest);
|
const zapRequestThread = EventExt.extractThread(zapRequest);
|
||||||
const requestContext = zapRequestThread?.root;
|
const requestContext = zapRequestThread?.root;
|
||||||
|
|
||||||
const isForwardedZap = refNote?.tags.some(a => a[0] === "zap") ?? false;
|
|
||||||
const anonZap = zapRequest.tags.find(a => a[0] === "anon");
|
const anonZap = zapRequest.tags.find(a => a[0] === "anon");
|
||||||
const pollOpt = zapRequest.tags.find(a => a[0] === "poll_option")?.[1];
|
const pollOpt = zapRequest.tags.find(a => a[0] === "poll_option")?.[1];
|
||||||
const ret: ParsedZap = {
|
const ret: ParsedZap = {
|
||||||
@ -63,11 +60,6 @@ export function parseZap(zapReceipt: NostrEvent, userCache: FeedCache<MetadataCa
|
|||||||
ret.valid = false;
|
ret.valid = false;
|
||||||
ret.errors.push("amount tag does not match invoice amount");
|
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) {
|
if (!ret.valid) {
|
||||||
Log("Invalid zap %O", ret);
|
Log("Invalid zap %O", ret);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user