flotilla/src/app/components/ChannelMessage.svelte

146 lines
5.6 KiB
Svelte
Raw Normal View History

2024-08-15 21:30:55 +00:00
<script lang="ts">
2024-10-17 23:03:50 +00:00
import {readable} from "svelte/store"
import {hash, ellipsize, uniqBy, groupBy} from "@welshman/lib"
2024-08-19 16:06:39 +00:00
import type {TrustedEvent} from "@welshman/util"
2024-10-17 23:03:50 +00:00
import {deriveEvents} from "@welshman/store"
import {deriveProfile, deriveProfileDisplay, formatTimestampAsTime, pubkey} from "@welshman/app"
2024-10-16 22:57:18 +00:00
import type {Thunk} from "@welshman/app"
2024-10-17 23:03:50 +00:00
import {REACTION, ZAP_RESPONSE} from "@welshman/util"
2024-08-30 23:25:41 +00:00
import {repository} from "@welshman/app"
2024-10-17 23:03:50 +00:00
import {slideAndFade, conditionalTransition} from "@lib/transition"
2024-08-16 17:50:38 +00:00
import Icon from "@lib/components/Icon.svelte"
import Delay from "@lib/components/Delay.svelte"
2024-08-16 17:50:38 +00:00
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-16 22:57:18 +00:00
import ThunkStatus from "@app/components/ThunkStatus.svelte"
import ProfileDetail from "@app/components/ProfileDetail.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-16 18:27:27 +00:00
import ChannelMessageMenuButton from "@app/components/ChannelMessageMenuButton.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"
import {pushModal, pushDrawer} 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-16 22:57:18 +00:00
export let thunk: Thunk
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-10-17 23:03:50 +00:00
const [_, colorValue] = colors[parseInt(hash(event.pubkey)) % colors.length]
2024-08-15 21:30:55 +00:00
const transition = conditionalTransition(thunk, slideAndFade)
const showProfile = () => pushDrawer(ProfileDetail, {pubkey: event.pubkey})
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-15 21:30:55 +00:00
</script>
<Delay>
<button
in:transition
on:click={openThread}
type="button"
class="group relative flex w-full flex-col gap-1 p-2 text-left transition-colors hover:bg-base-300">
{#if $rootEvent && !hideParent}
<div class="flex items-center gap-1 pl-12 text-xs">
<Icon icon="square-share-line" size={3} />
<p>In reply to</p>
<Avatar src={$rootProfile?.picture} size={4} />
<p class="text-primary">{$rootProfileDisplay}</p>
<p
class="flex items-center gap-1 overflow-hidden text-ellipsis whitespace-nowrap opacity-75 hover:underline">
{ellipsize($rootEvent.content, 30)}
</p>
</div>
2024-08-15 21:30:55 +00:00
{/if}
<div class="flex w-full gap-3">
2024-08-15 21:30:55 +00:00
{#if showPubkey}
<Button on:click={showProfile}>
2024-10-17 23:03:50 +00:00
<Avatar
src={$profile?.picture}
class="border border-solid border-base-content"
size={10} />
</Button>
{:else}
<div class="w-10 min-w-10 max-w-10" />
2024-08-15 21:30:55 +00:00
{/if}
<div class="-mt-1 flex-grow pr-1">
{#if showPubkey}
<div class="flex items-center gap-2">
2024-10-17 23:03:50 +00:00
<Button class="text-sm font-bold" style="color: {colorValue}" on:click={showProfile}>
{$profileDisplay}
</Button>
<span class="text-xs opacity-50">{formatTimestampAsTime(event.created_at)}</span>
2024-10-16 22:57:18 +00:00
</div>
2024-08-23 20:33:36 +00:00
{/if}
<div class="text-sm">
<Content {event} />
{#if thunk}
<ThunkStatus {thunk} />
2024-08-23 21:42:50 +00:00
{/if}
</div>
</div>
2024-08-23 21:42:50 +00:00
</div>
{#if $reactions.length > 0 || $zaps.length > 0}
<div class="ml-12 text-xs">
{#each groupBy( e => e.content, uniqBy(e => e.pubkey + e.content, $reactions), ).entries() as [content, events]}
{@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}>
<span>{displayReaction(content)}</span>
{#if events.length > 1}
<span>{events.length}</span>
{/if}
</button>
{/each}
</div>
{/if}
<button
class="join absolute right-1 top-1 border border-solid border-neutral text-xs opacity-0 transition-all group-hover:opacity-100"
on:click|stopPropagation>
<ChannelMessageEmojiButton {url} {room} {event} />
<ChannelMessageMenuButton {url} {room} {event} />
</button>
2024-10-01 18:47:21 +00:00
</button>
</Delay>