2025-07-04 13:22:19 +00:00
|
|
|
<script lang="ts">
|
2026-09-03 21:57:18 +00:00
|
|
|
import {onDestroy} from "svelte"
|
2026-09-12 14:04:06 +00:00
|
|
|
import {first, removeUndefined} from "@welshman/lib"
|
2026-09-25 22:46:22 +00:00
|
|
|
import {relay, toMsats} from "@welshman/util"
|
2026-09-12 14:04:06 +00:00
|
|
|
import type {TrustedEvent} from "@welshman/util"
|
2026-07-28 16:16:26 +00:00
|
|
|
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"
|
2026-09-03 21:57:18 +00:00
|
|
|
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"
|
2026-02-05 20:51:59 +00:00
|
|
|
import {errorMessage} from "@lib/util"
|
2025-07-04 13:22:19 +00:00
|
|
|
import ProfileLink from "@app/components/ProfileLink.svelte"
|
2026-09-03 21:57:18 +00:00
|
|
|
import QRCode from "@app/components/QRCode.svelte"
|
|
|
|
|
import WalletConnect from "@app/components/WalletConnect.svelte"
|
2026-07-15 21:23:43 +00:00
|
|
|
import ZapForm from "@app/components/ZapForm.svelte"
|
2026-09-12 14:04:06 +00:00
|
|
|
import {app, domain, network} from "@app/core"
|
2026-09-03 21:57:18 +00:00
|
|
|
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 = {
|
2026-06-15 21:00:15 +00:00
|
|
|
url?: string
|
2025-07-04 13:22:19 +00:00
|
|
|
pubkey: string
|
2026-09-12 14:04:06 +00:00
|
|
|
event?: TrustedEvent
|
2025-07-04 13:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
2026-09-12 14:04:06 +00:00
|
|
|
const {url, pubkey, event}: 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
|
|
|
|
2026-06-10 20:00:59 +00:00
|
|
|
const back = () => history.back()
|
2025-07-04 13:22:19 +00:00
|
|
|
|
2026-09-03 21:57:18 +00:00
|
|
|
const requestInvoice = async () => {
|
|
|
|
|
const currentZapper = zapper.get()!
|
|
|
|
|
const writer = $domain
|
|
|
|
|
.writer(ZapRequest)
|
|
|
|
|
.setContent(content)
|
2026-09-25 22:46:22 +00:00
|
|
|
.setAmount(toMsats(amount))
|
2026-09-03 21:57:18 +00:00
|
|
|
.setLnurl(currentZapper.lnurl)
|
|
|
|
|
.setRecipient(pubkey)
|
2025-07-04 13:22:19 +00:00
|
|
|
|
2026-09-12 14:04:06 +00:00
|
|
|
if (url) {
|
|
|
|
|
writer.forceRoutes(relay(url))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
|
writer.setEvent(event)
|
2026-09-03 21:57:18 +00:00
|
|
|
}
|
2026-07-28 16:16:26 +00:00
|
|
|
|
2026-09-03 21:57:18 +00:00
|
|
|
const res = await writer.requestInvoice(currentZapper)
|
2025-07-04 13:22:19 +00:00
|
|
|
|
2026-09-03 21:57:18 +00:00
|
|
|
if (!res.invoice) {
|
|
|
|
|
throw new Error(res.error || "no error given")
|
|
|
|
|
}
|
2025-07-04 13:22:19 +00:00
|
|
|
|
2026-09-03 21:57:18 +00:00
|
|
|
return {
|
2026-09-12 14:04:06 +00:00
|
|
|
relays: await writer.relays(),
|
2026-09-03 21:57:18 +00:00
|
|
|
invoice: res.invoice,
|
2026-09-12 14:04:06 +00:00
|
|
|
filters: [currentZapper.getResponseFilter(pubkey, event?.id)],
|
2026-09-03 21:57:18 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const payWithWallet = async () => {
|
|
|
|
|
const {relays, invoice, filters} = await requestInvoice()
|
|
|
|
|
|
|
|
|
|
await payInvoice(invoice)
|
2026-09-21 17:58:14 +00:00
|
|
|
await $network.loadLenient({relays, filters})
|
2026-09-03 21:57:18 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2026-09-03 21:57:18 +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",
|
2026-09-03 21:57:18 +00:00
|
|
|
message: `Failed to zap: ${errorMessage(e)}`,
|
2025-07-04 13:22:19 +00:00
|
|
|
})
|
|
|
|
|
} finally {
|
|
|
|
|
loading = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-03 21:57:18 +00:00
|
|
|
const connectWallet = () => {
|
|
|
|
|
pushModal(WalletConnect, {}, {nested: true})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const copyInvoice = () => {
|
|
|
|
|
if (invoice) {
|
|
|
|
|
clip(invoice)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-10 20:00:59 +00:00
|
|
|
let amount = $state<number>(first($zapAmounts) ?? 21)
|
2025-07-04 13:22:19 +00:00
|
|
|
let content = $state("⚡️")
|
|
|
|
|
let loading = $state(false)
|
2026-09-03 21:57:18 +00:00
|
|
|
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-09-23 00:03:45 +00:00
|
|
|
<ModalSubtitle>To <ProfileLink {pubkey} class="text-primary" /></ModalSubtitle>
|
2026-01-30 22:14:12 +00:00
|
|
|
</ModalHeader>
|
2026-09-03 21:57:18 +00:00
|
|
|
|
|
|
|
|
{#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>
|
2026-06-24 20:56:53 +00:00
|
|
|
<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>
|
2026-09-03 21:57:18 +00:00
|
|
|
{#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>
|