Total zap goals in millisats through welshman

This commit is contained in:
Coracle-Bot 2026-09-12 03:29:19 +00:00 committed by hodlbod
parent 38bf7bd566
commit 801fbb8918
3 changed files with 19 additions and 58 deletions

View file

@ -1,6 +1,6 @@
<script lang="ts">
import {writable} from "svelte/store"
import {relay} from "@welshman/util"
import {relay, toMsats} from "@welshman/util"
import {ZapGoal} from "@welshman/domain"
import {publish} from "@welshman/app"
import {isMobile, preventDefault} from "@lib/html"
@ -81,7 +81,7 @@
const eventWriter = writer(ZapGoal)
.setTitle(title)
.setSummary(content)
.setAmount(amount)
.setAmount(toMsats(amount))
.setUrls([url])
.setProtected(protect)
.addTags(...ed.storage.nostr.getEditorTags())

View file

@ -1,16 +1,12 @@
<script lang="ts">
import {derived} from "svelte/store"
import {now, DAY, removeUndefined, uniq, sum} from "@welshman/lib"
import {now, DAY, removeUndefined} from "@welshman/lib"
import type {TrustedEvent} from "@welshman/util"
import {fromMsats, ZAP_RECEIPT} from "@welshman/util"
import {ZapGoal} from "@welshman/domain"
import type {Zap} from "@welshman/domain"
import {Zappers} from "@welshman/app"
import {fromMsats} from "@welshman/util"
import {ZapGoals} from "@welshman/app"
import Bolt from "@assets/icons/bolt.svg?dataurl"
import Icon from "@lib/components/Icon.svelte"
import {deriveEvents} from "@app/repository"
import ZapButton from "@app/components/ZapButton.svelte"
import {app, reader} from "@app/core"
import {app} from "@app/core"
type Props = {
url?: string
@ -20,46 +16,29 @@
const {url, event, ...props}: Props = $props()
const receipts = deriveEvents([{kinds: [ZAP_RECEIPT], "#e": [event.id]}])
const progress = $app.use(ZapGoals).progress(event, removeUndefined([url])).$
const zaps = derived<typeof receipts, Zap[]>(
receipts,
($receipts, set) =>
$app
.use(Zappers)
.validZapReceipts($receipts, event, removeUndefined([url]))
.$.subscribe(set),
[],
)
const goal = reader(ZapGoal)(event)
const goalAmount = goal.amount() ?? 0
const closedAt = goal.closedAt()
// NIP-75: receipts published after closed_at don't count toward the goal.
const counted = $derived($zaps.filter(zap => !closedAt || zap.response.created_at <= closedAt))
const zapAmount = $derived(fromMsats(sum(counted.map(zap => zap.invoiceAmount))))
const contributorsCount = $derived(uniq(counted.map(zap => zap.request.pubkey)).length)
const daysOld = Math.ceil((now() - event.created_at) / DAY)
</script>
<div class="flex flex-col gap-8 {props.class}">
<div class="flex gap-8">
<div>
<p class="text-xl text-primary">{zapAmount} sats</p>
<p class="text-sm opacity-75">funded of {goalAmount} sats</p>
<p class="text-xl text-primary">{fromMsats($progress.amount)} sats</p>
<p class="text-sm opacity-75">funded of {fromMsats($progress.target)} sats</p>
</div>
<div>
<p class="text-xl">{contributorsCount}</p>
<p class="text-sm opacity-75">{contributorsCount === 1 ? "contributor" : "contributors"}</p>
<p class="text-xl">{$progress.contributors.length}</p>
<p class="text-sm opacity-75">
{$progress.contributors.length === 1 ? "contributor" : "contributors"}
</p>
</div>
<div>
<p class="text-xl">{daysOld}</p>
<p class="text-sm opacity-75">{daysOld === 1 ? "day" : "days"} old</p>
</div>
</div>
<progress class="progress" value={zapAmount} max={goalAmount}></progress>
<progress class="progress" value={$progress.amount} max={$progress.target}></progress>
<ZapButton {url} {event} class="button button-primary lg:m-auto lg:px-20">
<Icon icon={Bolt} />
Contribute to this goal

View file

@ -1,16 +1,13 @@
<script lang="ts">
import type {ComponentProps} from "svelte"
import {derived} from "svelte/store"
import {removeUndefined, sum} from "@welshman/lib"
import {ZAP_RECEIPT, fromMsats} from "@welshman/util"
import {removeUndefined} from "@welshman/lib"
import {fromMsats} from "@welshman/util"
import {ZapGoal} from "@welshman/domain"
import type {Zap} from "@welshman/domain"
import {Zappers} from "@welshman/app"
import {ZapGoals} from "@welshman/app"
import Bolt from "@assets/icons/bolt.svg?dataurl"
import Icon from "@lib/components/Icon.svelte"
import ContentMinimal from "@app/components/ContentMinimal.svelte"
import {app, reader} from "@app/core"
import {deriveEvents} from "@app/repository"
const props: ComponentProps<typeof ContentMinimal> = $props()
@ -19,29 +16,14 @@
const title = goal.title()
const summaryEvent = $derived({content: goal.summary(), tags: props.event.tags})
const receipts = deriveEvents([{kinds: [ZAP_RECEIPT], "#e": [props.event.id]}])
const zaps = derived<typeof receipts, Zap[]>(
receipts,
($receipts, set) =>
$app
.use(Zappers)
.validZapReceipts($receipts, props.event, removeUndefined([props.url]))
.$.subscribe(set),
[],
)
const goalAmount = goal.amount() ?? 0
const closedAt = goal.closedAt()
const counted = $derived($zaps.filter(zap => !closedAt || zap.response.created_at <= closedAt))
const zapAmount = $derived(fromMsats(sum(counted.map(zap => zap.invoiceAmount))))
const progress = $app.use(ZapGoals).progress(props.event, removeUndefined([props.url])).$
</script>
<div class="flex justify-between">
<span class="text-sm">{title}</span>
<div class="flex items-center gap-1">
<Icon icon={Bolt} size={4} />
{zapAmount}/{goalAmount} sats funded
{fromMsats($progress.amount)}/{fromMsats($progress.target)} sats funded
</div>
</div>
<ContentMinimal {...props} event={summaryEvent} />