flotilla/src/app/components/ZapInvoice.svelte

181 lines
5.4 KiB
Svelte
Raw Normal View History

2026-02-25 22:25:59 +00:00
<script lang="ts">
import {onDestroy} from "svelte"
2026-07-28 16:16:26 +00:00
import {first, uniq} from "@welshman/lib"
import {inbox} from "@welshman/util"
import {ZapRequest} from "@welshman/domain"
import {Zappers} from "@welshman/app"
2026-02-25 22:25:59 +00:00
import Bolt from "@assets/icons/bolt.svg?dataurl"
import Copy from "@assets/icons/copy.svg?dataurl"
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
import Icon from "@lib/components/Icon.svelte"
import Spinner from "@lib/components/Spinner.svelte"
import Button from "@lib/components/Button.svelte"
import Modal from "@lib/components/Modal.svelte"
import ModalBody from "@lib/components/ModalBody.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte"
import ModalTitle from "@lib/components/ModalTitle.svelte"
import ModalSubtitle from "@lib/components/ModalSubtitle.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte"
import {errorMessage} from "@lib/util"
import ProfileLink from "@app/components/ProfileLink.svelte"
import QRCode from "@app/components/QRCode.svelte"
import WalletConnect from "@app/components/WalletConnect.svelte"
import ZapForm from "@app/components/ZapForm.svelte"
2026-07-28 16:16:26 +00:00
import {app, domain, network, router} from "@app/core"
2026-06-08 21:14:40 +00:00
import {pushModal} from "@app/modal"
import {zapAmounts} from "@app/settings"
2026-06-08 21:14:40 +00:00
import {clip, pushToast} from "@app/toast"
2026-02-25 22:25:59 +00:00
type Props = {
url?: string
2026-02-25 22:25:59 +00:00
pubkey: string
eventId?: string
2026-07-28 16:16:26 +00:00
// NIP-75 requires a zap to a goal to request its receipt on the goal's own relays.
goalRelays?: string[]
2026-02-25 22:25:59 +00:00
}
2026-07-28 16:16:26 +00:00
const {url, pubkey, eventId, goalRelays = []}: Props = $props()
2026-02-25 22:25:59 +00:00
2026-07-28 16:16:26 +00:00
const zapper = $app.use(Zappers).forPubkey(pubkey)
2026-02-25 22:25:59 +00:00
const back = () => history.back()
2026-02-25 22:25:59 +00:00
const createInvoice = async () => {
loading = true
try {
2026-07-28 16:16:26 +00:00
const currentZapper = zapper.get()!
const relays = uniq([
...(url ? [url] : await $router.resolver.relays([inbox(pubkey)])),
...goalRelays,
])
const writer = $domain
.writer(ZapRequest)
.setContent(content)
.setAmount(amount * 1000)
.setLnurl(currentZapper.lnurl)
.setRecipient(pubkey)
.setUrls(relays)
if (eventId) {
writer.setEventId(eventId)
}
const res = await writer.requestInvoice(currentZapper)
2026-02-25 22:25:59 +00:00
if (!res.invoice) {
return pushToast({
theme: "error",
message: `Failed to create zap invoice: ${res.error || "no error given"}`,
})
}
invoice = res.invoice
paymentController?.abort()
paymentController = new AbortController()
2026-07-28 16:16:26 +00:00
$network.request({
2026-02-25 22:25:59 +00:00
relays,
signal: paymentController.signal,
2026-07-28 16:16:26 +00:00
filters: [currentZapper.getResponseFilter(pubkey, eventId)],
2026-02-25 22:25:59 +00:00
onEvent: () => {
pushToast({message: "Payment sent!"})
paymentController?.abort()
back()
},
})
} catch (e) {
console.error(e)
const message = errorMessage(e)
pushToast({
theme: "error",
message: `Failed to create zap invoice: ${message}`,
})
} finally {
loading = false
}
}
const connectWallet = () => {
pushModal(WalletConnect)
}
const copyInvoice = () => {
if (invoice) {
clip(invoice)
}
}
let amount = $state<number>(first($zapAmounts) ?? 21)
2026-02-25 22:25:59 +00:00
let content = $state("⚡️")
let loading = $state(false)
let invoice = $state<string>()
let paymentController: AbortController | undefined = $state()
onDestroy(() => {
paymentController?.abort()
})
</script>
<Modal>
<ModalBody>
<ModalHeader>
<ModalTitle>Send a Zap</ModalTitle>
2026-04-07 21:53:19 +00:00
<ModalSubtitle>To <ProfileLink {pubkey} class="text-primary!" /></ModalSubtitle>
2026-02-25 22:25:59 +00:00
</ModalHeader>
{#if invoice}
<div class="flex flex-col gap-6">
<div class="flex flex-col items-center gap-4">
<QRCode code={invoice} class="w-full max-w-56" />
<p class="text-content-muted text-center text-sm">
Scan with your lightning wallet, or copy the invoice below.
</p>
</div>
<label class="input flex w-full items-center gap-2">
<input readonly class="min-w-0 grow truncate" value={invoice} />
<Button
class="button button-neutral button-sm button-square shrink-0"
onclick={copyInvoice}>
<Icon icon={Copy} size={4} />
</Button>
</label>
2026-02-25 22:25:59 +00:00
</div>
{:else}
<ZapForm bind:amount bind:content>
{#snippet children()}
<div class="card card-sm card-flat flex flex-col items-center gap-3 p-4 text-center">
<p class="text-content-muted text-sm">
Connect a wallet to pay instantly without scanning a QR code.
</p>
<Button class="button button-neutral" onclick={connectWallet}>
Connect a lightning wallet
</Button>
2026-02-25 22:25:59 +00:00
</div>
{/snippet}
</ZapForm>
2026-02-25 22:25:59 +00:00
{/if}
</ModalBody>
<ModalFooter>
<Button class="button button-link" onclick={back}>
2026-02-25 22:25:59 +00:00
<Icon icon={AltArrowLeft} />
Go back
</Button>
{#if !invoice}
<Button class="button button-primary" onclick={createInvoice} disabled={loading}>
2026-02-25 22:25:59 +00:00
<Spinner {loading}>
<div class="flex items-center gap-2">
{#if !loading}
<Icon icon={Bolt} />
{/if}
Create invoice
</div>
</Spinner>
</Button>
{/if}
</ModalFooter>
</Modal>