flotilla/src/app/components/ReactionSummary.svelte

169 lines
5.3 KiB
Svelte
Raw Normal View History

<script lang="ts">
2024-11-15 22:03:54 +00:00
import {onMount} from "svelte"
2025-02-05 21:02:51 +00:00
import type {Snippet} from "svelte"
2025-07-04 13:22:19 +00:00
import {groupBy, sum, uniq, uniqBy, batch, displayList} from "@welshman/lib"
2025-05-12 22:10:24 +00:00
import {
REACTION,
2025-07-04 13:22:19 +00:00
ZAP_RESPONSE,
2025-05-12 22:10:24 +00:00
getReplyFilters,
getEmojiTags,
getEmojiTag,
2025-07-07 20:58:43 +00:00
fromMsats,
2025-05-12 22:10:24 +00:00
getTag,
REPORT,
DELETE,
} from "@welshman/util"
2025-07-04 13:22:19 +00:00
import type {TrustedEvent, EventContent, Zap} from "@welshman/util"
import {deriveEvents, deriveEventsMapped} from "@welshman/store"
2025-04-11 16:27:19 +00:00
import {load} from "@welshman/net"
2025-07-04 13:22:19 +00:00
import {pubkey, repository, getValidZap, displayProfileByPubkey} from "@welshman/app"
2025-02-04 03:28:29 +00:00
import {isMobile, preventDefault, stopPropagation} from "@lib/html"
2025-01-15 01:23:27 +00:00
import Icon from "@lib/components/Icon.svelte"
2025-05-12 22:10:24 +00:00
import Reaction from "@app/components/Reaction.svelte"
2025-01-15 01:23:27 +00:00
import EventReportDetails from "@app/components/EventReportDetails.svelte"
2025-07-17 21:39:59 +00:00
import {REACTION_KINDS} from "@app/state"
2025-01-15 01:23:27 +00:00
import {pushModal} from "@app/modal"
2025-02-04 00:37:14 +00:00
interface Props {
2025-05-12 22:10:24 +00:00
event: TrustedEvent
deleteReaction: (event: TrustedEvent) => void
createReaction: (event: EventContent) => void
2025-02-04 00:37:14 +00:00
url?: string
reactionClass?: string
noTooltip?: boolean
2025-02-05 21:02:51 +00:00
children?: Snippet
2025-02-04 00:37:14 +00:00
}
const {
2025-02-04 00:37:14 +00:00
event,
2025-05-12 22:10:24 +00:00
deleteReaction,
createReaction,
2025-02-04 00:37:14 +00:00
url = "",
reactionClass = "",
noTooltip = false,
children,
}: Props = $props()
2025-01-15 01:23:27 +00:00
const reports = deriveEvents(repository, {
filters: [{kinds: [REPORT], "#e": [event.id]}],
})
const reactions = deriveEvents(repository, {
filters: [{kinds: [REACTION], "#e": [event.id]}],
})
2025-07-04 13:22:19 +00:00
const zaps = deriveEventsMapped<Zap>(repository, {
filters: [{kinds: [ZAP_RESPONSE], "#e": [event.id]}],
itemToEvent: item => item.response,
eventToItem: (response: TrustedEvent) => getValidZap(response, event),
})
2025-05-12 22:10:24 +00:00
const onReactionClick = (events: TrustedEvent[]) => {
const reaction = events.find(e => e.pubkey === $pubkey)
if (reaction) {
deleteReaction(reaction)
} else {
const [event] = events
createReaction({
content: event.content,
tags: getEmojiTags(event.content.replace(/:/g, ""), event.tags),
})
}
}
2025-01-15 01:23:27 +00:00
const onReportClick = () => pushModal(EventReportDetails, {url, event})
const reportReasons = $derived(uniq($reports.map(e => getTag("e", e.tags)?.[2])))
2025-01-15 01:23:27 +00:00
2025-05-12 22:10:24 +00:00
const getReactionKey = (e: TrustedEvent) => getEmojiTag(e.content, e.tags)?.join("") || e.content
const groupedReactions = $derived(
2025-02-04 00:37:14 +00:00
groupBy(
2025-05-12 22:10:24 +00:00
getReactionKey,
uniqBy(e => `${e.pubkey}${getReactionKey(e)}`, $reactions),
2025-02-04 00:37:14 +00:00
),
2024-10-24 16:58:43 +00:00
)
2024-11-15 22:03:54 +00:00
2025-07-07 20:58:43 +00:00
const groupedZaps = $derived(groupBy(e => getReactionKey(e.request), $zaps))
2025-07-04 13:22:19 +00:00
2024-11-15 22:03:54 +00:00
onMount(() => {
2025-04-11 16:27:19 +00:00
const controller = new AbortController()
2025-04-09 22:32:18 +00:00
2025-01-17 23:01:05 +00:00
if (url) {
2025-04-11 16:27:19 +00:00
load({
2025-01-17 23:01:05 +00:00
relays: [url],
2025-04-11 16:27:19 +00:00
signal: controller.signal,
2025-07-17 21:39:59 +00:00
filters: getReplyFilters([event], {kinds: [REPORT, DELETE, ...REACTION_KINDS]}),
2025-04-11 16:27:19 +00:00
onEvent: batch(300, (events: TrustedEvent[]) => {
2025-01-17 23:01:05 +00:00
load({
relays: [url],
2025-04-11 21:44:27 +00:00
filters: getReplyFilters(events, {kinds: [DELETE]}),
2025-01-17 23:01:05 +00:00
})
}),
2025-04-11 16:27:19 +00:00
})
2025-04-09 22:32:18 +00:00
}
return () => {
2025-04-11 16:27:19 +00:00
controller.abort()
2025-01-17 23:01:05 +00:00
}
2024-11-15 22:03:54 +00:00
})
</script>
2025-07-04 13:22:19 +00:00
{#if $reactions.length > 0 || $zaps.length || $reports.length > 0}
2024-10-31 19:24:40 +00:00
<div class="flex min-w-0 flex-wrap gap-2">
2025-01-15 01:23:27 +00:00
{#if url && $reports.length > 0}
<button
type="button"
2025-05-13 21:31:34 +00:00
data-tip={`This content has been reported as "${displayList(reportReasons)}".`}
2025-01-15 01:23:27 +00:00
class="btn btn-error btn-xs tooltip-right flex items-center gap-1 rounded-full"
class:tooltip={!noTooltip && !isMobile}
2025-02-04 00:37:14 +00:00
onclick={stopPropagation(preventDefault(onReportClick))}>
2025-01-15 01:23:27 +00:00
<Icon icon="danger" />
<span>{$reports.length}</span>
</button>
{/if}
2025-07-04 13:22:19 +00:00
{#each groupedZaps.entries() as [key, zaps]}
2025-07-07 20:58:43 +00:00
{@const amount = fromMsats(sum(zaps.map(zap => zap.invoiceAmount)))}
{@const pubkeys = uniq(zaps.map(zap => zap.request.pubkey))}
2025-07-04 13:22:19 +00:00
{@const isOwn = $pubkey && pubkeys.includes($pubkey)}
{@const info = displayList(pubkeys.map(pubkey => displayProfileByPubkey(pubkey)))}
{@const tooltip = `${info} zapped`}
<button
type="button"
data-tip={tooltip}
class="flex-inline btn btn-neutral btn-xs gap-1 rounded-full {reactionClass}"
class:tooltip={!noTooltip && !isMobile}
class:border={isOwn}
class:border-solid={isOwn}
class:border-primary={isOwn}>
<Reaction event={zaps[0].request} />
<span>{amount}</span>
</button>
{/each}
2025-05-12 22:10:24 +00:00
{#each groupedReactions.entries() as [key, events]}
2024-11-18 23:41:58 +00:00
{@const pubkeys = events.map(e => e.pubkey)}
{@const isOwn = $pubkey && pubkeys.includes($pubkey)}
{@const info = displayList(pubkeys.map(pubkey => displayProfileByPubkey(pubkey)))}
2025-05-12 22:10:24 +00:00
{@const tooltip = `${info} reacted`}
{@const onClick = () => onReactionClick(events)}
2024-10-29 21:02:51 +00:00
<button
type="button"
2024-11-18 23:41:58 +00:00
data-tip={tooltip}
class="flex-inline btn btn-neutral btn-xs gap-1 rounded-full {reactionClass}"
class:tooltip={!noTooltip && !isMobile}
2024-10-29 21:02:51 +00:00
class:border={isOwn}
class:border-solid={isOwn}
class:border-primary={isOwn}
2025-02-04 00:37:14 +00:00
onclick={stopPropagation(preventDefault(onClick))}>
2025-05-12 22:10:24 +00:00
<Reaction event={events[0]} />
2024-10-29 21:02:51 +00:00
{#if events.length > 1}
<span>{events.length}</span>
{/if}
</button>
{/each}
2025-02-04 00:37:14 +00:00
{@render children?.()}
2024-10-29 21:02:51 +00:00
</div>
{/if}