flotilla/src/app/components/ChatMessage.svelte

184 lines
6.9 KiB
Svelte
Raw Normal View History

2024-08-15 21:30:55 +00:00
<script lang="ts">
2024-08-29 16:23:54 +00:00
import {onMount} from "svelte"
2024-09-25 22:49:17 +00:00
import type {SvelteComponent} from "svelte"
2024-09-25 22:01:40 +00:00
import type {NativeEmoji} from 'emoji-picker-element/shared'
2024-08-16 22:17:30 +00:00
import twColors from "tailwindcss/colors"
2024-08-29 16:23:54 +00:00
import type {Readable} from "svelte/store"
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-08-23 21:42:50 +00:00
import {deriveEvents} 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
tagReactionTo,
tagEvent,
makeThunk,
publishThunk,
pubkey,
2024-08-30 23:25:41 +00:00
} from "@welshman/app"
import type {PublishStatusData} from "@welshman/app"
2024-09-25 20:46:10 +00:00
import {REACTION, DELETE, ZAP_RESPONSE, createEvent, displayRelayUrl, getAncestorTags} 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"
2024-08-23 21:42:50 +00:00
import Button from "@lib/components/Button.svelte"
2024-08-16 17:50:38 +00:00
import Avatar from "@lib/components/Avatar.svelte"
2024-09-24 23:30:32 +00:00
import Content from "@app/components/Content.svelte"
2024-10-01 18:47:21 +00:00
import ChatThread from '@app/components/ChatThread.svelte'
2024-09-25 20:46:10 +00:00
import ChatMessageEmojiButton from "@app/components/ChatMessageEmojiButton.svelte"
2024-10-01 18:47:21 +00:00
import {tagRoom, REPLY, deriveEvent, displayReaction} from "@app/state"
2024-09-25 22:49:17 +00:00
import {publishDelete, publishReaction} from "@app/commands"
2024-10-01 18:47:21 +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
2024-08-16 22:17:30 +00:00
const colors = [
2024-08-26 21:43:43 +00:00
["amber", twColors.amber[600]],
["blue", twColors.blue[600]],
["cyan", twColors.cyan[600]],
["emerald", twColors.emerald[600]],
["fuchsia", twColors.fuchsia[600]],
["green", twColors.green[600]],
["indigo", twColors.indigo[600]],
["sky", twColors.sky[600]],
["lime", twColors.lime[600]],
["orange", twColors.orange[600]],
["pink", twColors.pink[600]],
["purple", twColors.purple[600]],
["red", twColors.red[600]],
["rose", twColors.rose[600]],
["sky", twColors.sky[600]],
["teal", twColors.teal[600]],
["violet", twColors.violet[600]],
["yellow", twColors.yellow[600]],
["zinc", twColors.zinc[600]],
2024-08-16 22:17:30 +00:00
]
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-08-23 20:33:36 +00:00
const ps = derived(publishStatusData, $m => Object.values($m[event.id] || {}))
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
pushModal(ChatThread, {url, room, event: root}, {drawer: true})
}
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-09-25 22:49:17 +00:00
let drawer: SvelteComponent
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-01 18:47:21 +00:00
<button type="button" on:click={openThread} class="group relative flex flex-col gap-1 p-2 transition-colors hover:bg-base-300 text-left w-full">
{#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}
<div class="flex gap-2">
{#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-09-24 23:30:32 +00:00
<div class="text-sm">
<Content {event} />
2024-08-23 20:33:36 +00:00
{#if isPending}
2024-08-26 21:43:43 +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>
</span>
{:else if failure}
<span
2024-08-26 21:43:43 +00:00
class="flex-inline tooltip ml-1 cursor-pointer gap-1"
2024-08-23 20:33:36 +00:00
data-tip="{failure.message} ({displayRelayUrl(failure.url)})">
<Icon icon="danger" class="translate-y-px" size={3} />
<span class="opacity-50">Failed to send!</span>
</span>
{/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-09-25 20:46:10 +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
class="join absolute -top-2 right-0 border border-solid border-neutral text-xs opacity-0 transition-all group-hover:opacity-100"
on:click|stopPropagation>
2024-09-25 22:49:17 +00:00
<ChatMessageEmojiButton {url} {room} {event} />
2024-08-16 17:50:38 +00:00
<button class="btn join-item btn-xs">
<Icon icon="menu-dots" size={4} />
</button>
2024-10-01 18:47:21 +00:00
</button>
</button>