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

116 lines
3.4 KiB
Svelte
Raw Normal View History

2024-10-31 19:24:40 +00:00
<script lang="ts">
2024-11-15 22:03:54 +00:00
import {onMount} from "svelte"
2024-10-31 19:24:40 +00:00
import {page} from "$app/stores"
import {sortBy, max, nthEq} from "@welshman/lib"
import type {TrustedEvent} from "@welshman/util"
2025-07-17 21:39:59 +00:00
import {THREAD, DELETE, COMMENT, getListTags, getPubkeyTagValues} from "@welshman/util"
import {userMutes} from "@welshman/app"
2024-11-14 18:53:13 +00:00
import {fly} from "@lib/transition"
2024-10-31 19:24:40 +00:00
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
import PageBar from "@lib/components/PageBar.svelte"
2025-03-19 17:30:36 +00:00
import PageContent from "@lib/components/PageContent.svelte"
2024-10-31 19:24:40 +00:00
import Spinner from "@lib/components/Spinner.svelte"
2024-11-15 22:03:54 +00:00
import MenuSpaceButton from "@app/components/MenuSpaceButton.svelte"
2024-10-31 19:24:40 +00:00
import ThreadItem from "@app/components/ThreadItem.svelte"
import ThreadCreate from "@app/components/ThreadCreate.svelte"
import {decodeRelay, getEventsForUrl} from "@app/state"
2024-12-16 19:24:10 +00:00
import {setChecked} from "@app/notifications"
2025-07-17 21:39:59 +00:00
import {REACTION_KINDS} from "@app/state"
import {makeFeed} from "@app/requests"
2024-11-15 22:03:54 +00:00
import {pushModal} from "@app/modal"
2024-10-31 19:24:40 +00:00
const url = decodeRelay($page.params.relay)
2024-11-15 22:03:54 +00:00
const mutedPubkeys = getPubkeyTagValues(getListTags($userMutes))
const threads: TrustedEvent[] = $state([])
const comments: TrustedEvent[] = $state([])
2024-10-31 19:24:40 +00:00
let loading = $state(true)
let element: HTMLElement | undefined = $state()
const createThread = () => pushModal(ThreadCreate, {url})
const events = $derived.by(() => {
const scores = new Map<string, number>()
for (const comment of comments) {
const id = comment.tags.find(nthEq(0, "E"))?.[1]
if (id) {
scores.set(id, max([scores.get(id), comment.created_at]))
}
}
2024-10-31 19:24:40 +00:00
return sortBy(e => -max([scores.get(e.id), e.created_at]), threads)
2024-11-14 18:53:13 +00:00
})
2024-11-15 22:03:54 +00:00
onMount(() => {
const {cleanup} = makeFeed({
2025-02-04 00:37:14 +00:00
element: element!,
relays: [url],
feedFilters: [{kinds: [THREAD, COMMENT]}],
subscriptionFilters: [
2025-07-17 21:39:59 +00:00
{kinds: [THREAD, DELETE, ...REACTION_KINDS]},
{kinds: [COMMENT], "#K": [String(THREAD)]},
],
initialEvents: getEventsForUrl(url, [{kinds: [THREAD, COMMENT], limit: 10}]),
onEvent: event => {
if (event.kind === THREAD && !mutedPubkeys.includes(event.pubkey)) {
threads.push(event)
}
if (event.kind === COMMENT) {
comments.push(event)
2025-02-04 00:37:14 +00:00
}
},
onExhausted: () => {
loading = false
},
2024-11-15 22:03:54 +00:00
})
2024-11-01 21:46:38 +00:00
2024-11-15 22:03:54 +00:00
return () => {
cleanup()
2024-11-15 22:03:54 +00:00
setChecked($page.url.pathname)
}
2024-11-14 18:53:13 +00:00
})
2024-10-31 19:24:40 +00:00
</script>
2025-03-19 17:30:36 +00:00
<PageBar>
{#snippet icon()}
<div class="center">
<Icon icon="notes-minimalistic" />
</div>
{/snippet}
{#snippet title()}
<strong>Threads</strong>
{/snippet}
{#snippet action()}
<div class="row-2">
<Button class="btn btn-primary btn-sm" onclick={createThread}>
2024-10-31 19:24:40 +00:00
<Icon icon="notes-minimalistic" />
2025-03-19 17:30:36 +00:00
Create a Thread
</Button>
<MenuSpaceButton {url} />
</div>
{/snippet}
</PageBar>
<PageContent bind:element class="flex flex-col gap-2 p-2 pt-4">
{#each events as event (event.id)}
<div in:fly>
<ThreadItem {url} event={$state.snapshot(event)} />
</div>
{/each}
<p class="flex h-10 items-center justify-center py-20">
<Spinner {loading}>
{#if loading}
Looking for threads...
{:else if events.length === 0}
No threads found.
{:else}
That's all!
{/if}
</Spinner>
</p>
</PageContent>