flotilla/src/routes/spaces/[relay]/threads/[id]/+page.svelte

115 lines
3.4 KiB
Svelte
Raw Normal View History

2024-10-23 21:20:24 +00:00
<script lang="ts">
2024-11-15 22:03:54 +00:00
import {onMount} from "svelte"
2024-10-23 21:20:24 +00:00
import {page} from "$app/stores"
import {sortBy, sleep} from "@welshman/lib"
import {COMMENT, getTagValue} from "@welshman/util"
2024-12-17 00:14:51 +00:00
import {repository, subscribe} from "@welshman/app"
2024-10-23 21:20:24 +00:00
import {deriveEvents} from "@welshman/store"
import Icon from "@lib/components/Icon.svelte"
2024-10-24 21:02:43 +00:00
import PageBar from "@lib/components/PageBar.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"
import Content from "@app/components/Content.svelte"
import NoteCard from "@app/components/NoteCard.svelte"
2024-11-15 22:03:54 +00:00
import MenuSpaceButton from "@app/components/MenuSpaceButton.svelte"
2024-10-23 23:16:28 +00:00
import ThreadActions from "@app/components/ThreadActions.svelte"
2025-02-06 18:24:31 +00:00
import EventReply from "@app/components/EventReply.svelte"
2024-12-10 21:07:17 +00:00
import {deriveEvent, decodeRelay} from "@app/state"
2024-11-14 18:53:13 +00:00
import {setChecked} from "@app/notifications"
2024-10-23 21:20:24 +00:00
const {relay, id} = $page.params
const url = decodeRelay(relay)
const event = deriveEvent(id)
2024-10-24 20:05:25 +00:00
const filters = [{kinds: [COMMENT], "#E": [id]}]
2024-10-24 18:52:02 +00:00
const replies = deriveEvents(repository, {filters})
2024-10-23 21:20:24 +00:00
const back = () => history.back()
2024-10-23 23:16:28 +00:00
const openReply = () => {
showReply = true
}
2024-10-23 21:20:24 +00:00
2024-10-23 23:16:28 +00:00
const closeReply = () => {
showReply = false
2024-10-23 21:20:24 +00:00
}
2024-10-23 23:16:28 +00:00
2024-11-21 19:52:29 +00:00
const expand = () => {
showAll = true
}
2025-02-04 00:37:14 +00:00
let showAll = $state(false)
let showReply = $state(false)
2024-10-24 18:52:02 +00:00
onMount(() => {
2024-12-17 00:14:51 +00:00
const sub = subscribe({relays: [url], filters})
2024-11-14 18:53:13 +00:00
2024-11-15 22:03:54 +00:00
return () => {
2024-12-17 00:14:51 +00:00
sub.close()
2024-11-15 22:03:54 +00:00
setChecked($page.url.pathname)
}
2024-11-14 18:53:13 +00:00
})
2024-10-23 21:20:24 +00:00
</script>
2024-10-24 21:02:43 +00:00
<div class="relative flex flex-col-reverse gap-3 px-2">
2025-02-03 23:01:42 +00:00
<div class="absolute left-[51px] top-32 h-[calc(100%-248px)] w-[2px] bg-neutral"></div>
2024-10-24 18:52:02 +00:00
{#if $event}
{#if !showReply}
2024-11-21 19:52:29 +00:00
<div class="flex justify-end px-2 pb-2">
<Button class="btn btn-primary" onclick={openReply}>
2024-10-24 18:52:02 +00:00
<Icon icon="reply" />
Reply to thread
</Button>
</div>
{/if}
2024-11-21 19:52:29 +00:00
{#each sortBy(e => -e.created_at, $replies).slice(0, showAll ? undefined : 4) as reply (reply.id)}
2024-10-24 18:52:02 +00:00
<NoteCard event={reply} class="card2 bg-alt z-feature w-full">
2024-10-25 00:25:52 +00:00
<div class="col-3 ml-12">
2024-10-29 21:02:51 +00:00
<Content showEntire event={reply} />
2024-10-25 00:25:28 +00:00
<ThreadActions event={reply} {url} />
2024-10-24 18:52:02 +00:00
</div>
</NoteCard>
{/each}
2024-11-22 22:46:29 +00:00
{#if !showAll && $replies.length > 4}
2024-11-21 19:52:29 +00:00
<div class="flex justify-center">
<Button class="btn btn-link" onclick={expand}>
2024-11-21 19:52:29 +00:00
<Icon icon="sort-vertical" />
Show all {$replies.length} replies
</Button>
</div>
{/if}
2024-10-24 18:52:02 +00:00
<NoteCard event={$event} class="card2 bg-alt z-feature w-full">
2024-10-25 00:25:52 +00:00
<div class="col-3 ml-12">
<Content showEntire event={$event} quoteProps={{relays: [url]}} />
2024-10-25 00:25:28 +00:00
<ThreadActions event={$event} {url} />
2024-10-23 21:20:24 +00:00
</div>
</NoteCard>
2024-10-24 18:52:02 +00:00
{:else}
{#await sleep(5000)}
<Spinner loading>Loading thread...</Spinner>
{:then}
<p>Failed to load thread.</p>
{/await}
{/if}
2024-10-29 21:02:51 +00:00
<PageBar class="mx-0">
2025-02-04 00:37:14 +00:00
{#snippet icon()}
<div>
<Button class="btn btn-neutral btn-sm" onclick={back}>
2025-02-04 00:37:14 +00:00
<Icon icon="alt-arrow-left" />
Go back
</Button>
</div>
{/snippet}
{#snippet title()}
<h1 class="text-xl">{getTagValue("title", $event?.tags || []) || ""}</h1>
2025-02-04 00:37:14 +00:00
{/snippet}
{#snippet action()}
<div>
<MenuSpaceButton {url} />
</div>
{/snippet}
2024-10-24 21:02:43 +00:00
</PageBar>
2024-10-23 21:20:24 +00:00
</div>
2024-10-23 23:16:28 +00:00
{#if showReply}
2025-02-06 18:24:31 +00:00
<EventReply {url} event={$event} onClose={closeReply} onSubmit={closeReply} />
2024-10-23 23:16:28 +00:00
{/if}