flotilla/src/routes/home/[chat]/+page.svelte

166 lines
5 KiB
Svelte
Raw Normal View History

2024-10-08 18:39:16 +00:00
<script lang="ts" context="module">
type Element = {
id: string
type: "date" | "note"
value: string | TrustedEvent
showPubkey: boolean
}
</script>
<script lang="ts">
import {onMount} from "svelte"
2024-10-08 18:39:16 +00:00
import {page} from "$app/stores"
import {derived} from "svelte/store"
2024-10-09 23:15:09 +00:00
import {sortBy, remove} from "@welshman/lib"
2024-10-08 18:39:16 +00:00
import type {TrustedEvent, EventContent} from "@welshman/util"
import {createEvent, DIRECT_MESSAGE} from "@welshman/util"
import {
pubkey,
formatTimestampAsDate,
inboxRelaySelectionsByPubkey,
loadInboxRelaySelections,
tagPubkey,
} from "@welshman/app"
2024-10-08 18:39:16 +00:00
import {fly} from "@lib/transition"
import Icon from "@lib/components/Icon.svelte"
import Link from "@lib/components/Link.svelte"
2024-10-08 18:39:16 +00:00
import Spinner from "@lib/components/Spinner.svelte"
import PageBar from "@lib/components/PageBar.svelte"
2024-10-08 18:39:16 +00:00
import Divider from "@lib/components/Divider.svelte"
import Name from "@app/components/Name.svelte"
import ProfileCircle from "@app/components/ProfileCircle.svelte"
import ProfileCircles from "@app/components/ProfileCircles.svelte"
import ChatMessage from "@app/components/ChatMessage.svelte"
import ChatCompose from "@app/components/ChannelCompose.svelte"
import {deriveChat, splitChatId} from "@app/state"
2024-10-08 20:20:55 +00:00
import {sendWrapped} from "@app/commands"
2024-10-08 18:39:16 +00:00
2024-10-09 23:15:09 +00:00
const id = $page.params.chat === "notes" ? $pubkey! : $page.params.chat
2024-10-08 18:39:16 +00:00
const chat = deriveChat(id)
const pubkeys = splitChatId(id)
const others = remove($pubkey, pubkeys)
const missingInboxes = derived(inboxRelaySelectionsByPubkey, $m =>
pubkeys.filter(pk => !$m.has(pk)),
)
2024-10-08 18:39:16 +00:00
const assertEvent = (e: any) => e as TrustedEvent
const assertNotNil = <T,>(x: T | undefined) => x!
2024-10-08 18:39:16 +00:00
const onSubmit = async ({content, ...params}: EventContent) => {
2024-10-11 18:20:13 +00:00
const tags = [...params.tags, ...remove($pubkey!, pubkeys).map(tagPubkey)]
2024-10-08 18:39:16 +00:00
const template = createEvent(DIRECT_MESSAGE, {content, tags})
2024-10-08 20:20:55 +00:00
await sendWrapped({template, pubkeys})
2024-10-08 18:39:16 +00:00
}
let loading = true
let elements: Element[] = []
$: {
elements = []
let previousDate
let previousPubkey
for (const event of sortBy(e => e.created_at, $chat?.messages || [])) {
const {id, pubkey, created_at} = event
const date = formatTimestampAsDate(created_at)
if (date !== previousDate) {
elements.push({type: "date", value: date, id: date, showPubkey: false})
}
elements.push({
id,
type: "note",
value: event,
showPubkey: date !== previousDate || previousPubkey !== pubkey,
})
previousDate = date
previousPubkey = pubkey
}
elements.reverse()
}
onMount(() => {
for (const pk of others) {
loadInboxRelaySelections(pk)
}
})
2024-10-08 18:39:16 +00:00
setTimeout(() => {
loading = false
}, 3000)
</script>
<div class="relative flex h-full flex-col">
2024-10-09 23:11:23 +00:00
{#if others.length > 0}
<PageBar>
<div slot="title" class="row-2">
{#if others.length === 1}
<ProfileCircle pubkey={others[0]} size={5} />
<Name pubkey={others[0]} />
{:else}
<ProfileCircles pubkeys={others} size={5} />
<p class="overflow-hidden text-ellipsis whitespace-nowrap">
2024-10-08 18:39:16 +00:00
<Name pubkey={others[0]} />
and {others.length - 1}
{others.length > 2 ? "others" : "other"}
</p>
{/if}
</div>
<div slot="action">
2024-10-11 22:29:54 +00:00
{#if remove($pubkey, $missingInboxes).length > 0}
{@const count = remove($pubkey, $missingInboxes).length}
{@const label = count > 0 ? 'inboxes are' : 'inbox is'}
<div
class="row-2 badge badge-error badge-lg tooltip tooltip-left cursor-pointer"
2024-10-11 22:29:54 +00:00
data-tip="{count} {label} not configured.">
<Icon icon="danger" />
2024-10-11 22:29:54 +00:00
{count}
</div>
{/if}
2024-10-08 18:39:16 +00:00
</div>
</PageBar>
2024-10-09 23:11:23 +00:00
{/if}
2024-10-08 18:39:16 +00:00
<div class="-mt-2 flex flex-grow flex-col-reverse overflow-auto py-2">
{#if $missingInboxes.includes(assertNotNil($pubkey))}
<div class="py-12">
<div class="card2 col-2 m-auto max-w-md items-center text-center">
<p class="row-2 text-lg text-error">
<Icon icon="danger" />
Your inbox is not configured.
</p>
<p>
In order to deliver messages, Flotilla needs to know where to send them. Please visit
your <Link class="link" href="/settings/relays">relay settings page</Link> to set up your
inbox.
</p>
</div>
</div>
{/if}
2024-10-08 18:39:16 +00:00
{#each elements as { type, id, value, showPubkey } (id)}
{#if type === "date"}
<Divider>{value}</Divider>
{:else}
<div in:fly>
<ChatMessage event={assertEvent(value)} {pubkeys} {showPubkey} />
</div>
{/if}
{/each}
<p class="flex h-10 items-center justify-center py-20">
<Spinner {loading}>
{#if loading}
Looking for messages...
{:else}
End of message history
{/if}
</Spinner>
</p>
</div>
2024-10-09 23:11:23 +00:00
<ChatCompose {onSubmit} />
2024-10-08 18:39:16 +00:00
</div>