flotilla/src/app/components/ContentQuote.svelte

106 lines
3.1 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, tracker, repository} from "@welshman/app"
2025-04-15 18:03:27 +00:00
import type {TrustedEvent} from "@welshman/util"
2025-02-06 19:33:01 +00:00
import {Address, 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"
2025-02-06 19:33:01 +00:00
import {makeThreadPath, 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
}
const {value, event, depth, hideMediaAtDepth, url}: 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
2024-12-05 23:32:27 +00:00
const openMessage = (url: string, room: string, id: string) => {
const event = repository.getEvent(id)
if (event) {
goto(makeRoomPath(url, room))
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 = $quote.tags.find(nthEq(0, ROOM))?.[1]
if (url && room) {
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}
2025-04-15 18:03:27 +00:00
<NoteCard event={$quote} {url} class="bg-alt rounded-box p-4">
<NoteContent {hideMediaAtDepth} {url} event={$quote} depth={depth + 1} />
2024-10-01 21:55:33 +00:00
</NoteCard>
{: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>