flotilla/src/app/components/ReactionSummary.svelte

244 lines
7.4 KiB
Svelte

<script lang="ts">
import cx from "classnames"
import {onMount} from "svelte"
import {derived} from "svelte/store"
import type {Snippet} from "svelte"
import {
batch,
call,
displayList,
groupBy,
map,
removeUndefined,
spec,
sum,
uniq,
uniqBy,
} from "@welshman/lib"
import {
REPORT,
REACTION,
ZAP_RECEIPT,
getReplyFilters,
fromMsats,
matchTag,
tagSpec,
userInbox,
DELETE,
} from "@welshman/util"
import type {TrustedEvent, EventContent} from "@welshman/util"
import {getEmojis} from "@welshman/domain"
import type {Zap} from "@welshman/domain"
import {Zappers} from "@welshman/app"
import {isMobile, stopPropagation} from "@lib/html"
import Danger from "@assets/icons/danger-triangle.svg?dataurl"
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
import {deriveEvents} from "@app/repository"
import Reaction from "@app/components/Reaction.svelte"
import ReportDetails from "@app/components/ReportDetails.svelte"
import ProfileList from "@app/components/ProfileList.svelte"
import ZapModal from "@app/components/Zap.svelte"
import {REACTION_KINDS} from "@app/content"
import {app, network, profiles, router, user} from "@app/core"
import {deriveUserIsSpaceAdmin} from "@app/management"
import {pushModal} from "@app/modal"
interface Props {
event: TrustedEvent
deleteReaction: (event: TrustedEvent) => void
createReaction: (event: EventContent) => void
url?: string
reactionClass?: string
noTooltip?: boolean
innerEvent?: TrustedEvent
children?: Snippet
}
const {
event,
deleteReaction,
createReaction,
url,
reactionClass = "",
noTooltip = false,
innerEvent = undefined,
children,
}: Props = $props()
const eventIds = innerEvent ? [event.id, innerEvent.id] : [event.id]
const reports = deriveEvents([{kinds: [REPORT], "#e": [event.id]}])
const reactions = deriveEvents([{kinds: [REACTION], "#e": eventIds}])
const receipts = deriveEvents([{kinds: [ZAP_RECEIPT], "#e": eventIds}])
// A receipt can be a zap of either the event or the one it wraps
const zaps = derived<typeof receipts, Zap[]>(
receipts,
($receipts, set) =>
derived(
removeUndefined([event, innerEvent]).map(
parent => $app.use(Zappers).validZapReceipts($receipts, parent).$,
),
(zapsByParent: Zap[][]) => uniqBy(zap => zap.response.id, zapsByParent.flat()),
).subscribe(set),
[],
)
const toggleReaction = (events: TrustedEvent[]) => {
const reaction = events.find(spec({pubkey: $user.pubkey}))
if (reaction) {
deleteReaction(reaction)
} else {
const [event] = events
const shortcode = event.content.replace(/:/g, "")
createReaction({
content: event.content,
tags: getEmojis(event)
.filter(spec({shortcode}))
.map(emoji => ["emoji", emoji.shortcode, emoji.url]),
})
}
}
const showReactors = (pubkeys: string[], title: string, subtitle: string) =>
pushModal(ProfileList, {title, subtitle, pubkeys, url})
const onReactionClick = (events: TrustedEvent[], pubkeys: string[], info: string) => {
if (isMobile && !pubkeys.includes($user.pubkey)) {
showReactors(pubkeys, info.replace(" reacted", ""), "Reacted to this message")
} else {
toggleReaction(events)
}
}
const onZapClick = (pubkeys: string[], info: string) => {
if (isMobile) {
showReactors(pubkeys, info.replace(" zapped", ""), "Zapped this message")
} else {
pushModal(ZapModal, {url, pubkey: event.pubkey, eventId: event.id})
}
}
const userIsAdmin = deriveUserIsSpaceAdmin(url)
const onReportClick = () => pushModal(ReportDetails, {url, event})
const reportReasons = $derived(
uniq(map(e => matchTag(tagSpec("e"), e.tags)?.[2], $reports.values())),
)
const getReactionKey = (e: TrustedEvent) => {
const shortcode = e.content.replace(/:/g, "")
const emoji = getEmojis(e).find(spec({shortcode}))
return emoji ? `emoji${emoji.shortcode}${emoji.url}` : e.content
}
const groupedReactions = $derived(
groupBy(
getReactionKey,
uniqBy(e => `${e.pubkey}${getReactionKey(e)}`, $reactions.values()),
),
)
const groupedZaps = $derived(groupBy(e => getReactionKey(e.request), $zaps.values()))
onMount(() => {
const controller = new AbortController()
call(async () => {
const relays = url ? [url] : await $router.resolver.relays([userInbox()])
if (relays.length > 0) {
$network.load({
relays,
signal: controller.signal,
filters: getReplyFilters([event], {kinds: REACTION_KINDS}),
onEvent: batch(300, (events: TrustedEvent[]) => {
$network.load({
relays,
filters: getReplyFilters(events, {kinds: [DELETE]}),
})
}),
})
}
})
return () => {
controller.abort()
}
})
</script>
{#if $reactions.length > 0 || $zaps.length || $reports.length > 0 || children}
<div class="flex min-w-0 flex-wrap gap-2">
{#if url && $reports.length > 0 && $userIsAdmin}
<Button
data-tip={`This content has been reported as "${displayList(reportReasons)}".`}
class={cx(
"button button-error button-xs tip-right flex items-center gap-1 rounded-full font-normal",
{
tip: !noTooltip && !isMobile,
},
)}
onclick={stopPropagation(onReportClick)}>
<Icon icon={Danger} />
<span>{$reports.length}</span>
</Button>
{/if}
{#each groupedZaps.entries() as [key, zaps]}
{@const amount = fromMsats(sum(zaps.map(zap => zap.invoiceAmount)))}
{@const pubkeys = uniq(zaps.map(zap => zap.request.pubkey))}
{@const isOwn = pubkeys.includes($user.pubkey)}
{@const info = displayList(
pubkeys.map(pubkey => $profiles.display(pubkey, removeUndefined([url])).get()),
)}
{@const tooltip = `${info} zapped`}
{@const onZapClickHandler = () => onZapClick(pubkeys, tooltip)}
<Button
data-tip={tooltip}
class={cx(
reactionClass,
"button button-xs flex-inline flex items-center gap-1 rounded-full text-xs font-normal",
{
tip: !noTooltip && !isMobile,
"button-primary": isOwn,
"button-neutral": !isOwn,
},
)}
onclick={stopPropagation(onZapClickHandler)}>
<Reaction event={zaps[0].request} />
<span>{amount}</span>
</Button>
{/each}
{#each groupedReactions.entries() as [key, events]}
{@const pubkeys = events.map(e => e.pubkey)}
{@const isOwn = pubkeys.includes($user.pubkey)}
{@const info = displayList(
pubkeys.map(pubkey => $profiles.display(pubkey, removeUndefined([url])).get()),
)}
{@const tooltip = `${info} reacted`}
{@const onClick = () => onReactionClick(events, pubkeys, info)}
<Button
data-tip={tooltip}
class={cx(reactionClass, "button button-xs flex-inline gap-1 rounded-full font-normal", {
tip: !noTooltip && !isMobile,
"button-primary": isOwn,
"button-neutral": !isOwn,
})}
onclick={stopPropagation(onClick)}>
<Reaction event={events[0]} />
{#if events.length > 1}
<span>{events.length}</span>
{/if}
</Button>
{/each}
{@render children?.()}
</div>
{/if}