Files
vue-chat-layer/app/components/Chat/ThreadPane.vue
Jakub Zych 269c75b5dd feat: threads, pins, reactions, attachments, link embeds and the command picker
- threads: reply counts, ChatThreadPane with replies, live replies and thread mute
- channel pins in ChatPinsPanel; a refused pin hides the action for the session
- reactions, attachments with upload progress and bearer downloads, inline edits, the delete dialog (moderators also in channels)
- link cards with the registerEmbedRenderer registry; linkify for escaped text
- slash commands: the command picker over chatbots, dispatch with sender-only answers
- slots conversation-actions (DMs only) and sidebar-footer
- chat-tokens.css and chat-base.css, EN and PL copy, README
2026-10-31 14:04:54 +01:00

143 lines
5.6 KiB
Vue

<script setup lang="ts">
// The open thread: its root message, the replies (oldest first) and a reply
// composer. Replies from others arrive live through the store; the bell
// mutes or unmutes the thread for the user.
import type { ChatMessage } from '../../lib/types'
import { shortName } from '../../lib/chatFormat'
const emit = defineEmits<{ close: [] }>()
const { t } = useI18n()
const chat = useChat()
const uid = useId()
const listEl = ref<HTMLElement | null>(null)
const input = ref<HTMLTextAreaElement | null>(null)
const deleteDialog = ref<{ open: (m: ChatMessage, from?: HTMLElement | null) => void } | null>(null)
const rootId = computed(() => chat.thread?.rootId ?? null)
const root = computed(() => {
const th = chat.thread
return th ? (chat.messages[th.conversationId]?.find((m) => m.id === th.rootId) ?? null) : null
})
const list = computed(() => (rootId.value !== null ? chat.replies[rootId.value] : undefined))
const failed = computed(() => (rootId.value !== null ? !!chat.repliesError[rootId.value] : false))
const muted = computed(() => (rootId.value !== null ? !!chat.threadMuted[rootId.value] : false))
const draft = computed(() => chat.threadDraft(rootId.value))
const text = computed({
get: () => draft.value.text,
set: (v: string) => {
if (rootId.value !== null) chat.setThreadDraftText(rootId.value, v)
},
})
const errorLine = computed(() => {
const d = draft.value
if (!d.error) return ''
return d.detail ? `${t(`chat.${d.error}`)} ${d.detail}` : t(`chat.${d.error}`)
})
const rootName = computed(() => (root.value ? shortName(chat.members.get(root.value.sender_id), root.value.sender_name, t('chat.someone')) : ''))
function scrollToBottom() {
void nextTick(() => {
const e = listEl.value
if (e) e.scrollTop = e.scrollHeight
})
}
watch(() => list.value?.length, scrollToBottom)
async function submit() {
if (rootId.value === null) return
if (await chat.sendReply(rootId.value)) scrollToBottom()
}
function onKeydown(e: KeyboardEvent) {
if (e.isComposing || e.key !== 'Enter' || e.shiftKey) return
e.preventDefault()
void submit()
}
function close() {
chat.closeThread()
emit('close')
}
onMounted(() => {
scrollToBottom()
input.value?.focus()
})
</script>
<template>
<aside v-if="rootId !== null" class="chat-thread" :aria-labelledby="`chat-thread-h-${uid}`" @keydown.esc="close">
<header class="chat-head">
<div class="chat-head-row">
<div class="chat-head-name">
<span class="chat-eyebrow">{{ t('chat.thread.title') }}</span>
<h2 :id="`chat-thread-h-${uid}`" class="chat-title">{{ rootName ? t('chat.thread.with', { name: rootName }) : t('chat.thread.title') }}</h2>
</div>
<div class="chat-head-actions">
<button
class="chat-icon-btn"
type="button"
:aria-pressed="muted ? 'true' : 'false'"
:aria-label="muted ? t('chat.thread.unmute') : t('chat.thread.mute')"
:title="muted ? t('chat.thread.unmute') : t('chat.thread.mute')"
@click="chat.setThreadMute(rootId, !muted)"
>
<ChatIcon :name="muted ? 'bell-off' : 'bell'" />
</button>
<button class="chat-icon-btn" type="button" :aria-label="t('chat.thread.close')" :title="t('chat.thread.close')" @click="close">
<ChatIcon name="close" />
</button>
</div>
</div>
</header>
<div ref="listEl" class="chat-thread-list" role="region" :aria-label="t('chat.thread.title')" tabindex="0">
<ChatMessage v-if="root" :message="root" :author="chat.members.get(root.sender_id)" :me-id="chat.userId" in-thread @delete="(m, from) => deleteDialog?.open(m, from)" />
<div class="chat-thread-divider" role="separator">
<span>{{ t('chat.thread.replies', { n: list?.length ?? root?.thread_summary?.reply_count ?? 0 }, list?.length ?? root?.thread_summary?.reply_count ?? 0) }}</span>
</div>
<p v-if="failed" class="chat-error" role="alert">
{{ t('chat.thread.failed') }}
<button class="chat-link-btn" type="button" @click="chat.loadReplies(rootId)">{{ t('chat.retry') }}</button>
</p>
<div v-else-if="!list" class="chat-skeletons" aria-busy="true" :aria-label="t('chat.loading')">
<div class="chat-skeleton"></div>
<div class="chat-skeleton"></div>
</div>
<template v-else>
<ChatMessage
v-for="m in list"
:key="m.id"
:message="m"
:author="chat.members.get(m.sender_id)"
:me-id="chat.userId"
in-thread
@delete="(msg, from) => deleteDialog?.open(msg, from)"
/>
</template>
</div>
<form class="chat-composer chat-thread-composer" @submit.prevent="submit">
<label class="chat-sr" :for="`chat-thread-input-${uid}`">{{ t('chat.thread.replyLabel') }}</label>
<p v-if="errorLine" class="chat-error" role="alert">{{ errorLine }}</p>
<div class="chat-composer-field">
<textarea
:id="`chat-thread-input-${uid}`"
ref="input"
v-model="text"
rows="2"
:placeholder="t('chat.thread.replyPlaceholder')"
:readonly="draft.sending"
@keydown="onKeydown"
></textarea>
<div class="chat-composer-tools">
<span class="chat-composer-gap"></span>
<button class="chat-btn chat-btn--primary chat-send" type="submit" :disabled="!text.trim() || draft.sending" :aria-busy="draft.sending ? 'true' : undefined">
{{ t('chat.thread.send') }}<ChatIcon name="arrow-right" />
</button>
</div>
</div>
</form>
<ChatDeleteDialog ref="deleteDialog" />
</aside>
</template>