Skip to content

Commit

Permalink
fix zapping streams
Browse files Browse the repository at this point in the history
  • Loading branch information
hzrd149 committed Oct 4, 2023
1 parent c0e3269 commit b5d1cbd
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 26 deletions.
13 changes: 8 additions & 5 deletions src/components/event-zap-modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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({
Expand All @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/components/event-zap-modal/input-step.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 4 additions & 7 deletions src/components/note/note-zap-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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);
};

Expand Down Expand Up @@ -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}
/>
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/nostr/zaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 }];
}
7 changes: 2 additions & 5 deletions src/views/goals/components/goal-zap-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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);
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/views/streams/components/stream-zap-button.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand Down Expand Up @@ -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}
Expand Down
3 changes: 1 addition & 2 deletions src/views/user/components/user-zap-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}}
/>
Expand Down

0 comments on commit b5d1cbd

Please sign in to comment.