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 17:50:38 +00:00
|
|
|
import {page} from "$app/stores"
|
2024-10-01 18:47:21 +00:00
|
|
|
import {sortBy, append} from "@welshman/lib"
|
|
|
|
|
import type {TrustedEvent, EventContent} from "@welshman/util"
|
|
|
|
|
import {createEvent} from "@welshman/util"
|
|
|
|
|
import {formatTimestampAsDate, makeThunk, publishThunk} from "@welshman/app"
|
|
|
|
|
import {fly} from "@lib/transition"
|
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-09-20 18:00:41 +00:00
|
|
|
import Divider from "@lib/components/Divider.svelte"
|
2024-09-11 18:09:25 +00:00
|
|
|
import ChatMessage from "@app/components/ChatMessage.svelte"
|
|
|
|
|
import ChatCompose from "@app/components/ChatCompose.svelte"
|
2024-10-01 18:47:21 +00:00
|
|
|
import {userMembership, decodeNRelay, makeChatId, deriveChat, GENERAL, tagRoom, MESSAGE} from "@app/state"
|
2024-09-11 19:19:45 +00:00
|
|
|
import {addRoomMembership, removeRoomMembership} from "@app/commands"
|
2024-08-15 19:08:07 +00:00
|
|
|
|
2024-09-23 17:28:33 +00:00
|
|
|
const {nrelay, room = GENERAL} = $page.params
|
2024-09-11 18:21:16 +00:00
|
|
|
const url = decodeNRelay(nrelay)
|
2024-09-23 17:28:33 +00:00
|
|
|
const chat = deriveChat(makeChatId(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
|
|
|
|
2024-10-01 18:47:21 +00:00
|
|
|
const onSubmit = ({content, tags}: EventContent) => {
|
|
|
|
|
const event = createEvent(MESSAGE, {content, tags: append(tagRoom(room, url), tags)})
|
|
|
|
|
|
|
|
|
|
publishThunk(makeThunk({event, relays: [url]}))
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 21:30:55 +00:00
|
|
|
let loading = true
|
|
|
|
|
let elements: Element[] = []
|
|
|
|
|
|
2024-09-23 17:28:33 +00:00
|
|
|
$: membership = $userMembership?.roomsByUrl.get(url) || []
|
2024-09-11 19:19:45 +00:00
|
|
|
|
2024-08-15 21:30:55 +00:00
|
|
|
$: {
|
|
|
|
|
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-26 21:43:43 +00:00
|
|
|
<div class="relative z-feature mx-2 rounded-xl pt-4">
|
2024-09-11 22:21:25 +00:00
|
|
|
<div
|
|
|
|
|
class="flex min-h-12 items-center justify-between gap-4 rounded-xl bg-base-100 px-4 shadow-xl">
|
2024-08-16 22:17:30 +00:00
|
|
|
<div class="flex items-center gap-2">
|
|
|
|
|
<Icon icon="hashtag" />
|
2024-09-23 20:10:15 +00:00
|
|
|
<strong>{room}</strong>
|
2024-08-16 22:17:30 +00:00
|
|
|
</div>
|
2024-09-23 20:10:15 +00:00
|
|
|
{#if room !== GENERAL}
|
2024-09-23 17:28:33 +00:00
|
|
|
{#if membership.includes(room)}
|
|
|
|
|
<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-08-16 22:17:30 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-08-26 21:43:43 +00:00
|
|
|
<div class="-mt-2 flex flex-grow flex-col-reverse overflow-auto 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-09-20 18:00:41 +00:00
|
|
|
<Divider>{value}</Divider>
|
2024-08-15 21:30:55 +00:00
|
|
|
{:else}
|
2024-10-01 18:47:21 +00:00
|
|
|
<div in:fly>
|
|
|
|
|
<ChatMessage {url} {room} event={assertEvent(value)} {showPubkey} />
|
|
|
|
|
</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>
|
2024-10-01 18:47:21 +00:00
|
|
|
<div class="shadow-top-xl border-t border-solid border-base-100 bg-base-100">
|
|
|
|
|
<ChatCompose {onSubmit} />
|
|
|
|
|
</div>
|
2024-08-15 19:08:07 +00:00
|
|
|
</div>
|