flotilla/src/app/components/RoomItem.svelte

169 lines
6.3 KiB
Svelte
Raw Normal View History

2024-08-15 21:30:55 +00:00
<script lang="ts">
2025-10-06 18:23:19 +00:00
import cx from "classnames"
2026-07-28 16:16:26 +00:00
import {get, readable} from "svelte/store"
2026-04-10 22:27:36 +00:00
import {
hash,
gte,
now,
displayList,
formatTimestampAsTime,
formatTimestampAsDate,
} from "@welshman/lib"
2025-05-12 22:10:24 +00:00
import type {TrustedEvent, EventContent} from "@welshman/util"
2026-08-14 15:47:36 +00:00
import {MESSAGE, COMMENT, getIdOrAddress, matchTag, tagSpec, tagValue} from "@welshman/util"
2024-10-24 22:48:45 +00:00
import {isMobile} from "@lib/html"
2025-10-06 18:23:19 +00:00
import Pen from "@assets/icons/pen.svg?dataurl"
import Reply from "@assets/icons/reply-2.svg?dataurl"
import ReplyAlt from "@assets/icons/reply.svg?dataurl"
2025-03-03 21:59:38 +00:00
import TapTarget from "@lib/components/TapTarget.svelte"
import Icon from "@lib/components/Icon.svelte"
2025-10-06 18:23:19 +00:00
import Link from "@lib/components/Link.svelte"
import Button from "@lib/components/Button.svelte"
import ThunkFailure from "@app/components/ThunkFailure.svelte"
2026-07-28 16:16:26 +00:00
import {publishReaction, retractReaction} from "@app/reactions"
2024-12-16 20:54:17 +00:00
import ProfileDetail from "@app/components/ProfileDetail.svelte"
import ProfileCircle from "@app/components/ProfileCircle.svelte"
import ReactionSummary from "@app/components/ReactionSummary.svelte"
2025-10-30 22:36:14 +00:00
import RoomItemZapButton from "@app/components/RoomItemZapButton.svelte"
import RoomItemEmojiButton from "@app/components/RoomItemEmojiButton.svelte"
import RoomItemMenuButton from "@app/components/RoomItemMenuButton.svelte"
import RoomItemMenuMobile from "@app/components/RoomItemMenuMobile.svelte"
import RoomItemContent from "@app/components/RoomItemContent.svelte"
2026-07-28 16:16:26 +00:00
import {profiles, thunks, user} from "@app/core"
import {colors} from "@app/theme"
import {ENABLE_ZAPS} from "@app/env"
2026-07-28 16:16:26 +00:00
import {deriveEvent, deriveEventsForUrl} from "@app/repository"
2026-08-14 15:47:36 +00:00
import {makeContentPath} from "@app/routes"
2026-06-08 21:14:40 +00:00
import {pushModal} from "@app/modal"
2024-08-15 21:30:55 +00:00
2026-07-28 16:16:26 +00:00
type Props = {
2025-04-15 18:03:27 +00:00
url: string
2025-02-04 00:37:14 +00:00
event: TrustedEvent
2025-04-15 18:03:27 +00:00
replyTo?: (event: TrustedEvent) => void
2025-02-04 00:37:14 +00:00
showPubkey?: boolean
canEdit: (event: TrustedEvent) => boolean
onEdit: (event: TrustedEvent) => void
2025-02-04 00:37:14 +00:00
}
2026-06-04 21:31:07 +00:00
const {url, event, replyTo = undefined, showPubkey = false, canEdit, onEdit}: Props = $props()
2024-08-15 21:30:55 +00:00
2026-08-14 15:47:36 +00:00
const path = makeContentPath(url, event.kind, getIdOrAddress(event))
2026-07-28 16:16:26 +00:00
const h = tagValue(tagSpec("h"), event.tags)
2025-01-24 21:35:09 +00:00
const today = formatTimestampAsDate(now())
2026-07-28 16:16:26 +00:00
const profileDisplay = $profiles.display(event.pubkey, [url]).$
const thunk = $thunks.merge(get($thunks.history).filter(t => t.event.id === event.id))
2025-10-21 16:26:06 +00:00
const [_, colorValue] = colors[hash(event.pubkey) % colors.length]
2026-04-10 22:27:36 +00:00
2026-07-28 16:16:26 +00:00
const qTag = matchTag(tagSpec("q"), event.tags)
2026-04-10 22:27:36 +00:00
const isQuoteOnly = Boolean(
gte(qTag?.length, 2) && event.content.trim().match(/^nostr:n(event|addr)1\w+\s*$/),
)
const innerComments = isQuoteOnly
? deriveEventsForUrl(url, [{kinds: [COMMENT], "#e": [qTag![1]]}])
: readable([])
const innerEvent = isQuoteOnly ? deriveEvent(qTag![1], [url]) : readable(undefined)
2024-08-15 21:30:55 +00:00
2025-04-15 18:03:27 +00:00
const reply = () => replyTo!(event)
const edit = canEdit(event) ? () => onEdit(event) : undefined
2025-10-30 22:36:14 +00:00
const onTap = () => pushModal(RoomItemMenuMobile, {url, event, reply, edit})
2024-10-25 23:59:12 +00:00
2025-04-15 18:03:27 +00:00
const openProfile = () => pushModal(ProfileDetail, {pubkey: event.pubkey, url})
2024-12-16 20:54:17 +00:00
2026-07-28 16:16:26 +00:00
const deleteReaction = (reaction: TrustedEvent) => retractReaction(reaction, {url, h})
2024-09-25 20:46:10 +00:00
2026-07-28 16:16:26 +00:00
const createReaction = (values: EventContent) => publishReaction(event, values, {url, h})
2024-08-15 21:30:55 +00:00
</script>
2025-03-03 21:59:38 +00:00
<TapTarget
data-event={event.id}
2026-04-10 22:27:36 +00:00
{onTap}
class={cx(
"room__item group relative flex w-full cursor-default flex-col px-2 py-0.5 text-left transition-colors",
2026-06-04 21:31:07 +00:00
{"mt-1.5": showPubkey},
)}>
<div class="flex w-full gap-3 overflow-auto">
2024-10-25 23:59:12 +00:00
{#if showPubkey}
<Button onclick={openProfile} class="flex items-start pt-1.5 justify-center w-8 shrink-0">
<ProfileCircle
pubkey={event.pubkey}
class="border border-solid"
style="border-color: var(--line)"
size={8} />
2024-12-16 20:54:17 +00:00
</Button>
2024-10-25 23:59:12 +00:00
{:else}
<div class="w-8 shrink-0"></div>
2024-10-25 23:59:12 +00:00
{/if}
2026-04-07 21:53:19 +00:00
<div class="min-w-0 grow pr-1">
2024-08-15 21:30:55 +00:00
{#if showPubkey}
2024-10-25 23:59:12 +00:00
<div class="flex items-center gap-2">
<Button onclick={openProfile} class="text-sm font-bold" style="color: {colorValue}">
{$profileDisplay}
2024-12-16 20:54:17 +00:00
</Button>
2025-01-24 21:35:09 +00:00
<span class="text-xs opacity-50">
{#if formatTimestampAsDate(event.created_at) === today}
Today
{:else}
{formatTimestampAsDate(event.created_at)}
{/if}
at {formatTimestampAsTime(event.created_at)}
</span>
2024-10-25 23:59:12 +00:00
</div>
2024-08-15 21:30:55 +00:00
{/if}
2025-10-06 18:23:19 +00:00
<div class:mt-2={showPubkey && event.kind !== MESSAGE}>
2026-04-10 22:27:36 +00:00
<RoomItemContent {url} event={$innerEvent ?? event} />
2024-10-25 23:59:12 +00:00
{#if thunk}
<ThunkFailure showToastOnRetry {thunk} class="mt-1 flex justify-end" />
2024-08-23 20:33:36 +00:00
{/if}
</div>
2024-08-23 21:42:50 +00:00
</div>
2024-10-25 23:59:12 +00:00
</div>
<div class="flex gap-2 ml-10 mt-1 pl-1">
2025-05-12 22:10:24 +00:00
<ReactionSummary
{url}
{event}
{deleteReaction}
{createReaction}
reactionClass="tip-right"
2026-04-10 22:27:36 +00:00
innerEvent={$innerEvent} />
{#if path && $innerComments.length > 0}
{@const pubkeys = $innerComments.map(e => e.pubkey)}
2026-07-28 16:16:26 +00:00
{@const isOwn = pubkeys.includes($user.pubkey)}
{@const info = displayList(pubkeys.map(pk => $profiles.display(pk).get()))}
2025-10-06 18:23:19 +00:00
{@const tooltip = `${info} commented`}
<div data-tip={tooltip} class="tip tip-right flex">
2025-10-06 18:23:19 +00:00
<Link
href={path}
class={cx("button button-xs gap-1 rounded-full", {
"button-neutral": !isOwn,
"button-primary": isOwn,
2025-10-06 18:23:19 +00:00
})}>
<Icon icon={ReplyAlt} />
2026-04-10 22:27:36 +00:00
<span>{$innerComments.length} comment{$innerComments.length === 1 ? "" : "s"}</span>
2025-10-06 18:23:19 +00:00
</Link>
</div>
{/if}
2024-10-25 23:59:12 +00:00
</div>
2025-04-16 21:19:09 +00:00
{#if !isMobile}
2025-03-04 18:20:06 +00:00
<button
class="room__item-actions join absolute right-2 top-0.5 opacity-0 transition-all"
2025-03-04 18:20:06 +00:00
class:group-hover:opacity-100={!isMobile}>
2025-07-17 21:39:59 +00:00
{#if ENABLE_ZAPS}
2025-10-30 22:36:14 +00:00
<RoomItemZapButton {url} {event} />
2025-07-17 21:39:59 +00:00
{/if}
2025-10-30 22:36:14 +00:00
<RoomItemEmojiButton {url} {event} />
2025-03-04 18:20:06 +00:00
{#if replyTo}
<Button class="button button-xs button-neutral join-item" onclick={reply}>
2025-09-10 22:26:39 +00:00
<Icon icon={Reply} size={4} />
2025-03-04 18:20:06 +00:00
</Button>
{/if}
{#if edit}
<Button class="button button-xs button-neutral join-item" onclick={edit}>
<Icon icon={Pen} size={4} />
</Button>
{/if}
2025-10-30 22:36:14 +00:00
<RoomItemMenuButton {url} {event} />
2025-03-04 18:20:06 +00:00
</button>
{/if}
2025-03-03 21:59:38 +00:00
</TapTarget>