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

187 lines
5.2 KiB
Svelte
Raw Normal View History

2024-08-16 22:17:30 +00:00
<script lang="ts" context="module">
type Element = {
id: string
type: "date" | "note"
2024-08-19 16:06:39 +00:00
value: string | TrustedEvent
2024-08-16 22:17:30 +00:00
showPubkey: boolean
}
</script>
2024-08-14 00:58:26 +00:00
<script lang="ts">
import {nip19} from "nostr-tools"
2024-11-15 22:03:54 +00:00
import {onMount, onDestroy} from "svelte"
import type {Readable} from "svelte/store"
import type {Editor} from "svelte-tiptap"
2024-08-16 17:50:38 +00:00
import {page} from "$app/stores"
2024-12-05 21:37:15 +00:00
import {sleep, append, now, ctx} from "@welshman/lib"
2024-10-01 18:47:21 +00:00
import type {TrustedEvent, EventContent} from "@welshman/util"
2024-11-15 22:03:54 +00:00
import {createEvent, DELETE} from "@welshman/util"
2024-10-30 20:52:35 +00:00
import {formatTimestampAsDate, publishThunk} from "@welshman/app"
import {slide} from "@lib/transition"
2024-11-20 00:15:23 +00:00
import {createScroller, type Scroller} from "@lib/html"
2024-08-16 22:17:30 +00:00
import Icon from "@lib/components/Icon.svelte"
2024-09-11 19:19:45 +00:00
import Button from "@lib/components/Button.svelte"
2024-08-16 17:50:38 +00:00
import Spinner from "@lib/components/Spinner.svelte"
2024-10-09 23:11:23 +00:00
import PageBar from "@lib/components/PageBar.svelte"
2024-09-20 18:00:41 +00:00
import Divider from "@lib/components/Divider.svelte"
2024-11-15 22:03:54 +00:00
import MenuSpaceButton from "@app/components/MenuSpaceButton.svelte"
2024-10-03 21:43:17 +00:00
import ChannelMessage from "@app/components/ChannelMessage.svelte"
import ChannelCompose from "@app/components/ChannelCompose.svelte"
2024-10-08 18:39:16 +00:00
import {
2024-11-15 22:03:54 +00:00
pullConservatively,
2024-10-31 17:37:20 +00:00
userSettingValues,
2024-10-08 18:39:16 +00:00
userMembership,
2024-10-21 17:30:12 +00:00
decodeRelay,
2024-12-05 21:37:15 +00:00
deriveChannelMessages,
2024-10-08 18:39:16 +00:00
GENERAL,
tagRoom,
MESSAGE,
2024-10-08 22:30:18 +00:00
getMembershipRoomsByUrl,
2024-12-05 21:37:15 +00:00
displayRoom,
2024-10-08 18:39:16 +00:00
} from "@app/state"
2024-11-14 18:53:13 +00:00
import {setChecked} from "@app/notifications"
2024-11-15 22:03:54 +00:00
import {addRoomMembership, removeRoomMembership, subscribePersistent} from "@app/commands"
2024-10-24 21:02:43 +00:00
import {popKey} from "@app/implicit"
2024-08-15 19:08:07 +00:00
2024-10-21 17:30:12 +00:00
const {room = GENERAL} = $page.params
2024-10-24 22:48:45 +00:00
const content = popKey<string>("content") || ""
2024-10-21 17:30:12 +00:00
const url = decodeRelay($page.params.relay)
2024-12-05 21:37:15 +00:00
const events = deriveChannelMessages(url, room)
2024-08-15 21:30:55 +00:00
2024-08-19 16:06:39 +00:00
const assertEvent = (e: any) => e as TrustedEvent
2024-08-15 21:30:55 +00:00
const replyTo = (event: TrustedEvent) => {
const relays = ctx.app.router.Event(event).getUrls()
const nevent = nip19.neventEncode({...event, relays})
$editor.commands.insertNEvent({nevent})
$editor.commands.insertContent("\n")
$editor.commands.focus()
}
2024-11-12 22:32:36 +00:00
const onSubmit = ({content, tags}: EventContent) =>
publishThunk({
relays: [url],
event: createEvent(MESSAGE, {content, tags: append(tagRoom(room, url), tags)}),
delay: $userSettingValues.send_delay,
})
2024-10-01 18:47:21 +00:00
2024-11-20 00:15:23 +00:00
let limit = 15
2024-08-15 21:30:55 +00:00
let loading = true
2024-11-20 00:15:23 +00:00
let unsub: () => void
let element: HTMLElement
let scroller: Scroller
let editor: Readable<Editor>
2024-08-15 21:30:55 +00:00
let elements: Element[] = []
$: {
elements = []
let previousDate
let previousPubkey
2024-12-05 21:37:15 +00:00
for (const event of $events.toReversed()) {
2024-08-16 18:56:35 +00:00
const {id, pubkey, created_at} = event
2024-08-15 21:30:55 +00:00
const date = formatTimestampAsDate(created_at)
if (date !== previousDate) {
2024-08-16 17:50:38 +00:00
elements.push({type: "date", value: date, id: date, showPubkey: false})
2024-08-15 21:30:55 +00:00
}
elements.push({
id,
2024-08-16 17:50:38 +00:00
type: "note",
2024-08-15 21:30:55 +00:00
value: event,
showPubkey: date !== previousDate || previousPubkey !== pubkey,
})
previousDate = date
previousPubkey = pubkey
}
2024-11-20 00:15:23 +00:00
elements = elements.reverse().slice(0, limit)
2024-08-15 21:30:55 +00:00
}
2024-11-20 00:15:23 +00:00
onMount(async () => {
// Sveltekiiit
await sleep(100)
2024-11-15 22:03:54 +00:00
pullConservatively({
relays: [url],
2024-12-05 21:37:15 +00:00
filters: [{kinds: [MESSAGE, DELETE], "#h": [room]}],
2024-11-15 22:03:54 +00:00
})
2024-11-20 00:15:23 +00:00
scroller = createScroller({
element,
delay: 300,
threshold: 3000,
onScroll: () => {
limit += 15
},
})
unsub = subscribePersistent({
2024-11-15 22:03:54 +00:00
relays: [url],
2024-12-05 21:37:15 +00:00
filters: [{kinds: [MESSAGE], "#h": [room], since: now()}],
2024-11-15 22:03:54 +00:00
})
})
2024-11-14 18:53:13 +00:00
onDestroy(() => {
2024-11-14 16:33:11 +00:00
setChecked($page.url.pathname)
2024-11-20 00:15:23 +00:00
scroller?.stop()
unsub?.()
2024-11-14 16:33:11 +00:00
})
2024-08-15 21:30:55 +00:00
setTimeout(() => {
loading = false
2024-10-24 18:52:02 +00:00
}, 5000)
2024-08-14 00:58:26 +00:00
</script>
2024-10-09 21:19:27 +00:00
<div class="relative flex h-full flex-col">
2024-10-09 23:11:23 +00:00
<PageBar>
<div slot="icon" class="center">
<Icon icon="hashtag" />
</div>
2024-12-05 21:37:15 +00:00
<strong slot="title">{displayRoom(room)}</strong>
2024-10-23 16:45:06 +00:00
<div slot="action" class="row-2">
2024-09-23 20:10:15 +00:00
{#if room !== GENERAL}
2024-10-08 22:30:18 +00:00
{#if getMembershipRoomsByUrl(url, $userMembership).includes(room)}
2024-09-23 17:28:33 +00:00
<Button class="btn btn-neutral btn-sm" on:click={() => removeRoomMembership(url, room)}>
2024-09-11 19:19:45 +00:00
<Icon icon="arrows-a-logout-2" />
Leave Room
</Button>
{:else}
2024-09-23 17:28:33 +00:00
<Button class="btn btn-neutral btn-sm" on:click={() => addRoomMembership(url, room)}>
2024-09-11 19:19:45 +00:00
<Icon icon="login-2" />
Join Room
</Button>
{/if}
{/if}
2024-11-15 22:03:54 +00:00
<MenuSpaceButton {url} />
2024-08-16 22:17:30 +00:00
</div>
2024-10-09 23:11:23 +00:00
</PageBar>
2024-11-20 00:15:23 +00:00
<div
class="scroll-container -mt-2 flex flex-grow flex-col-reverse overflow-auto py-2"
bind:this={element}>
2024-08-16 17:50:38 +00:00
{#each elements as { type, id, value, showPubkey } (id)}
2024-08-15 21:30:55 +00:00
{#if type === "date"}
2024-09-20 18:00:41 +00:00
<Divider>{value}</Divider>
2024-08-15 21:30:55 +00:00
{:else}
<div in:slide class:-mt-4={!showPubkey}>
<ChannelMessage {url} {room} {replyTo} event={assertEvent(value)} {showPubkey} />
2024-10-01 18:47:21 +00:00
</div>
2024-08-15 21:30:55 +00:00
{/if}
{/each}
2024-08-16 17:50:38 +00:00
<p class="flex h-10 items-center justify-center py-20">
2024-08-15 21:30:55 +00:00
<Spinner {loading}>
{#if loading}
Looking for messages...
{:else}
End of message history
{/if}
</Spinner>
</p>
2024-08-15 19:08:07 +00:00
</div>
<ChannelCompose bind:editor {content} {onSubmit} />
2024-08-15 19:08:07 +00:00
</div>