flotilla/src/app/components/ChannelMessage.svelte

160 lines
6.1 KiB
Svelte
Raw Normal View History

2024-08-15 21:30:55 +00:00
<script lang="ts">
2024-08-23 20:33:36 +00:00
import {readable, derived} from "svelte/store"
2024-10-01 18:47:21 +00:00
import {hash, ellipsize, uniqBy, groupBy, now} from "@welshman/lib"
2024-08-19 16:06:39 +00:00
import type {TrustedEvent} from "@welshman/util"
2024-10-15 20:05:01 +00:00
import {deriveEvents, throttled} from "@welshman/store"
2024-08-23 20:33:36 +00:00
import {PublishStatus} from "@welshman/net"
2024-08-30 23:25:41 +00:00
import {
publishStatusData,
deriveProfile,
deriveProfileDisplay,
formatTimestampAsTime,
2024-09-25 20:46:10 +00:00
pubkey,
2024-08-30 23:25:41 +00:00
} from "@welshman/app"
import type {PublishStatusData} from "@welshman/app"
2024-10-08 18:39:16 +00:00
import {REACTION, ZAP_RESPONSE, displayRelayUrl} from "@welshman/util"
2024-08-30 23:25:41 +00:00
import {repository} from "@welshman/app"
2024-08-16 17:50:38 +00:00
import Icon from "@lib/components/Icon.svelte"
import Avatar from "@lib/components/Avatar.svelte"
2024-10-15 21:00:15 +00:00
import Button from "@lib/components/Button.svelte"
2024-09-24 23:30:32 +00:00
import Content from "@app/components/Content.svelte"
2024-10-15 21:00:15 +00:00
import EventInfo from "@app/components/EventInfo.svelte"
2024-10-08 18:39:16 +00:00
import ChannelThread from "@app/components/ChannelThread.svelte"
2024-10-03 21:43:17 +00:00
import ChannelMessageEmojiButton from "@app/components/ChannelMessageEmojiButton.svelte"
2024-10-08 18:39:16 +00:00
import {colors, tagRoom, deriveEvent, displayReaction} from "@app/state"
2024-09-25 22:49:17 +00:00
import {publishDelete, publishReaction} from "@app/commands"
2024-10-08 18:39:16 +00:00
import {pushModal} from "@app/modal"
2024-08-15 21:30:55 +00:00
2024-09-25 20:46:10 +00:00
export let url
export let room
2024-08-19 16:06:39 +00:00
export let event: TrustedEvent
2024-10-01 18:47:21 +00:00
export let showPubkey = false
export let hideParent = false
2024-08-15 21:30:55 +00:00
const profile = deriveProfile(event.pubkey)
2024-08-20 20:20:45 +00:00
const profileDisplay = deriveProfileDisplay(event.pubkey)
2024-08-26 21:43:43 +00:00
const reactions = deriveEvents(repository, {filters: [{kinds: [REACTION], "#e": [event.id]}]})
const zaps = deriveEvents(repository, {filters: [{kinds: [ZAP_RESPONSE], "#e": [event.id]}]})
2024-10-01 18:47:21 +00:00
const rootTag = event.tags.find(t => t[0].match(/^e$/i))
const rootId = rootTag?.[1]
const rootHints = [rootTag?.[2]].filter(Boolean) as string[]
const rootEvent = rootId ? deriveEvent(rootId, rootHints) : readable(null)
2024-08-16 22:17:30 +00:00
const [colorName, colorValue] = colors[parseInt(hash(event.pubkey)) % colors.length]
2024-10-15 22:52:30 +00:00
const ps = throttled(
300,
derived(publishStatusData, $m => Object.values($m[event.id] || {})),
)
2024-08-23 20:33:36 +00:00
2024-10-15 21:00:15 +00:00
const showInfo = () => pushModal(EventInfo, {event})
2024-08-23 20:33:36 +00:00
const findStatus = ($ps: PublishStatusData[], statuses: PublishStatus[]) =>
$ps.find(({status}) => statuses.includes(status))
2024-08-15 21:30:55 +00:00
2024-10-01 18:47:21 +00:00
const openThread = () => {
const root = $rootEvent || event
2024-10-03 21:43:17 +00:00
pushModal(ChannelThread, {url, room, event: root}, {drawer: true})
2024-10-01 18:47:21 +00:00
}
2024-09-25 20:46:10 +00:00
const onReactionClick = (content: string, events: TrustedEvent[]) => {
const reaction = events.find(e => e.pubkey === $pubkey)
if (reaction) {
2024-09-25 22:49:17 +00:00
publishDelete({relays: [url], event: reaction})
2024-09-25 20:46:10 +00:00
} else {
2024-09-25 22:49:17 +00:00
publishReaction({
event,
content,
relays: [url],
2024-10-01 18:47:21 +00:00
tags: [tagRoom(room, url)],
2024-09-25 22:49:17 +00:00
})
2024-09-25 20:46:10 +00:00
}
}
2024-10-01 18:47:21 +00:00
$: rootPubkey = $rootEvent?.pubkey || rootTag?.[4]
$: rootProfile = deriveProfile(rootPubkey || "")
$: rootProfileDisplay = deriveProfileDisplay(rootPubkey || "")
2024-08-23 20:33:36 +00:00
$: isPublished = findStatus($ps, [PublishStatus.Success])
2024-08-23 21:42:50 +00:00
$: isPending = findStatus($ps, [PublishStatus.Pending]) && event.created_at > now() - 30
2024-08-26 21:43:43 +00:00
$: failure =
!isPending && !isPublished && findStatus($ps, [PublishStatus.Failure, PublishStatus.Timeout])
2024-08-15 21:30:55 +00:00
</script>
2024-10-08 18:39:16 +00:00
<button
type="button"
on:click={openThread}
class="group relative flex w-full flex-col gap-1 p-2 text-left transition-colors hover:bg-base-300">
2024-10-01 18:47:21 +00:00
{#if $rootEvent && !hideParent}
2024-08-16 17:50:38 +00:00
<div class="flex items-center gap-1 pl-12 text-xs">
2024-10-01 18:47:21 +00:00
<Icon icon="square-share-line" size={3} />
<p>In reply to</p>
<Avatar src={$rootProfile?.picture} size={4} />
<p class="text-primary">{$rootProfileDisplay}</p>
2024-08-16 17:50:38 +00:00
<p
2024-10-01 18:47:21 +00:00
class="flex items-center gap-1 overflow-hidden text-ellipsis whitespace-nowrap opacity-75 hover:underline">
{ellipsize($rootEvent.content, 30)}
2024-08-16 17:50:38 +00:00
</p>
2024-08-15 21:30:55 +00:00
</div>
{/if}
2024-10-15 22:52:30 +00:00
<div class="flex w-full gap-2">
2024-08-15 21:30:55 +00:00
{#if showPubkey}
<Avatar src={$profile?.picture} class="border border-solid border-base-content" size={10} />
{:else}
2024-08-26 21:43:43 +00:00
<div class="w-10 min-w-10 max-w-10" />
2024-08-15 21:30:55 +00:00
{/if}
2024-09-24 23:30:32 +00:00
<div class="-mt-1 flex-grow pr-1">
2024-08-15 21:30:55 +00:00
{#if showPubkey}
2024-08-26 21:43:43 +00:00
<div class="flex items-center gap-2">
<strong class="text-sm" style="color: {colorValue}" data-color={colorName}
>{$profileDisplay}</strong>
<span class="text-xs opacity-50">{formatTimestampAsTime(event.created_at)}</span>
2024-08-23 21:42:50 +00:00
</div>
2024-08-15 21:30:55 +00:00
{/if}
2024-10-15 20:12:38 +00:00
<div class="text-sm">
2024-09-24 23:30:32 +00:00
<Content {event} />
2024-08-23 20:33:36 +00:00
{#if isPending}
2024-10-15 20:12:38 +00:00
<span class="flex-inline ml-1 gap-1">
<span class="loading loading-spinner mx-1 h-3 w-3 translate-y-px" />
2024-08-23 20:33:36 +00:00
<span class="opacity-50">Sending...</span>
2024-10-15 20:12:38 +00:00
</span>
2024-08-23 20:33:36 +00:00
{:else if failure}
2024-10-15 20:12:38 +00:00
<span
class="flex-inline tooltip ml-1 cursor-pointer gap-1"
data-tip="{failure.message} ({displayRelayUrl(failure.url)})">
<Icon icon="danger" class="translate-y-px" size={3} />
2024-08-23 20:33:36 +00:00
<span class="opacity-50">Failed to send!</span>
2024-10-15 20:12:38 +00:00
</span>
2024-08-23 20:33:36 +00:00
{/if}
2024-09-24 23:30:32 +00:00
</div>
2024-08-15 21:30:55 +00:00
</div>
</div>
2024-08-23 21:42:50 +00:00
{#if $reactions.length > 0 || $zaps.length > 0}
2024-08-26 21:43:43 +00:00
<div class="ml-12 text-xs">
2024-10-15 22:52:30 +00:00
{#each groupBy( e => e.content, uniqBy(e => e.pubkey + e.content, $reactions), ).entries() as [content, events]}
2024-10-01 18:47:21 +00:00
{@const isOwn = events.some(e => e.pubkey === $pubkey)}
{@const onClick = () => onReactionClick(content, events)}
<button
type="button"
class="flex-inline btn btn-neutral btn-xs mr-2 gap-1 rounded-full"
class:border={isOwn}
class:border-solid={isOwn}
class:border-primary={isOwn}
on:click|stopPropagation={onClick}>
2024-08-23 21:42:50 +00:00
<span>{displayReaction(content)}</span>
{#if events.length > 1}
<span>{events.length}</span>
{/if}
2024-10-01 18:47:21 +00:00
</button>
2024-08-23 21:42:50 +00:00
{/each}
</div>
{/if}
2024-10-01 18:47:21 +00:00
<button
2024-10-15 22:52:30 +00:00
class="join absolute right-1 top-1 border border-solid border-neutral text-xs opacity-0 transition-all group-hover:opacity-100"
2024-10-01 18:47:21 +00:00
on:click|stopPropagation>
2024-10-03 21:43:17 +00:00
<ChannelMessageEmojiButton {url} {room} {event} />
2024-10-15 21:00:15 +00:00
<Button class="btn join-item btn-xs" on:click={showInfo}>
<Icon size={4} icon="code-2" />
</Button>
2024-10-01 18:47:21 +00:00
</button>
</button>