2024-10-23 21:20:24 +00:00
|
|
|
<script lang="ts">
|
2024-11-15 22:03:54 +00:00
|
|
|
import {onMount} from "svelte"
|
2026-06-17 16:08:58 +00:00
|
|
|
import * as nip19 from "nostr-tools/nip19"
|
2024-10-23 21:20:24 +00:00
|
|
|
import {page} from "$app/stores"
|
2026-06-17 16:08:58 +00:00
|
|
|
import {goto} from "$app/navigation"
|
2026-07-28 16:16:26 +00:00
|
|
|
import {sleep, spec} from "@welshman/lib"
|
2025-10-13 20:35:02 +00:00
|
|
|
import type {MakeNonOptional} from "@welshman/lib"
|
2026-06-17 16:08:58 +00:00
|
|
|
import type {TrustedEvent} from "@welshman/util"
|
2026-07-28 16:16:26 +00:00
|
|
|
import {deriveEventsAsc} from "@welshman/store"
|
|
|
|
|
import {COMMENT, tagValue, tagSpec} from "@welshman/util"
|
2025-04-09 22:32:18 +00:00
|
|
|
import {request} from "@welshman/net"
|
2025-09-10 22:26:39 +00:00
|
|
|
import Reply from "@assets/icons/reply-2.svg?dataurl"
|
2024-10-23 21:20:24 +00:00
|
|
|
import Icon from "@lib/components/Icon.svelte"
|
2025-03-19 17:30:36 +00:00
|
|
|
import PageContent from "@lib/components/PageContent.svelte"
|
2024-10-24 18:52:02 +00:00
|
|
|
import Spinner from "@lib/components/Spinner.svelte"
|
2024-10-23 21:20:24 +00:00
|
|
|
import Button from "@lib/components/Button.svelte"
|
2026-06-17 16:08:58 +00:00
|
|
|
import Link from "@lib/components/Link.svelte"
|
2026-03-12 23:41:07 +00:00
|
|
|
import SpaceBar from "@app/components/SpaceBar.svelte"
|
2026-06-17 16:08:58 +00:00
|
|
|
import ThreadPost from "@app/components/ThreadPost.svelte"
|
|
|
|
|
import ThreadPagination from "@app/components/ThreadPagination.svelte"
|
2025-02-06 18:24:31 +00:00
|
|
|
import EventReply from "@app/components/EventReply.svelte"
|
2026-06-17 16:08:58 +00:00
|
|
|
import RoomName from "@app/components/RoomName.svelte"
|
2026-07-28 16:16:26 +00:00
|
|
|
import {deriveEvent, deriveEventsById} from "@app/repository"
|
2026-06-08 22:33:38 +00:00
|
|
|
import {decodeRelay} from "@app/relays"
|
2026-06-17 16:08:58 +00:00
|
|
|
import {makeSpacePath, scrollToEvent} from "@app/routes"
|
|
|
|
|
|
|
|
|
|
const POSTS_PER_PAGE = 20
|
2024-10-23 21:20:24 +00:00
|
|
|
|
2025-10-13 20:35:02 +00:00
|
|
|
const {relay, id} = $page.params as MakeNonOptional<typeof $page.params>
|
2024-10-23 21:20:24 +00:00
|
|
|
const url = decodeRelay(relay)
|
2025-11-20 23:54:06 +00:00
|
|
|
const event = deriveEvent(id, [url])
|
2024-10-24 20:05:25 +00:00
|
|
|
const filters = [{kinds: [COMMENT], "#E": [id]}]
|
2026-07-28 16:16:26 +00:00
|
|
|
const replies = deriveEventsAsc(deriveEventsById(filters))
|
2024-10-23 21:20:24 +00:00
|
|
|
|
|
|
|
|
const back = () => history.back()
|
|
|
|
|
|
2026-06-17 16:08:58 +00:00
|
|
|
const posts = $derived.by(() => {
|
|
|
|
|
if (!$event) return []
|
|
|
|
|
|
|
|
|
|
return [$event, ...$replies]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const replyCount = $derived(Math.max(0, posts.length - 1))
|
2026-07-28 16:16:26 +00:00
|
|
|
const h = $derived(tagValue(tagSpec("h"), $event?.tags || []))
|
2026-06-17 16:08:58 +00:00
|
|
|
|
|
|
|
|
const pageCount = $derived(Math.max(1, Math.ceil(posts.length / POSTS_PER_PAGE)))
|
|
|
|
|
|
|
|
|
|
const currentPage = $derived.by(() => {
|
|
|
|
|
const raw = parseInt($page.url.searchParams.get("page") || "1")
|
|
|
|
|
|
|
|
|
|
if (Number.isNaN(raw) || raw < 1) return 1
|
|
|
|
|
if (raw > pageCount) return pageCount
|
|
|
|
|
|
|
|
|
|
return raw
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const pagePosts = $derived(
|
|
|
|
|
posts.slice((currentPage - 1) * POSTS_PER_PAGE, currentPage * POSTS_PER_PAGE),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const setPage = (nextPage: number) => {
|
|
|
|
|
const params = new URLSearchParams($page.url.searchParams)
|
|
|
|
|
|
|
|
|
|
if (nextPage <= 1) {
|
|
|
|
|
params.delete("page")
|
|
|
|
|
} else {
|
|
|
|
|
params.set("page", String(nextPage))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const search = params.toString()
|
|
|
|
|
|
|
|
|
|
goto(`${$page.url.pathname}${search ? `?${search}` : ""}`, {
|
|
|
|
|
keepFocus: true,
|
|
|
|
|
noScroll: true,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const openReply = (post: TrustedEvent) => {
|
|
|
|
|
replyTo = post
|
2024-10-23 23:16:28 +00:00
|
|
|
showReply = true
|
|
|
|
|
}
|
2024-10-23 21:20:24 +00:00
|
|
|
|
2024-10-23 23:16:28 +00:00
|
|
|
const closeReply = () => {
|
|
|
|
|
showReply = false
|
2026-06-17 16:08:58 +00:00
|
|
|
replyTo = undefined
|
2024-10-23 21:20:24 +00:00
|
|
|
}
|
2024-10-23 23:16:28 +00:00
|
|
|
|
2026-06-17 16:08:58 +00:00
|
|
|
const openThreadReply = () => {
|
|
|
|
|
if ($event) {
|
|
|
|
|
openReply($event)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const clearReplyParent = () => {
|
|
|
|
|
if ($event) {
|
|
|
|
|
replyTo = $event
|
|
|
|
|
}
|
2024-11-21 19:52:29 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-04 00:37:14 +00:00
|
|
|
let showReply = $state(false)
|
2026-06-17 16:08:58 +00:00
|
|
|
let replyTo: TrustedEvent | undefined = $state()
|
|
|
|
|
let hashHandled = $state(false)
|
|
|
|
|
|
|
|
|
|
$effect(() => {
|
|
|
|
|
if (hashHandled || posts.length === 0) return
|
|
|
|
|
|
|
|
|
|
const hash = window.location.hash.replace(/^#/, "")
|
|
|
|
|
|
|
|
|
|
if (!hash.startsWith("nevent1")) return
|
|
|
|
|
|
|
|
|
|
let eventId: string
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const decoded = nip19.decode(hash)
|
|
|
|
|
|
|
|
|
|
if (decoded.type !== "nevent") return
|
|
|
|
|
|
|
|
|
|
eventId = decoded.data.id
|
|
|
|
|
} catch {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 16:16:26 +00:00
|
|
|
const index = posts.findIndex(spec({id: eventId}))
|
2026-06-17 16:08:58 +00:00
|
|
|
|
|
|
|
|
if (index < 0) return
|
|
|
|
|
|
|
|
|
|
hashHandled = true
|
|
|
|
|
|
|
|
|
|
const targetPage = Math.ceil((index + 1) / POSTS_PER_PAGE)
|
|
|
|
|
|
|
|
|
|
if (targetPage !== currentPage) {
|
|
|
|
|
setPage(targetPage)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setTimeout(() => scrollToEvent(posts[index]!.id), 100)
|
|
|
|
|
})
|
2024-10-24 18:52:02 +00:00
|
|
|
|
|
|
|
|
onMount(() => {
|
2025-04-11 16:27:19 +00:00
|
|
|
const controller = new AbortController()
|
|
|
|
|
|
|
|
|
|
request({relays: [url], filters, signal: controller.signal})
|
2024-11-14 18:53:13 +00:00
|
|
|
|
2024-11-15 22:03:54 +00:00
|
|
|
return () => {
|
2025-04-11 16:27:19 +00:00
|
|
|
controller.abort()
|
2024-11-15 22:03:54 +00:00
|
|
|
}
|
2024-11-14 18:53:13 +00:00
|
|
|
})
|
2024-10-23 21:20:24 +00:00
|
|
|
</script>
|
|
|
|
|
|
2026-06-17 16:08:58 +00:00
|
|
|
<SpaceBar {back} class="!h-auto min-h-20 py-3">
|
2025-03-19 17:30:36 +00:00
|
|
|
{#snippet title()}
|
2026-06-17 16:08:58 +00:00
|
|
|
<div class="flex min-w-0 flex-col gap-0.5">
|
2026-07-02 21:22:55 +00:00
|
|
|
<h1 class="truncate min-w-0 font-bold sm:text-xl">
|
2026-07-28 16:16:26 +00:00
|
|
|
{tagValue(tagSpec("title"), $event?.tags || []) || ""}
|
2026-06-17 16:08:58 +00:00
|
|
|
</h1>
|
|
|
|
|
<p class="text-xs opacity-75">
|
|
|
|
|
{replyCount}
|
|
|
|
|
{replyCount === 1 ? "reply" : "replies"}
|
|
|
|
|
{#if h}
|
|
|
|
|
· <Link href={makeSpacePath(url, h)} class="link">#<RoomName {url} {h} /></Link>
|
|
|
|
|
{/if}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2025-03-19 17:30:36 +00:00
|
|
|
{/snippet}
|
2026-03-12 23:41:07 +00:00
|
|
|
</SpaceBar>
|
2025-03-19 17:30:36 +00:00
|
|
|
|
2026-06-24 20:56:53 +00:00
|
|
|
<PageContent noPad class="flex flex-col">
|
2024-10-24 18:52:02 +00:00
|
|
|
{#if $event}
|
2026-06-24 20:56:53 +00:00
|
|
|
<div class="bg-surface border-y" style="border-color: var(--line)">
|
2026-06-17 16:08:58 +00:00
|
|
|
{#each pagePosts as post (post.id)}
|
|
|
|
|
<ThreadPost {url} event={post} threadPubkey={$event.pubkey} onReply={openReply} />
|
2025-03-19 17:30:36 +00:00
|
|
|
{/each}
|
|
|
|
|
</div>
|
2026-06-17 16:08:58 +00:00
|
|
|
{#if pageCount > 1}
|
|
|
|
|
<ThreadPagination page={currentPage} {pageCount} onPage={setPage} />
|
|
|
|
|
{/if}
|
|
|
|
|
{#if showReply && replyTo && $event}
|
|
|
|
|
<EventReply
|
|
|
|
|
{url}
|
|
|
|
|
event={$event}
|
|
|
|
|
parent={replyTo.id === $event.id ? undefined : replyTo}
|
|
|
|
|
onClose={closeReply}
|
|
|
|
|
onClearParent={clearReplyParent}
|
|
|
|
|
onSubmit={closeReply} />
|
2025-03-19 17:30:36 +00:00
|
|
|
{:else}
|
2026-06-17 16:08:58 +00:00
|
|
|
<div class="flex justify-end p-4">
|
2026-06-24 20:56:53 +00:00
|
|
|
<Button class="button button-primary" onclick={openThreadReply}>
|
2025-09-10 22:26:39 +00:00
|
|
|
<Icon icon={Reply} />
|
2025-03-19 17:30:36 +00:00
|
|
|
Reply to thread
|
2024-11-21 19:52:29 +00:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
2024-10-24 18:52:02 +00:00
|
|
|
{:else}
|
2026-06-04 21:31:07 +00:00
|
|
|
<div class="flex justify-center py-20">
|
|
|
|
|
{#await sleep(5000)}
|
|
|
|
|
<Spinner loading>Loading thread...</Spinner>
|
|
|
|
|
{:then}
|
|
|
|
|
<p>Failed to load thread.</p>
|
|
|
|
|
{/await}
|
|
|
|
|
</div>
|
2024-10-24 18:52:02 +00:00
|
|
|
{/if}
|
2025-03-19 17:30:36 +00:00
|
|
|
</PageContent>
|