flotilla/src/app/components/ChannelThread.svelte

62 lines
1.9 KiB
Svelte
Raw Normal View History

2024-10-01 18:47:21 +00:00
<script lang="ts">
2024-10-17 23:03:50 +00:00
import {writable} from "svelte/store"
2024-10-16 22:57:18 +00:00
import {assoc, sortBy, append} from "@welshman/lib"
2024-10-08 18:39:16 +00:00
import {createEvent} from "@welshman/util"
import type {EventContent, TrustedEvent} from "@welshman/util"
import {repository, publishThunk} from "@welshman/app"
2024-10-16 22:57:18 +00:00
import type {Thunk} from "@welshman/app"
2024-10-08 18:39:16 +00:00
import {deriveEvents} from "@welshman/store"
import ChannelMessage from "@app/components/ChannelMessage.svelte"
import ChannelCompose from "@app/components/ChannelCompose.svelte"
import {tagRoom, REPLY} from "@app/state"
2024-10-01 18:47:21 +00:00
export let url, room, event: TrustedEvent
2024-10-16 22:57:18 +00:00
const thunks = writable({} as Record<string, Thunk>)
2024-10-01 18:47:21 +00:00
const replies = deriveEvents(repository, {
2024-10-08 18:39:16 +00:00
filters: [{kinds: [REPLY], "#E": [event.id]}],
2024-10-01 18:47:21 +00:00
})
const onSubmit = ({content, tags}: EventContent) => {
const seenRoots = new Set<string>()
for (const [raw, ...tag] of event.tags.filter(t => t[0].match(/^K|E|A|I$/i))) {
const T = raw.toUpperCase()
const t = raw.toLowerCase()
if (seenRoots.has(T)) {
tags.push([t, ...tag])
} else {
tags.push([T, ...tag])
seenRoots.add(T)
}
}
if (seenRoots.size === 0) {
2024-10-08 18:39:16 +00:00
tags.push(["K", String(event.kind)])
tags.push(["E", event.id])
2024-10-01 18:47:21 +00:00
} else {
2024-10-08 18:39:16 +00:00
tags.push(["k", String(event.kind)])
tags.push(["e", event.id])
2024-10-01 18:47:21 +00:00
}
const reply = createEvent(REPLY, {content, tags: append(tagRoom(room, url), tags)})
2024-10-16 22:57:18 +00:00
const thunk = publishThunk({event: reply, relays: [url]})
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
}
</script>
2024-10-15 22:52:30 +00:00
<div class="col-2">
2024-10-01 18:47:21 +00:00
<div class="overflow-auto pt-3">
2024-10-16 22:57:18 +00:00
<ChannelMessage {url} {room} {event} thunk={$thunks[event.id]} showPubkey />
2024-10-01 18:47:21 +00:00
{#each sortBy(e => e.created_at, $replies) as reply (reply.id)}
2024-10-16 22:57:18 +00:00
<ChannelMessage {url} {room} event={reply} thunk={$thunks[reply.id]} showPubkey hideParent />
2024-10-01 18:47:21 +00:00
{/each}
</div>
<div class="bottom-0 left-0 right-0">
2024-10-03 21:43:17 +00:00
<ChannelCompose {onSubmit} />
2024-10-01 18:47:21 +00:00
</div>
</div>