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

127 lines
3.8 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"
import {derived} from "svelte/store"
2024-10-31 19:24:40 +00:00
import {page} from "$app/stores"
import {sortBy, min, nthEq, sleep} from "@welshman/lib"
2025-01-24 17:36:24 +00:00
import {THREAD, COMMENT, getListTags, getPubkeyTagValues} from "@welshman/util"
import {throttled} from "@welshman/store"
2025-01-02 23:08:16 +00:00
import {feedFromFilters, makeIntersectionFeed, makeRelayFeed} from "@welshman/feeds"
2024-11-07 23:10:28 +00:00
import {createFeedController, userMutes} from "@welshman/app"
2024-11-12 22:32:36 +00:00
import {createScroller, type Scroller} from "@lib/html"
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"
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"
2025-01-24 17:36:24 +00:00
import {decodeRelay, deriveEventsForUrl} from "@app/state"
2024-12-16 19:24:10 +00:00
import {setChecked} from "@app/notifications"
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)
2025-01-24 17:36:24 +00:00
const threadFilter = {kinds: [THREAD]}
const commentFilter = {kinds: [COMMENT], "#K": [String(THREAD)]}
const feed = feedFromFilters([threadFilter, commentFilter])
const threads = deriveEventsForUrl(url, [threadFilter])
const comments = deriveEventsForUrl(url, [commentFilter])
2024-11-15 22:03:54 +00:00
const mutedPubkeys = getPubkeyTagValues(getListTags($userMutes))
2024-10-31 19:24:40 +00:00
const events = throttled(
800,
derived([threads, comments], ([$threads, $comments]) => {
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, min([scores.get(id), -comment.created_at]))
}
}
return sortBy(
e => min([scores.get(e.id), -e.created_at]),
$threads.filter(e => !mutedPubkeys.includes(e.pubkey)),
)
}),
)
2024-10-31 19:24:40 +00:00
const createThread = () => pushModal(ThreadCreate, {url})
2024-11-14 18:53:13 +00:00
const ctrl = createFeedController({
useWindowing: true,
2025-01-02 23:08:16 +00:00
feed: makeIntersectionFeed(makeRelayFeed(url), feed),
2024-11-14 18:53:13 +00:00
onExhausted: () => {
loading = false
},
})
let limit = 10
2024-10-31 19:24:40 +00:00
let loading = true
2024-11-14 18:53:13 +00:00
let unmounted = false
2024-10-31 19:24:40 +00:00
let element: Element
2024-11-12 22:32:36 +00:00
let scroller: Scroller
2024-11-07 23:10:28 +00:00
2024-11-15 22:03:54 +00:00
onMount(() => {
2024-11-12 21:32:54 +00:00
// Element is frequently not defined. I don't know why
2024-11-15 22:03:54 +00:00
sleep(1000).then(() => {
if (!unmounted) {
scroller = createScroller({
element,
delay: 300,
threshold: 3000,
onScroll: () => {
limit += 10
if ($events.length - limit < 10) {
2024-11-15 22:03:54 +00:00
ctrl.load(50)
}
},
})
}
})
2024-11-01 21:46:38 +00:00
2024-11-15 22:03:54 +00:00
return () => {
unmounted = true
scroller?.stop()
setChecked($page.url.pathname)
}
2024-11-14 18:53:13 +00:00
})
2024-10-31 19:24:40 +00:00
</script>
2024-11-01 22:08:27 +00:00
<div class="relative flex h-screen flex-col" bind:this={element}>
2024-10-31 19:24:40 +00:00
<PageBar>
<div slot="icon" class="center">
<Icon icon="notes-minimalistic" />
</div>
<strong slot="title">Threads</strong>
<div slot="action" class="row-2">
<Button class="btn btn-primary btn-sm" on:click={createThread}>
<Icon icon="notes-minimalistic" />
Create a Thread
</Button>
2024-11-15 22:03:54 +00:00
<MenuSpaceButton {url} />
2024-10-31 19:24:40 +00:00
</div>
</PageBar>
2024-11-01 22:08:27 +00:00
<div class="flex flex-grow flex-col gap-2 overflow-auto p-2">
{#each $events as event (event.id)}
2024-11-14 18:53:13 +00:00
<div in:fly>
<ThreadItem {url} {event} />
</div>
2024-10-31 19:24:40 +00:00
{/each}
{#if loading || $events.length === 0}
2024-11-14 20:55:17 +00:00
<p class="flex h-10 items-center justify-center py-20" out:fly>
<Spinner {loading}>
{#if loading}
Looking for threads...
{:else if $events.length === 0}
2024-11-14 20:55:17 +00:00
No threads found.
{/if}
</Spinner>
</p>
{/if}
2024-10-31 19:24:40 +00:00
</div>
</div>