flotilla/src/app/components/ReactionSummary.svelte

89 lines
3 KiB
Svelte
Raw Normal View History

<script lang="ts">
2024-11-15 22:03:54 +00:00
import {onMount} from "svelte"
2025-01-15 01:23:27 +00:00
import {groupBy, uniq, uniqBy, batch} from "@welshman/lib"
import {REACTION, getTag, REPORT, DELETE} from "@welshman/util"
2024-12-11 00:38:22 +00:00
import type {TrustedEvent} from "@welshman/util"
import {deriveEvents} from "@welshman/store"
2024-11-18 23:41:58 +00:00
import {pubkey, repository, load, displayProfileByPubkey} from "@welshman/app"
import {displayList} from "@lib/util"
import {isMobile} from "@lib/html"
2025-01-15 01:23:27 +00:00
import Icon from "@lib/components/Icon.svelte"
import EventReportDetails from "@app/components/EventReportDetails.svelte"
import {displayReaction} from "@app/state"
2025-01-15 01:23:27 +00:00
import {pushModal} from "@app/modal"
export let event
export let onReactionClick
2025-01-15 01:23:27 +00:00
export let url = ""
export let reactionClass = ""
export let noTooltip = false
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-01-15 01:23:27 +00:00
const onReportClick = () => pushModal(EventReportDetails, {url, event})
$: reportReasons = uniq($reports.map(e => getTag("e", e.tags)?.[2]))
2024-10-24 16:58:43 +00:00
$: groupedReactions = groupBy(
e => e.content,
uniqBy(e => e.pubkey + e.content, $reactions),
)
2024-11-15 22:03:54 +00:00
onMount(() => {
2024-12-11 00:38:22 +00:00
load({
2025-01-15 01:23:27 +00:00
relays: [url],
filters: [{kinds: [REACTION, REPORT, DELETE], "#e": [event.id]}],
2024-12-11 00:38:22 +00:00
onEvent: batch(300, (events: TrustedEvent[]) => {
load({
2025-01-15 01:23:27 +00:00
relays: [url],
2024-12-11 00:38:22 +00:00
filters: [{kinds: [DELETE], "#e": events.map(e => e.id)}],
})
}),
})
2024-11-15 22:03:54 +00:00
})
</script>
2025-01-15 01:23:27 +00:00
{#if $reactions.length > 0 || $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"
data-tip={`This content has been reported as "${displayList(reportReasons)}".`}}
class="btn btn-error btn-xs tooltip-right flex items-center gap-1 rounded-full"
class:tooltip={!noTooltip && !isMobile}
on:click|preventDefault|stopPropagation={onReportClick}>
<Icon icon="danger" />
<span>{$reports.length}</span>
</button>
{/if}
2024-10-29 21:02:51 +00:00
{#each groupedReactions.entries() as [content, 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)))}
{@const tooltip = `${info} reacted ${displayReaction(content)}`}
2024-10-29 21:02:51 +00:00
{@const onClick = () => onReactionClick(content, events)}
<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}
2024-11-15 22:03:54 +00:00
on:click|preventDefault|stopPropagation={onClick}>
2024-10-29 21:02:51 +00:00
<span>{displayReaction(content)}</span>
{#if events.length > 1}
<span>{events.length}</span>
{/if}
</button>
{/each}
2024-10-31 19:24:40 +00:00
<slot />
2024-10-29 21:02:51 +00:00
</div>
{/if}