2024-10-24 16:45:27 +00:00
|
|
|
<script lang="ts">
|
2025-10-30 19:53:21 +00:00
|
|
|
import cx from "classnames"
|
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-11-20 23:54:06 +00:00
|
|
|
import {groupBy, map, sum, uniq, uniqBy, batch, displayList} from "@welshman/lib"
|
2025-05-12 22:10:24 +00:00
|
|
|
import {
|
2025-10-06 18:23:19 +00:00
|
|
|
REPORT,
|
2025-05-12 22:10:24 +00:00
|
|
|
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,
|
|
|
|
|
DELETE,
|
|
|
|
|
} from "@welshman/util"
|
2025-07-04 13:22:19 +00:00
|
|
|
import type {TrustedEvent, EventContent, Zap} from "@welshman/util"
|
2025-11-20 23:54:06 +00:00
|
|
|
import {deriveArray, deriveEventsById, deriveItemsByKey} from "@welshman/store"
|
2025-04-11 16:27:19 +00:00
|
|
|
import {load} from "@welshman/net"
|
2026-06-18 10:52:04 +00:00
|
|
|
import {Router} from "@welshman/router"
|
2025-07-04 13:22:19 +00:00
|
|
|
import {pubkey, repository, getValidZap, displayProfileByPubkey} from "@welshman/app"
|
2026-06-24 20:56:53 +00:00
|
|
|
import {isMobile} from "@lib/html"
|
2025-09-10 22:26:39 +00:00
|
|
|
import Danger from "@assets/icons/danger-triangle.svg?dataurl"
|
2025-01-15 01:23:27 +00:00
|
|
|
import Icon from "@lib/components/Icon.svelte"
|
2026-06-24 20:56:53 +00:00
|
|
|
import Button from "@lib/components/Button.svelte"
|
2025-05-12 22:10:24 +00:00
|
|
|
import Reaction from "@app/components/Reaction.svelte"
|
2025-11-14 19:35:19 +00:00
|
|
|
import ReportDetails from "@app/components/ReportDetails.svelte"
|
2026-06-08 22:33:38 +00:00
|
|
|
import {REACTION_KINDS} from "@app/content"
|
|
|
|
|
import {deriveUserIsSpaceAdmin} from "@app/members"
|
2026-06-08 21:14:40 +00:00
|
|
|
import {pushModal} from "@app/modal"
|
2024-10-24 16:45:27 +00:00
|
|
|
|
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
|
2026-04-10 22:27:36 +00:00
|
|
|
innerEvent?: TrustedEvent
|
2025-02-05 21:02:51 +00:00
|
|
|
children?: Snippet
|
2025-02-04 00:37:14 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-04 01:21:46 +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,
|
2026-04-10 22:27:36 +00:00
|
|
|
innerEvent = undefined,
|
2025-02-04 00:37:14 +00:00
|
|
|
children,
|
|
|
|
|
}: Props = $props()
|
2024-10-24 16:45:27 +00:00
|
|
|
|
2026-04-10 22:27:36 +00:00
|
|
|
const eventIds = innerEvent ? [event.id, innerEvent.id] : [event.id]
|
|
|
|
|
|
2025-11-20 23:54:06 +00:00
|
|
|
const reports = deriveArray(
|
|
|
|
|
deriveEventsById({repository, filters: [{kinds: [REPORT], "#e": [event.id]}]}),
|
|
|
|
|
)
|
2025-01-15 01:23:27 +00:00
|
|
|
|
2025-11-20 23:54:06 +00:00
|
|
|
const reactions = deriveArray(
|
2026-04-10 22:27:36 +00:00
|
|
|
deriveEventsById({repository, filters: [{kinds: [REACTION], "#e": eventIds}]}),
|
2025-11-20 23:54:06 +00:00
|
|
|
)
|
2024-10-24 16:45:27 +00:00
|
|
|
|
2025-11-20 23:54:06 +00:00
|
|
|
const zaps = deriveArray(
|
|
|
|
|
deriveItemsByKey<Zap>({
|
|
|
|
|
repository,
|
|
|
|
|
getKey: zap => zap.response.id,
|
2026-04-10 22:27:36 +00:00
|
|
|
filters: [{kinds: [ZAP_RESPONSE], "#e": eventIds}],
|
|
|
|
|
eventToItem: (response: TrustedEvent) => {
|
|
|
|
|
const zap = getValidZap(response, event)
|
|
|
|
|
|
|
|
|
|
if (zap) {
|
|
|
|
|
return zap
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (innerEvent) {
|
|
|
|
|
return getValidZap(response, innerEvent)
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-11-20 23:54:06 +00:00
|
|
|
}),
|
|
|
|
|
)
|
2025-07-04 13:22:19 +00:00
|
|
|
|
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),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-03 23:54:20 +00:00
|
|
|
const userIsAdmin = deriveUserIsSpaceAdmin(url)
|
|
|
|
|
|
2025-11-14 19:35:19 +00:00
|
|
|
const onReportClick = () => pushModal(ReportDetails, {url, event})
|
2025-01-15 01:23:27 +00:00
|
|
|
|
2025-11-20 23:54:06 +00:00
|
|
|
const reportReasons = $derived(uniq(map(e => getTag("e", e.tags)?.[2], $reports.values())))
|
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
|
|
|
|
|
|
2025-02-04 01:21:46 +00:00
|
|
|
const groupedReactions = $derived(
|
2025-02-04 00:37:14 +00:00
|
|
|
groupBy(
|
2025-05-12 22:10:24 +00:00
|
|
|
getReactionKey,
|
2025-11-20 23:54:06 +00:00
|
|
|
uniqBy(e => `${e.pubkey}${getReactionKey(e)}`, $reactions.values()),
|
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-11-20 23:54:06 +00:00
|
|
|
const groupedZaps = $derived(groupBy(e => getReactionKey(e.request), $zaps.values()))
|
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()
|
2026-06-18 10:52:04 +00:00
|
|
|
const relays = url ? [url] : Router.get().ForUser().getUrls()
|
2025-04-09 22:32:18 +00:00
|
|
|
|
2026-06-18 10:52:04 +00:00
|
|
|
if (relays.length > 0) {
|
2025-04-11 16:27:19 +00:00
|
|
|
load({
|
2026-06-18 10:52:04 +00:00
|
|
|
relays,
|
2025-04-11 16:27:19 +00:00
|
|
|
signal: controller.signal,
|
2025-10-06 18:23:19 +00:00
|
|
|
filters: getReplyFilters([event], {kinds: REACTION_KINDS}),
|
2025-04-11 16:27:19 +00:00
|
|
|
onEvent: batch(300, (events: TrustedEvent[]) => {
|
2025-01-17 23:01:05 +00:00
|
|
|
load({
|
2026-06-18 10:52:04 +00:00
|
|
|
relays,
|
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
|
|
|
})
|
2024-10-24 16:45:27 +00:00
|
|
|
</script>
|
|
|
|
|
|
2026-02-03 17:49:44 +00:00
|
|
|
{#if $reactions.length > 0 || $zaps.length || $reports.length > 0 || children}
|
2024-10-31 19:24:40 +00:00
|
|
|
<div class="flex min-w-0 flex-wrap gap-2">
|
2026-04-03 23:54:20 +00:00
|
|
|
{#if url && $reports.length > 0 && $userIsAdmin}
|
2026-06-24 20:56:53 +00:00
|
|
|
<Button
|
2025-05-13 21:31:34 +00:00
|
|
|
data-tip={`This content has been reported as "${displayList(reportReasons)}".`}
|
2026-06-24 20:56:53 +00:00
|
|
|
class={cx(
|
|
|
|
|
"button button-error button-xs tip-right flex items-center gap-1 rounded-full font-normal",
|
|
|
|
|
{
|
|
|
|
|
tip: !noTooltip && !isMobile,
|
|
|
|
|
},
|
|
|
|
|
)}
|
|
|
|
|
onclick={onReportClick}>
|
2025-09-10 22:26:39 +00:00
|
|
|
<Icon icon={Danger} />
|
2025-01-15 01:23:27 +00:00
|
|
|
<span>{$reports.length}</span>
|
2026-06-24 20:56:53 +00:00
|
|
|
</Button>
|
2025-01-15 01:23:27 +00:00
|
|
|
{/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`}
|
2026-06-24 20:56:53 +00:00
|
|
|
<Button
|
2025-07-04 13:22:19 +00:00
|
|
|
data-tip={tooltip}
|
2025-10-30 19:53:21 +00:00
|
|
|
class={cx(
|
|
|
|
|
reactionClass,
|
2026-06-24 20:56:53 +00:00
|
|
|
"button button-xs flex-inline flex items-center gap-1 rounded-full text-xs font-normal",
|
2025-10-30 19:53:21 +00:00
|
|
|
{
|
2026-06-24 20:56:53 +00:00
|
|
|
tip: !noTooltip && !isMobile,
|
|
|
|
|
"button-primary": isOwn,
|
|
|
|
|
"button-neutral": !isOwn,
|
2025-10-30 19:53:21 +00:00
|
|
|
},
|
|
|
|
|
)}>
|
2025-07-04 13:22:19 +00:00
|
|
|
<Reaction event={zaps[0].request} />
|
|
|
|
|
<span>{amount}</span>
|
2026-06-24 20:56:53 +00:00
|
|
|
</Button>
|
2025-07-04 13:22:19 +00:00
|
|
|
{/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)}
|
2026-06-24 20:56:53 +00:00
|
|
|
<Button
|
2024-11-18 23:41:58 +00:00
|
|
|
data-tip={tooltip}
|
2026-06-24 20:56:53 +00:00
|
|
|
class={cx(reactionClass, "button button-xs flex-inline gap-1 rounded-full font-normal", {
|
|
|
|
|
tip: !noTooltip && !isMobile,
|
|
|
|
|
"button-primary": isOwn,
|
|
|
|
|
"button-neutral": !isOwn,
|
|
|
|
|
})}
|
|
|
|
|
onclick={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}
|
2026-06-24 20:56:53 +00:00
|
|
|
</Button>
|
2024-10-29 21:02:51 +00:00
|
|
|
{/each}
|
2025-02-04 00:37:14 +00:00
|
|
|
{@render children?.()}
|
2024-10-29 21:02:51 +00:00
|
|
|
</div>
|
|
|
|
|
{/if}
|