fix zapping streams

This commit is contained in:
hzrd149
2023-10-04 09:58:41 -05:00
parent c0e3269b7f
commit b5d1cbd041
7 changed files with 19 additions and 26 deletions

View File

@@ -110,9 +110,10 @@ async function getPayRequestsForEvent(
event: NostrEvent, event: NostrEvent,
amount: number, amount: number,
comment?: string, comment?: string,
fallbackPubkey?: string,
additionalRelays?: string[], additionalRelays?: string[],
) { ) {
const splits = getZapSplits(event); const splits = getZapSplits(event, fallbackPubkey);
const draftZapRequests: PayRequest[] = []; const draftZapRequests: PayRequest[] = [];
for (const { pubkey, percent } of splits) { for (const { pubkey, percent } of splits) {
@@ -134,11 +135,11 @@ export type ZapModalProps = Omit<ModalProps, "children"> & {
relays?: string[]; relays?: string[];
initialComment?: string; initialComment?: string;
initialAmount?: number; initialAmount?: number;
onInvoice: (invoice: string) => void;
allowComment?: boolean; allowComment?: boolean;
showEmbed?: boolean; showEmbed?: boolean;
embedProps?: EmbedProps; embedProps?: EmbedProps;
additionalRelays?: string[]; additionalRelays?: string[];
onZapped: () => void;
}; };
export default function ZapModal({ export default function ZapModal({
@@ -148,18 +149,18 @@ export default function ZapModal({
onClose, onClose,
initialComment, initialComment,
initialAmount, initialAmount,
onInvoice,
allowComment = true, allowComment = true,
showEmbed = true, showEmbed = true,
embedProps, embedProps,
additionalRelays = [], additionalRelays = [],
onZapped,
...props ...props
}: ZapModalProps) { }: ZapModalProps) {
const [callbacks, setCallbacks] = useState<PayRequest[]>(); const [callbacks, setCallbacks] = useState<PayRequest[]>();
const renderContent = () => { const renderContent = () => {
if (callbacks && callbacks.length > 0) { if (callbacks && callbacks.length > 0) {
return <PayStep callbacks={callbacks} onComplete={onClose} />; return <PayStep callbacks={callbacks} onComplete={onZapped} />;
} else { } else {
return ( return (
<InputStep <InputStep
@@ -173,7 +174,9 @@ export default function ZapModal({
onSubmit={async (values) => { onSubmit={async (values) => {
const amountInMSats = values.amount * 1000; const amountInMSats = values.amount * 1000;
if (event) { if (event) {
setCallbacks(await getPayRequestsForEvent(event, amountInMSats, values.comment, additionalRelays)); setCallbacks(
await getPayRequestsForEvent(event, amountInMSats, values.comment, pubkey, additionalRelays),
);
} else { } else {
const callback = await getPayRequestForPubkey( const callback = await getPayRequestForPubkey(
pubkey, pubkey,

View File

@@ -71,7 +71,7 @@ export default function InputStep({
}, },
}); });
const splits = event ? getZapSplits(event) : []; const splits = event ? getZapSplits(event, pubkey) : [];
const { metadata: lnurlMetadata } = useUserLNURLMetadata(pubkey); const { metadata: lnurlMetadata } = useUserLNURLMetadata(pubkey);
const canZap = lnurlMetadata?.allowsNostr && lnurlMetadata?.nostrPubkey; const canZap = lnurlMetadata?.allowsNostr && lnurlMetadata?.nostrPubkey;

View File

@@ -9,7 +9,6 @@ import eventZapsService from "../../services/event-zaps";
import { NostrEvent } from "../../types/nostr-event"; import { NostrEvent } from "../../types/nostr-event";
import { LightningIcon } from "../icons"; import { LightningIcon } from "../icons";
import ZapModal from "../event-zap-modal"; import ZapModal from "../event-zap-modal";
import { useInvoiceModalContext } from "../../providers/invoice-modal";
import useUserLNURLMetadata from "../../hooks/use-user-lnurl-metadata"; import useUserLNURLMetadata from "../../hooks/use-user-lnurl-metadata";
import { getEventUID } from "../../helpers/nostr/events"; import { getEventUID } from "../../helpers/nostr/events";
@@ -22,15 +21,13 @@ export type NoteZapButtonProps = Omit<ButtonProps, "children"> & {
export default function NoteZapButton({ event, allowComment, showEventPreview, ...props }: NoteZapButtonProps) { export default function NoteZapButton({ event, allowComment, showEventPreview, ...props }: NoteZapButtonProps) {
const account = useCurrentAccount(); const account = useCurrentAccount();
const { metadata } = useUserLNURLMetadata(event.pubkey); const { metadata } = useUserLNURLMetadata(event.pubkey);
const { requestPay } = useInvoiceModalContext();
const zaps = useEventZaps(event.id); const zaps = useEventZaps(event.id);
const { isOpen, onOpen, onClose } = useDisclosure(); const { isOpen, onOpen, onClose } = useDisclosure();
const hasZapped = !!account && zaps.some((zap) => zap.request.pubkey === account.pubkey); const hasZapped = !!account && zaps.some((zap) => zap.request.pubkey === account.pubkey);
const handleInvoice = async (invoice: string) => { const onZapped = () => {
onClose(); onClose();
await requestPay(invoice);
eventZapsService.requestZaps(getEventUID(event), clientRelaysService.getReadUrls(), true); eventZapsService.requestZaps(getEventUID(event), clientRelaysService.getReadUrls(), true);
}; };
@@ -64,10 +61,10 @@ export default function NoteZapButton({ event, allowComment, showEventPreview, .
{isOpen && ( {isOpen && (
<ZapModal <ZapModal
isOpen={isOpen} isOpen={isOpen}
onClose={onClose}
event={event}
onInvoice={handleInvoice}
pubkey={event.pubkey} pubkey={event.pubkey}
event={event}
onClose={onClose}
onZapped={onZapped}
allowComment={allowComment} allowComment={allowComment}
showEmbed={showEventPreview} showEmbed={showEventPreview}
/> />

View File

@@ -75,7 +75,7 @@ export function parseZapEvent(event: NostrEvent): ParsedZap {
} }
export type EventSplit = { pubkey: string; percent: number; relay?: string }[]; export type EventSplit = { pubkey: string; percent: number; relay?: string }[];
export function getZapSplits(event: NostrEvent): EventSplit { export function getZapSplits(event: NostrEvent, fallbackPubkey?: string): EventSplit {
const tags = event.tags.filter((t) => t[0] === "zap" && t[1] && t[3]) as [string, string, string, string][]; const tags = event.tags.filter((t) => t[0] === "zap" && t[1] && t[3]) as [string, string, string, string][];
if (tags.length > 0) { if (tags.length > 0) {
@@ -85,5 +85,5 @@ export function getZapSplits(event: NostrEvent): EventSplit {
const total = targets.reduce((v, p) => v + p.percent, 0); const total = targets.reduce((v, p) => v + p.percent, 0);
return targets.map((p) => ({ ...p, percent: p.percent / total })); return targets.map((p) => ({ ...p, percent: p.percent / total }));
} else return [{ pubkey: event.pubkey, relay: "", percent: 1 }]; } else return [{ pubkey: fallbackPubkey || event.pubkey, relay: "", percent: 1 }];
} }

View File

@@ -3,7 +3,6 @@ import { NostrEvent } from "../../../types/nostr-event";
import ZapModal from "../../../components/event-zap-modal"; import ZapModal from "../../../components/event-zap-modal";
import eventZapsService from "../../../services/event-zaps"; import eventZapsService from "../../../services/event-zaps";
import { getEventUID } from "../../../helpers/nostr/events"; import { getEventUID } from "../../../helpers/nostr/events";
import { useInvoiceModalContext } from "../../../providers/invoice-modal";
import { getGoalRelays } from "../../../helpers/nostr/goal"; import { getGoalRelays } from "../../../helpers/nostr/goal";
import { useReadRelayUrls } from "../../../hooks/use-client-relays"; import { useReadRelayUrls } from "../../../hooks/use-client-relays";
@@ -12,12 +11,10 @@ export default function GoalZapButton({
...props ...props
}: Omit<ButtonProps, "children" | "onClick"> & { goal: NostrEvent }) { }: Omit<ButtonProps, "children" | "onClick"> & { goal: NostrEvent }) {
const modal = useDisclosure(); const modal = useDisclosure();
const { requestPay } = useInvoiceModalContext();
const readRelays = useReadRelayUrls(getGoalRelays(goal)); const readRelays = useReadRelayUrls(getGoalRelays(goal));
const handleInvoice = async (invoice: string) => { const onZapped = async () => {
modal.onClose(); modal.onClose();
await requestPay(invoice);
setTimeout(() => { setTimeout(() => {
eventZapsService.requestZaps(getEventUID(goal), readRelays, true); eventZapsService.requestZaps(getEventUID(goal), readRelays, true);
}, 1000); }, 1000);
@@ -33,7 +30,7 @@ export default function GoalZapButton({
isOpen isOpen
onClose={modal.onClose} onClose={modal.onClose}
event={goal} event={goal}
onInvoice={handleInvoice} onZapped={onZapped}
pubkey={goal.pubkey} pubkey={goal.pubkey}
relays={getGoalRelays(goal)} relays={getGoalRelays(goal)}
allowComment allowComment

View File

@@ -1,7 +1,6 @@
import { Button, IconButton, useDisclosure } from "@chakra-ui/react"; import { Button, IconButton, useDisclosure } from "@chakra-ui/react";
import { ParsedStream } from "../../../helpers/nostr/stream"; import { ParsedStream } from "../../../helpers/nostr/stream";
import { LightningIcon } from "../../../components/icons"; import { LightningIcon } from "../../../components/icons";
import { useInvoiceModalContext } from "../../../providers/invoice-modal";
import useUserLNURLMetadata from "../../../hooks/use-user-lnurl-metadata"; import useUserLNURLMetadata from "../../../hooks/use-user-lnurl-metadata";
import ZapModal from "../../../components/event-zap-modal"; import ZapModal from "../../../components/event-zap-modal";
import { useRelaySelectionRelays } from "../../../providers/relay-selection-provider"; import { useRelaySelectionRelays } from "../../../providers/relay-selection-provider";
@@ -19,7 +18,6 @@ export default function StreamZapButton({
label?: string; label?: string;
}) { }) {
const zapModal = useDisclosure(); const zapModal = useDisclosure();
const { requestPay } = useInvoiceModalContext();
const zapMetadata = useUserLNURLMetadata(stream.host); const zapMetadata = useUserLNURLMetadata(stream.host);
const relays = useRelaySelectionRelays(); const relays = useRelaySelectionRelays();
const goal = useStreamGoal(stream); const goal = useStreamGoal(stream);
@@ -50,10 +48,9 @@ export default function StreamZapButton({
isOpen isOpen
event={zapEvent} event={zapEvent}
pubkey={stream.host} pubkey={stream.host}
onInvoice={async (invoice) => { onZapped={async () => {
if (onZap) onZap(); if (onZap) onZap();
zapModal.onClose(); zapModal.onClose();
await requestPay(invoice);
}} }}
onClose={zapModal.onClose} onClose={zapModal.onClose}
initialComment={initComment} initialComment={initComment}

View File

@@ -30,8 +30,7 @@ export default function UserZapButton({ pubkey, ...props }: { pubkey: string } &
isOpen={isOpen} isOpen={isOpen}
onClose={onClose} onClose={onClose}
pubkey={pubkey} pubkey={pubkey}
onInvoice={async (invoice) => { onZapped={async () => {
await requestPay(invoice);
onClose(); onClose();
}} }}
/> />