flotilla/src/app/components/Zap.svelte

121 lines
3.5 KiB
Svelte
Raw Normal View History

2025-07-04 13:22:19 +00:00
<script lang="ts">
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 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 ZapForm from "@app/components/ZapForm.svelte"
import {payInvoice} from "@app/lightning"
import {zapAmounts} from "@app/settings"
2026-06-08 21:14:40 +00:00
import {pushToast} from "@app/toast"
2026-07-28 16:16:26 +00:00
import {app, domain, network, router} from "@app/core"
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 sendZap = 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)
2025-07-04 13:22:19 +00:00
if (!res.invoice) {
return pushToast({
theme: "error",
message: `Failed to zap: ${res.error || "no error given"}`,
})
}
await payInvoice(res.invoice)
2026-07-28 16:16:26 +00:00
await $network.load({
relays,
filters: [currentZapper.getResponseFilter(pubkey, eventId)],
})
2025-07-04 13:22:19 +00:00
pushToast({message: "Zap successfully sent!"})
back()
} catch (e) {
console.error(e)
const message = errorMessage(e)
2025-07-04 13:22:19 +00:00
pushToast({
theme: "error",
message: `Failed to zap: ${message}`,
})
} finally {
loading = false
}
}
let amount = $state<number>(first($zapAmounts) ?? 21)
2025-07-04 13:22:19 +00:00
let content = $state("⚡️")
let loading = $state(false)
</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>
<ZapForm bind:amount bind:content />
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>
<Button class="button button-primary" onclick={sendZap} disabled={loading}>
2025-07-04 13:22:19 +00:00
<Spinner {loading}>
<div class="flex items-center gap-2">
{#if !loading}
2025-09-10 22:26:39 +00:00
<Icon icon={Bolt} />
2025-07-04 13:22:19 +00:00
{/if}
Send Zap
</div>
</Spinner>
</Button>
</ModalFooter>
2026-01-30 22:14:12 +00:00
</Modal>