flotilla/src/app/components/Zap.svelte

105 lines
3.2 KiB
Svelte
Raw Normal View History

2025-07-04 13:22:19 +00:00
<script lang="ts">
import {first} from "@welshman/lib"
2025-07-04 13:22:19 +00:00
import {signer, deriveZapperForPubkey} from "@welshman/app"
import {load} from "@welshman/net"
import {Router} from "@welshman/router"
import {requestZap, makeZapRequest, getZapResponseFilter} from "@welshman/util"
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"
2025-07-04 13:22:19 +00:00
type Props = {
url?: string
2025-07-04 13:22:19 +00:00
pubkey: string
eventId?: string
}
const {url, pubkey, eventId}: Props = $props()
const zapperStore = deriveZapperForPubkey(pubkey)
const back = () => history.back()
2025-07-04 13:22:19 +00:00
const sendZap = async () => {
loading = true
try {
const zapper = $zapperStore!
const msats = amount * 1000
const relays = url ? [url] : Router.get().ForPubkey(pubkey).getUrls()
const filters = [getZapResponseFilter({zapper, pubkey, eventId})]
const params = {pubkey, content, eventId, msats, relays, zapper}
const event = await $signer!.sign(makeZapRequest(params))
const res = await requestZap({zapper, event})
if (!res.invoice) {
return pushToast({
theme: "error",
message: `Failed to zap: ${res.error || "no error given"}`,
})
}
await payInvoice(res.invoice)
await load({relays, filters})
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>