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

95 lines
2.5 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">
2024-08-16 18:56:35 +00:00
import {sortBy} from "@welshman/lib"
2024-08-19 16:06:39 +00:00
import type {TrustedEvent} from "@welshman/util"
2024-08-16 17:50:38 +00:00
import {page} from "$app/stores"
import {formatTimestampAsDate} from "@lib/util"
2024-08-16 22:17:30 +00:00
import Icon from "@lib/components/Icon.svelte"
2024-08-16 17:50:38 +00:00
import Spinner from "@lib/components/Spinner.svelte"
import GroupNote from "@app/components/GroupNote.svelte"
2024-08-19 21:22:16 +00:00
import GroupCompose from "@app/components/GroupCompose.svelte"
2024-08-22 19:21:54 +00:00
import {deriveGroupChat} from "@app/state"
2024-08-15 19:08:07 +00:00
2024-08-16 22:17:30 +00:00
const {nom} = $page.params
2024-08-22 19:21:54 +00:00
const chat = deriveGroupChat(nom)
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
let loading = true
let elements: Element[] = []
$: {
elements = []
let previousDate
let previousPubkey
2024-08-22 19:21:54 +00:00
for (const {event} of sortBy(m => m.event.created_at, $chat?.messages || [])) {
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
}
elements.reverse()
}
setTimeout(() => {
loading = false
}, 3000)
2024-08-14 00:58:26 +00:00
</script>
2024-08-16 22:17:30 +00:00
<div class="relative flex h-screen flex-col">
2024-08-23 00:25:53 +00:00
<div class="relative z-feature mx-2 pt-4 rounded-xl">
2024-08-16 22:17:30 +00:00
<div class="flex min-h-12 items-center gap-4 rounded-xl bg-base-100 px-4 shadow-xl">
<div class="flex items-center gap-2">
<Icon icon="hashtag" />
<strong>General</strong>
</div>
</div>
</div>
2024-08-23 00:25:53 +00:00
<div class="flex flex-grow flex-col-reverse overflow-auto -mt-2 py-2">
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-08-16 22:17:30 +00:00
<div class="flex items-center gap-2 p-2 text-xs opacity-50">
<div class="h-px flex-grow bg-base-content opacity-25" />
2024-08-15 21:30:55 +00:00
<p>{value}</p>
2024-08-16 22:17:30 +00:00
<div class="h-px flex-grow bg-base-content opacity-25" />
2024-08-15 21:30:55 +00:00
</div>
{:else}
<GroupNote event={assertEvent(value)} {showPubkey} />
{/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>
2024-08-19 21:22:16 +00:00
<GroupCompose />
2024-08-15 19:08:07 +00:00
</div>