flotilla/src/app/components/Zap.svelte

205 lines
5.9 KiB
Svelte
Raw Normal View History

2025-07-04 13:22:19 +00:00
<script lang="ts">
import {onDestroy} from "svelte"
2026-08-22 20:12:11 +00:00
import {first, removeUndefined, uniq} from "@welshman/lib"
2026-07-28 16:16:26 +00:00
import {inbox} from "@welshman/util"
import {ZapRequest} from "@welshman/domain"
import {Zappers} from "@welshman/app"
2025-09-10 22:26:39 +00:00
import Bolt from "@assets/icons/bolt.svg?dataurl"
import Copy from "@assets/icons/copy.svg?dataurl"
2025-09-10 22:26:39 +00:00
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
2025-07-04 13:22:19 +00:00
import Icon from "@lib/components/Icon.svelte"
import Spinner from "@lib/components/Spinner.svelte"
import Button from "@lib/components/Button.svelte"
2026-01-30 22:14:12 +00:00
import Modal from "@lib/components/Modal.svelte"
import ModalBody from "@lib/components/ModalBody.svelte"
2025-07-04 13:22:19 +00:00
import ModalHeader from "@lib/components/ModalHeader.svelte"
2026-02-02 18:09:14 +00:00
import ModalTitle from "@lib/components/ModalTitle.svelte"
import ModalSubtitle from "@lib/components/ModalSubtitle.svelte"
2025-07-04 13:22:19 +00:00
import ModalFooter from "@lib/components/ModalFooter.svelte"
import {errorMessage} from "@lib/util"
2025-07-04 13:22:19 +00:00
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"
import {payInvoice, wallet} from "@app/lightning"
import {pushModal} from "@app/modal"
import {zapAmounts} from "@app/settings"
import {clip, pushToast} from "@app/toast"
2025-07-04 13:22:19 +00:00
type Props = {
url?: string
2025-07-04 13:22:19 +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[]
2025-07-04 13:22:19 +00:00
}
2026-07-28 16:16:26 +00:00
const {url, pubkey, eventId, goalRelays = []}: Props = $props()
2025-07-04 13:22:19 +00:00
2026-08-22 20:12:11 +00:00
const zapper = $app.use(Zappers).forPubkey(pubkey, removeUndefined([url]))
2025-07-04 13:22:19 +00:00
const back = () => history.back()
2025-07-04 13:22:19 +00:00
const requestInvoice = async () => {
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)
2025-07-04 13:22:19 +00:00
if (eventId) {
writer.setEventId(eventId)
}
2026-07-28 16:16:26 +00:00
const res = await writer.requestInvoice(currentZapper)
2025-07-04 13:22:19 +00:00
if (!res.invoice) {
throw new Error(res.error || "no error given")
}
2025-07-04 13:22:19 +00:00
return {
relays,
invoice: res.invoice,
filters: [currentZapper.getResponseFilter(pubkey, eventId)],
}
}
const payWithWallet = async () => {
const {relays, invoice, filters} = await requestInvoice()
await payInvoice(invoice)
await $network.load({relays, filters})
pushToast({message: "Zap successfully sent!"})
back()
}
const createInvoice = async () => {
const {relays, invoice: created, filters} = await requestInvoice()
invoice = created
paymentController?.abort()
paymentController = new AbortController()
2025-07-04 13:22:19 +00:00
$network.request({
relays,
filters,
signal: paymentController.signal,
onEvent: () => {
pushToast({message: "Payment sent!"})
paymentController?.abort()
back()
},
})
}
const sendZap = async () => {
loading = true
try {
if ($wallet) {
await payWithWallet()
} else {
await createInvoice()
}
2025-07-04 13:22:19 +00:00
} catch (e) {
console.error(e)
pushToast({
theme: "error",
message: `Failed to zap: ${errorMessage(e)}`,
2025-07-04 13:22:19 +00:00
})
} finally {
loading = false
}
}
const connectWallet = () => {
pushModal(WalletConnect, {}, {nested: true})
}
const copyInvoice = () => {
if (invoice) {
clip(invoice)
}
}
let amount = $state<number>(first($zapAmounts) ?? 21)
2025-07-04 13:22:19 +00:00
let content = $state("⚡️")
let loading = $state(false)
let invoice = $state<string>()
let paymentController: AbortController | undefined = $state()
onDestroy(() => {
paymentController?.abort()
})
2025-07-04 13:22:19 +00:00
</script>
2026-01-30 22:14:12 +00:00
<Modal>
<ModalBody>
<ModalHeader>
2026-02-02 18:09:14 +00:00
<ModalTitle>Send a Zap</ModalTitle>
2026-04-07 21:53:19 +00:00
<ModalSubtitle>To <ProfileLink {pubkey} class="text-primary!" /></ModalSubtitle>
2026-01-30 22:14:12 +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>
</div>
{:else}
<ZapForm bind:amount bind:content>
{#if !$wallet}
<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>
</div>
{/if}
</ZapForm>
{/if}
2026-01-30 22:14:12 +00:00
</ModalBody>
2025-07-04 13:22:19 +00:00
<ModalFooter>
<Button class="button button-link" onclick={back}>
2025-09-10 22:26:39 +00:00
<Icon icon={AltArrowLeft} />
2025-07-04 13:22:19 +00:00
Go back
</Button>
{#if !invoice}
<Button class="button button-primary" onclick={sendZap} disabled={loading}>
<Spinner {loading}>
<div class="flex items-center gap-2">
{#if !loading}
<Icon icon={Bolt} />
{/if}
{$wallet ? "Send Zap" : "Create invoice"}
</div>
</Spinner>
</Button>
{/if}
2025-07-04 13:22:19 +00:00
</ModalFooter>
2026-01-30 22:14:12 +00:00
</Modal>