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-10-17 23:03:50 +00:00
|
|
|
import {onMount} from "svelte"
|
2024-08-16 17:50:38 +00:00
|
|
|
import {page} from "$app/stores"
|
2024-10-17 23:03:50 +00:00
|
|
|
import {writable} from "svelte/store"
|
2024-10-24 22:48:45 +00:00
|
|
|
import {sortBy, now, assoc, append} from "@welshman/lib"
|
2024-10-01 18:47:21 +00:00
|
|
|
import type {TrustedEvent, EventContent} from "@welshman/util"
|
|
|
|
|
import {createEvent} from "@welshman/util"
|
2024-10-16 23:16:50 +00:00
|
|
|
import {formatTimestampAsDate, subscribe, publishThunk} from "@welshman/app"
|
2024-10-16 22:57:18 +00:00
|
|
|
import type {Thunk} from "@welshman/app"
|
2024-10-16 23:16:50 +00:00
|
|
|
import {slide} 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-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-10-23 16:45:06 +00:00
|
|
|
import MenuSpace from "@app/components/MenuSpace.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 {
|
|
|
|
|
userMembership,
|
2024-10-21 17:30:12 +00:00
|
|
|
decodeRelay,
|
2024-10-08 18:39:16 +00:00
|
|
|
makeChannelId,
|
|
|
|
|
deriveChannel,
|
|
|
|
|
GENERAL,
|
|
|
|
|
tagRoom,
|
|
|
|
|
MESSAGE,
|
2024-10-24 20:05:25 +00:00
|
|
|
COMMENT,
|
2024-10-08 22:30:18 +00:00
|
|
|
getMembershipRoomsByUrl,
|
2024-10-08 18:39:16 +00:00
|
|
|
} from "@app/state"
|
2024-09-11 19:19:45 +00:00
|
|
|
import {addRoomMembership, removeRoomMembership} from "@app/commands"
|
2024-10-23 16:45:06 +00:00
|
|
|
import {pushDrawer} from "@app/modal"
|
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-10-17 16:44:06 +00:00
|
|
|
const channel = deriveChannel(makeChannelId(url, room))
|
2024-10-16 22:57:18 +00:00
|
|
|
const thunks = writable({} as Record<string, Thunk>)
|
2024-08-15 21:30:55 +00:00
|
|
|
|
2024-10-23 16:45:06 +00:00
|
|
|
const openMenu = () => pushDrawer(MenuSpace, {url})
|
|
|
|
|
|
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)})
|
2024-10-16 23:16:50 +00:00
|
|
|
const thunk = publishThunk({event, relays: [url], delay: 2000})
|
2024-10-01 18:47:21 +00:00
|
|
|
|
2024-10-16 22:57:18 +00:00
|
|
|
thunks.update(assoc(thunk.event.id, thunk))
|
2024-10-01 18:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-15 21:30:55 +00:00
|
|
|
let loading = true
|
|
|
|
|
let elements: Element[] = []
|
|
|
|
|
|
|
|
|
|
$: {
|
|
|
|
|
elements = []
|
|
|
|
|
|
|
|
|
|
let previousDate
|
|
|
|
|
let previousPubkey
|
|
|
|
|
|
2024-10-03 21:43:17 +00:00
|
|
|
for (const {event} of sortBy(m => m.event.created_at, $channel?.messages || [])) {
|
2024-10-24 20:05:25 +00:00
|
|
|
if (event.kind === COMMENT) {
|
2024-10-23 17:20:03 +00:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-16 23:16:50 +00:00
|
|
|
onMount(() => {
|
2024-10-17 23:03:50 +00:00
|
|
|
subscribe({filters: [{"#~": [room], since: now()}], relays: [url]})
|
2024-10-16 23:16:50 +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>
|
|
|
|
|
<strong slot="title">{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-10-24 16:21:34 +00:00
|
|
|
<Button on:click={openMenu} class="btn btn-neutral btn-sm md:hidden">
|
2024-10-23 16:45:06 +00:00
|
|
|
<Icon icon="menu-dots" />
|
|
|
|
|
</Button>
|
2024-08-16 22:17:30 +00:00
|
|
|
</div>
|
2024-10-09 23:11:23 +00:00
|
|
|
</PageBar>
|
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-16 22:57:18 +00:00
|
|
|
{@const event = assertEvent(value)}
|
|
|
|
|
{@const thunk = $thunks[event.id]}
|
2024-10-16 23:16:50 +00:00
|
|
|
<div in:slide>
|
2024-10-16 22:57:18 +00:00
|
|
|
<ChannelMessage {url} {room} {event} {thunk} {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>
|
2024-10-23 23:41:11 +00:00
|
|
|
<ChannelCompose {content} {onSubmit} />
|
2024-08-15 19:08:07 +00:00
|
|
|
</div>
|