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"
|
2024-11-15 22:03:54 +00:00
|
|
|
import {sortBy, sleep, uniqBy, now} from "@welshman/lib"
|
2024-11-19 21:24:18 +00:00
|
|
|
import {getListTags, getPubkeyTagValues, LOCAL_RELAY_URL} from "@welshman/util"
|
2024-11-14 18:53:13 +00:00
|
|
|
import type {TrustedEvent} from "@welshman/util"
|
2024-10-31 19:24:40 +00:00
|
|
|
import {feedsFromFilters, makeIntersectionFeed, makeRelayFeed} from "@welshman/feeds"
|
|
|
|
|
import {nthEq} from "@welshman/lib"
|
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"
|
2024-11-15 22:03:54 +00:00
|
|
|
import {THREAD, COMMENT, decodeRelay, getEventsForUrl} from "@app/state"
|
|
|
|
|
import {subscribePersistent} from "@app/commands"
|
2024-11-14 18:53:13 +00:00
|
|
|
import {THREAD_FILTERS, 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)
|
|
|
|
|
|
2024-11-15 22:03:54 +00:00
|
|
|
const mutedPubkeys = getPubkeyTagValues(getListTags($userMutes))
|
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,
|
|
|
|
|
feed: makeIntersectionFeed(makeRelayFeed(url), feedsFromFilters(THREAD_FILTERS)),
|
|
|
|
|
onEvent: (event: TrustedEvent) => {
|
|
|
|
|
if (
|
|
|
|
|
event.kind === THREAD &&
|
|
|
|
|
!event.tags.some(nthEq(0, "e")) &&
|
|
|
|
|
!mutedPubkeys.includes(event.pubkey)
|
|
|
|
|
) {
|
|
|
|
|
buffer.push(event)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onExhausted: () => {
|
|
|
|
|
loading = false
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
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-13 01:44:29 +00:00
|
|
|
let buffer: TrustedEvent[] = []
|
2024-11-14 18:53:13 +00:00
|
|
|
let events: TrustedEvent[] = sortBy(e => -e.created_at, getEventsForUrl(url, [{kinds: [THREAD]}]))
|
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: () => {
|
|
|
|
|
buffer = sortBy(e => -e.created_at, buffer)
|
|
|
|
|
events = uniqBy(e => e.id, [...events, ...buffer.splice(0, 5)])
|
2024-11-12 21:27:19 +00:00
|
|
|
|
2024-11-15 22:03:54 +00:00
|
|
|
if (buffer.length < 50) {
|
|
|
|
|
ctrl.load(50)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
2024-11-01 21:46:38 +00:00
|
|
|
|
2024-11-15 22:03:54 +00:00
|
|
|
const unsub = subscribePersistent({
|
2024-11-19 21:24:18 +00:00
|
|
|
relays: [url, LOCAL_RELAY_URL],
|
|
|
|
|
filters: [
|
|
|
|
|
{kinds: [THREAD], since: now()},
|
|
|
|
|
{kinds: [COMMENT], "#K": [String(THREAD)], since: now()},
|
|
|
|
|
],
|
|
|
|
|
onEvent: (event: TrustedEvent) => {
|
|
|
|
|
if (event.kind === THREAD) {
|
|
|
|
|
const index = Math.max(
|
|
|
|
|
0,
|
|
|
|
|
events.findIndex(e => e.created_at < event.created_at),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
events = [...events.slice(0, index), event, ...events.slice(index)]
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-11-15 22:03:54 +00:00
|
|
|
})
|
2024-11-14 18:53:13 +00:00
|
|
|
|
2024-11-15 22:03:54 +00:00
|
|
|
return () => {
|
|
|
|
|
unsub()
|
|
|
|
|
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">
|
2024-11-13 01:44:29 +00:00
|
|
|
{#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}
|
2024-11-14 20:55:17 +00:00
|
|
|
{#if loading || events.length === 0}
|
|
|
|
|
<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}
|
|
|
|
|
No threads found.
|
|
|
|
|
{/if}
|
|
|
|
|
</Spinner>
|
|
|
|
|
</p>
|
|
|
|
|
{/if}
|
2024-10-31 19:24:40 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|