import "./send-zap.css"; import * as Dialog from "@radix-ui/react-dialog"; import { useEffect, useState } from "react"; import { LNURL } from "@snort/shared"; import { NostrEvent, EventPublisher } from "@snort/system"; import { formatSats } from "../number"; import { Icon } from "./icon"; import AsyncButton from "./async-button"; import { findTag } from "utils"; import { Relays } from "index"; import QrCode from "./qr-code"; interface SendZapsProps { lnurl: string; ev?: NostrEvent; targetName?: string; onFinish: () => void; } function SendZaps({ lnurl, ev, targetName, onFinish }: SendZapsProps) { const UsdRate = 30_000; const satsAmounts = [ 100, 1_000, 5_000, 10_000, 50_000, 100_000, 500_000, 1_000_000, ]; const usdAmounts = [0.05, 0.5, 2, 5, 10, 50, 100, 200]; const [isFiat, setIsFiat] = useState(false); const [svc, setSvc] = useState(); const [amount, setAmount] = useState(satsAmounts[0]); const [comment, setComment] = useState(""); const [invoice, setInvoice] = useState(""); const name = targetName ?? svc?.name; async function loadService() { const s = new LNURL(lnurl); await s.load(); setSvc(s); } useEffect(() => { if (!svc) { loadService().catch(console.warn); } }, [lnurl]); async function send() { if (!svc) return; const pub = await EventPublisher.nip7(); if (!pub) return; const amountInSats = isFiat ? Math.floor((amount / UsdRate) * 1e8) : amount; let zap: NostrEvent | undefined; if (ev) { zap = await pub.zap( amountInSats * 1000, ev.pubkey, Relays, undefined, comment, (eb) => { return eb.tag(["a", `${ev.kind}:${ev.pubkey}:${findTag(ev, "d")}`]); } ); } const invoice = await svc.getInvoice(amountInSats, comment, zap); if (!invoice.pr) return; if (window.webln) { await window.webln.enable(); await window.webln.sendPayment(invoice.pr); onFinish(); } else { setInvoice(invoice.pr); } } function input() { if (invoice) return; return ( <>
{ setIsFiat(false); setAmount(satsAmounts[0]); }} > SATS { setIsFiat(true); setAmount(usdAmounts[0]); }} > USD
Zap amount in {isFiat ? "USD" : "sats"}
{(isFiat ? usdAmounts : satsAmounts).map((a) => ( setAmount(a)} > {isFiat ? `$${a.toLocaleString()}` : formatSats(a)} ))}
Your comment for {name}