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

View File

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

View File

@ -75,7 +75,7 @@ export function parseZapEvent(event: NostrEvent): ParsedZap {
}
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][];
if (tags.length > 0) {
@ -85,5 +85,5 @@ export function getZapSplits(event: NostrEvent): EventSplit {
const total = targets.reduce((v, p) => v + p.percent, 0);
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 eventZapsService from "../../../services/event-zaps";
import { getEventUID } from "../../../helpers/nostr/events";
import { useInvoiceModalContext } from "../../../providers/invoice-modal";
import { getGoalRelays } from "../../../helpers/nostr/goal";
import { useReadRelayUrls } from "../../../hooks/use-client-relays";
@ -12,12 +11,10 @@ export default function GoalZapButton({
...props
}: Omit<ButtonProps, "children" | "onClick"> & { goal: NostrEvent }) {
const modal = useDisclosure();
const { requestPay } = useInvoiceModalContext();
const readRelays = useReadRelayUrls(getGoalRelays(goal));
const handleInvoice = async (invoice: string) => {
const onZapped = async () => {
modal.onClose();
await requestPay(invoice);
setTimeout(() => {
eventZapsService.requestZaps(getEventUID(goal), readRelays, true);
}, 1000);
@ -33,7 +30,7 @@ export default function GoalZapButton({
isOpen
onClose={modal.onClose}
event={goal}
onInvoice={handleInvoice}
onZapped={onZapped}
pubkey={goal.pubkey}
relays={getGoalRelays(goal)}
allowComment

View File

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

View File

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