flotilla/src/app/components/RoomItem.svelte

174 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-04-10 22:27:36 +00:00
import {readable} from "svelte/store"
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-04-10 22:27:36 +00:00
import {MESSAGE, COMMENT, getTag} from "@welshman/util"
2025-10-06 18:23:19 +00:00
import {
thunks,
pubkey,
mergeThunks,
2025-10-06 18:23:19 +00:00
deriveProfileDisplay,
displayProfileByPubkey,
} from "@welshman/app"
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"
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-04-10 22:27:36 +00:00
import {colors, ENABLE_ZAPS, deriveEventsForUrl, deriveEvent} from "@app/core/state"
import {publishDelete, publishReaction, canEnforceNip70} from "@app/core/commands"
2025-10-30 22:36:14 +00:00
import {getRoomItemPath} from "@app/util/routes"
import {pushModal} from "@app/util/modal"
2024-08-15 21:30:55 +00:00
2025-02-04 00:37:14 +00:00
interface 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
2025-10-30 22:36:14 +00:00
const path = getRoomItemPath(url, event)
const shouldProtect = canEnforceNip70(url)
2025-01-24 21:35:09 +00:00
const today = formatTimestampAsDate(now())
const profileDisplay = deriveProfileDisplay(event.pubkey, [url])
const thunk = mergeThunks($thunks.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
const qTag = getTag("q", event.tags)
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
const deleteReaction = async (event: TrustedEvent) =>
publishDelete({relays: [url], event, protect: await shouldProtect})
2024-09-25 20:46:10 +00:00
const createReaction = async (template: EventContent) =>
publishReaction({...template, event, relays: [url], protect: await shouldProtect})
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(
"group relative flex w-full cursor-default flex-col px-2 py-0.5 text-left hover:bg-base-100/50",
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 border-base-content"
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>
2025-07-22 17:10:20 +00:00
<div class="row-2 ml-10 mt-1 pl-1">
2025-05-12 22:10:24 +00:00
<ReactionSummary
{url}
{event}
{deleteReaction}
{createReaction}
2026-04-10 22:27:36 +00:00
reactionClass="tooltip-right"
innerEvent={$innerEvent} />
{#if path && $innerComments.length > 0}
{@const pubkeys = $innerComments.map(e => e.pubkey)}
2025-10-06 18:23:19 +00:00
{@const isOwn = $pubkey && pubkeys.includes($pubkey)}
{@const info = displayList(pubkeys.map(pubkey => displayProfileByPubkey(pubkey)))}
{@const tooltip = `${info} commented`}
<div data-tip={tooltip} class="tooltip tooltip-right flex">
<Link
href={path}
class={cx("btn btn-xs gap-1 rounded-full", {
"btn-neutral": !isOwn,
"btn-primary": isOwn,
})}>
<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
2026-04-07 16:51:25 +00:00
class="join absolute right-2 top-0.5 border border-solid border-neutral text-xs opacity-0 transition-all pr-2"
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="btn join-item btn-xs" 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="btn join-item btn-xs" 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>