flotilla/src/app/components/ContentQuote.svelte

117 lines
3.6 KiB
Svelte
Raw Normal View History

2024-09-24 21:12:38 +00:00
<script lang="ts">
2025-04-09 22:32:18 +00:00
import * as nip19 from "nostr-tools/nip19"
import {goto} from "$app/navigation"
2025-04-09 22:32:18 +00:00
import {nthEq} from "@welshman/lib"
import {Router} from "@welshman/router"
import {tracker, repository} from "@welshman/app"
2025-04-15 18:03:27 +00:00
import type {TrustedEvent} from "@welshman/util"
import {Address, getTagValue, DIRECT_MESSAGE, MESSAGE, THREAD, EVENT_TIME} from "@welshman/util"
2025-03-20 16:38:57 +00:00
import {scrollToEvent} from "@lib/html"
import Button from "@lib/components/Button.svelte"
2024-10-01 21:55:33 +00:00
import Spinner from "@lib/components/Spinner.svelte"
2024-09-24 21:12:38 +00:00
import NoteCard from "@app/components/NoteCard.svelte"
2025-02-11 23:37:50 +00:00
import NoteContent from "@app/components/NoteContent.svelte"
2024-12-10 21:07:17 +00:00
import {deriveEvent, entityLink, ROOM} from "@app/state"
import {makeThreadPath, makeSpacePath, makeCalendarPath, makeRoomPath} from "@app/routes"
2024-09-24 21:12:38 +00:00
2025-04-15 18:03:27 +00:00
type Props = {
value: any
hideMediaAtDepth: number
event: TrustedEvent
depth: number
url?: string
minimal?: boolean
2025-04-15 18:03:27 +00:00
}
const {value, event, depth, hideMediaAtDepth, url, minimal}: Props = $props()
2024-09-24 21:12:38 +00:00
2025-04-15 18:03:27 +00:00
const {id, identifier, kind, pubkey, relays = []} = value
const idOrAddress = id || new Address(kind, pubkey, identifier).toString()
2025-04-15 18:03:27 +00:00
const mergedRelays = Router.get().Quote(event, idOrAddress, relays).getUrls()
if (url) {
mergedRelays.push(url)
}
const quote = deriveEvent(idOrAddress, mergedRelays)
const entity = id
? nip19.neventEncode({id, relays: mergedRelays})
: new Address(kind, pubkey, identifier, mergedRelays).toNaddr()
2024-10-24 16:21:34 +00:00
const openMessage = (url: string, room: string | undefined, id: string) => {
2024-12-05 23:32:27 +00:00
const event = repository.getEvent(id)
const path = room ? makeRoomPath(url, room) : makeSpacePath(url, "chat")
2024-12-05 23:32:27 +00:00
if (event) {
goto(path)
2025-03-20 16:38:57 +00:00
scrollToEvent(id)
2024-12-05 23:32:27 +00:00
}
return Boolean(event)
}
const onclick = () => {
if ($quote) {
if ($quote.kind === DIRECT_MESSAGE) {
return scrollToEvent($quote.id)
}
2024-12-05 23:32:27 +00:00
const [url] = tracker.getRelays($quote.id)
const room = getTagValue(ROOM, $quote.tags)
if (url) {
if ($quote.kind === THREAD) {
return goto(makeThreadPath(url, $quote.id))
}
2025-02-06 19:33:01 +00:00
if ($quote.kind === EVENT_TIME) {
return goto(makeCalendarPath(url, $quote.id))
}
if ($quote.kind === MESSAGE) {
2024-12-05 23:32:27 +00:00
return scrollToEvent($quote.id) || openMessage(url, room, $quote.id)
}
const kind = $quote.tags.find(nthEq(0, "K"))?.[1]
const id = $quote.tags.find(nthEq(0, "E"))?.[1]
if (id && kind) {
if (parseInt(kind) === THREAD) {
return goto(makeThreadPath(url, id))
}
2025-02-06 19:33:01 +00:00
if (parseInt(kind) === EVENT_TIME) {
return goto(makeCalendarPath(url, id))
}
if (parseInt(kind) === MESSAGE) {
2024-12-05 23:32:27 +00:00
return scrollToEvent(id) || openMessage(url, room, id)
}
}
}
}
window.open(entityLink(entity))
}
2024-09-24 21:12:38 +00:00
</script>
<Button class="my-2 block max-w-full text-left" {onclick}>
2024-11-18 22:39:43 +00:00
{#if $quote}
{#if minimal && $quote.kind === MESSAGE}
<div
class="border-l-2 border-solid border-l-primary py-1 pl-2 opacity-90"
style="background-color: color-mix(in srgb, var(--primary) 10%, var(--base-300) 90%);">
<NoteContent {hideMediaAtDepth} {url} event={$quote} depth={depth + 1} />
</div>
{:else}
<NoteCard event={$quote} {url} class="bg-alt rounded-box p-4">
<NoteContent {hideMediaAtDepth} {url} event={$quote} depth={depth + 1} />
</NoteCard>
{/if}
2024-10-01 21:55:33 +00:00
{:else}
2024-10-08 18:39:16 +00:00
<div class="rounded-box p-4">
2024-10-01 21:55:33 +00:00
<Spinner loading>Loading event...</Spinner>
</div>
{/if}
</Button>