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

100 lines
3.2 KiB
Svelte
Raw Normal View History

2024-10-31 19:24:40 +00:00
<script lang="ts">
import {onDestroy, onMount} from "svelte"
2025-10-06 18:23:19 +00:00
import {readable} from "svelte/store"
import type {Readable} from "svelte/store"
import {sortBy, partition, spec, max, pushToMapKey, groupBy} from "@welshman/lib"
2026-08-30 00:42:07 +00:00
import type {Maybe} from "@welshman/lib"
import type {TrustedEvent} from "@welshman/util"
2026-07-28 16:16:26 +00:00
import {THREAD, tagValue, tagSpec} from "@welshman/util"
2025-09-10 22:26:39 +00:00
import NotesMinimalistic from "@assets/icons/notes-minimalistic.svg?dataurl"
2024-10-31 19:24:40 +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-31 19:24:40 +00:00
import Spinner from "@lib/components/Spinner.svelte"
import SpaceBar from "@app/components/SpaceBar.svelte"
import ThreadBoard from "@app/components/ThreadBoard.svelte"
import {decodeRelay} from "@app/relays"
2026-07-28 16:16:26 +00:00
import {displayRoom} from "@app/rooms"
import {makeCommentFilter} from "@app/content"
2026-08-30 00:42:07 +00:00
import {isFeedLoading, makeFeed, makeFeedContext, makeScrollLoader} from "@app/feeds"
import type {PageProps} from "./$types"
2024-10-31 19:24:40 +00:00
const {params}: PageProps = $props()
const url = decodeRelay(params.relay)
const context = makeFeedContext({relays: [url]})
onDestroy(context.cleanup)
2024-10-31 19:24:40 +00:00
2026-08-30 00:42:07 +00:00
let older: Maybe<ReturnType<typeof makeScrollLoader>> = $state()
const loading = $derived(isFeedLoading($older))
const exhausted = $derived($older?.status === "exhausted")
let element: HTMLElement | undefined = $state()
2025-10-06 18:23:19 +00:00
let events: Readable<TrustedEvent[]> = $state(readable([]))
const threadFeed = $derived.by(() => {
2025-10-06 18:23:19 +00:00
const scores = new Map<string, number[]>()
const [threads, comments] = partition(spec({kind: THREAD}), $events)
for (const comment of comments) {
2026-07-28 16:16:26 +00:00
const id = tagValue(tagSpec("E"), comment.tags)
if (id) {
2025-10-06 18:23:19 +00:00
pushToMapKey(scores, id, comment.created_at)
}
}
2024-10-31 19:24:40 +00:00
const items = sortBy(e => -max([...(scores.get(e.id) || []), e.created_at]), threads)
const grouped = groupBy(e => tagValue(tagSpec("h"), e.tags) || "", items)
const byRoom = new Map<string, TrustedEvent[]>([["", []], ...grouped])
const roomName = (h: string) => (h ? displayRoom(url, h) : "general").toLowerCase()
const boards = sortBy(([h]) => roomName(h), Array.from(byRoom.entries()))
return {items, boards}
2024-11-14 18:53:13 +00:00
})
2024-11-15 22:03:54 +00:00
onMount(() => {
2025-10-06 18:23:19 +00:00
const feed = makeFeed({
2026-06-29 23:28:11 +00:00
relays: [url],
onEvent: context.add,
2025-10-06 18:23:19 +00:00
filters: [{kinds: [THREAD]}, makeCommentFilter([THREAD])],
2024-11-15 22:03:54 +00:00
})
2024-11-01 21:46:38 +00:00
2025-10-06 18:23:19 +00:00
events = feed.events
2026-08-30 00:42:07 +00:00
// These lists are sorted newest first, so reaching the bottom is reaching the oldest thing
// loaded.
older = makeScrollLoader(element!, feed.loadOlder)
return () => {
older?.stop()
feed.cleanup()
}
2024-11-14 18:53:13 +00:00
})
2024-10-31 19:24:40 +00:00
</script>
<SpaceBar>
{#snippet leading()}
<Icon icon={NotesMinimalistic} />
{/snippet}
{#snippet title()}
2025-03-19 17:30:36 +00:00
<strong>Threads</strong>
{/snippet}
</SpaceBar>
2025-03-19 17:30:36 +00:00
2026-06-22 20:36:57 +00:00
<PageContent bind:element class="flex flex-col gap-2 p-2 sm:gap-4 sm:p-4">
{#each threadFeed.boards as [h, threads] (h)}
<ThreadBoard {url} {h} {threads} {context} />
2025-03-19 17:30:36 +00:00
{/each}
<p class="flex h-10 items-center justify-center py-20">
<Spinner {loading}>
{#if loading}
Looking for threads...
2026-08-30 00:42:07 +00:00
{:else if exhausted && threadFeed.items.length === 0}
2025-03-19 17:30:36 +00:00
No threads found.
{/if}
</Spinner>
</p>
</PageContent>