Files
vue-chat-layer/app/components/Chat/Panel.vue
Jakub Zych 5d581539b3 feat: port the chat store, realtime client and components
- realtime.ts: the page's one Centrifuge client, setRealtimeAuth, onUserEvent for user:{me} events the chat does not handle
- chatEvents, chatFormat, chatText, chatUnread, format, media, tabs ported; copy passed in localized
- useChat over useState: channels with join and leave, DMs, member list, unread and mention counts, read marks, typing, presence, polling fallback
- ChatPanel, ChatHeader, ChatMessage, ChatDmList, ChatMemberPicker, ChatMentionPopup, ChatAvatar, ChatIcon, ChatNotices
- EN and PL strings
2026-10-31 13:52:56 +01:00

70 lines
2.5 KiB
Vue

<script setup lang="ts">
// The open conversation: header, messages, the typing line and the
// composer. With nothing open it shows the empty state.
import { shortName } from '../../lib/chatFormat'
const props = withDefaults(defineProps<{ visible?: boolean; back?: boolean }>(), { visible: true, back: true })
const emit = defineEmits<{ back: [] }>()
const { t } = useI18n()
const chat = useChat()
const list = ref<{ scrollToBottom: (smooth?: boolean) => void } | null>(null)
const composer = ref<{ focus: () => void } | null>(null)
const dmName = computed(() => (chat.activeDm ? shortName(chat.members.get(chat.activeDm.other_user_id), '', t('chat.someone')) : ''))
const name = computed(() => (chat.activeChannel ? `# ${chat.activeChannel.name}` : dmName.value))
/** Who is typing in the open conversation; empty without realtime. */
const typingLine = computed(() => {
const id = chat.activeId
if (id === null) return ''
const names = chat.typingIn(id).map((u) => shortName(chat.members.get(u), '', t('chat.someone')))
if (!names.length) return ''
if (names.length === 1) return t('chat.typing.one', { a: names[0] })
if (names.length === 2) return t('chat.typing.two', { a: names[0], b: names[1] })
return t('chat.typing.many')
})
// The store needs to know whether the conversation is on screen.
watch(
() => props.visible,
(v) => (chat.panelVisible = v),
{ immediate: true },
)
onBeforeUnmount(() => (chat.panelVisible = false))
function focus() {
void nextTick(() => composer.value?.focus())
}
defineExpose({ focus })
</script>
<template>
<section class="chat-panel" :aria-label="name || t('chat.name')">
<template v-if="chat.activeId !== null && chat.kind">
<ChatHeader :back="back" @back="emit('back')" />
<ChatMessageList
ref="list"
:key="chat.activeId"
:conversation-id="chat.activeId"
:name="name"
:kind="chat.kind"
:other-name="dmName"
:visible="visible"
/>
<div class="chat-typing" aria-hidden="true">{{ typingLine }}</div>
<ChatComposer
ref="composer"
:key="`c-${chat.activeId}`"
:conversation-id="chat.activeId"
:label="t('chat.composer.label', { name })"
:placeholder="t('chat.composer.placeholder', { name })"
@sent="list?.scrollToBottom()"
/>
</template>
<div v-else class="chat-panel-empty">
<p class="chat-panel-empty-title">{{ t('chat.pickTitle') }}</p>
<p class="chat-muted">{{ t('chat.pickText') }}</p>
</div>
</section>
</template>