- 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
79 lines
3.3 KiB
Vue
79 lines
3.3 KiB
Vue
<script setup lang="ts">
|
|
// The user's DMs, newest first, and the new-message button (the member
|
|
// picker itself lives in ChatPage and the floating panel).
|
|
import type { ChatConversationRow } from '../../lib/types'
|
|
import { shortName } from '../../lib/chatFormat'
|
|
import { badgeText } from '../../lib/chatUnread'
|
|
import { dayKey, fmtDateShort, fmtTime } from '../../lib/format'
|
|
|
|
const emit = defineEmits<{ compose: []; opened: [conversationId: number] }>()
|
|
const { t } = useI18n()
|
|
const chat = useChat()
|
|
|
|
const rows = computed(() => chat.dmRows)
|
|
const firstLoad = computed(() => !rows.value.length && (chat.bootstrapState === 'loading' || chat.bootstrapState === 'idle'))
|
|
const failed = computed(() => !rows.value.length && chat.bootstrapState === 'error')
|
|
|
|
function name(row: ChatConversationRow) {
|
|
return shortName(chat.members.get(row.other_user_id), '', t('chat.someone'))
|
|
}
|
|
|
|
function preview(row: ChatConversationRow) {
|
|
const m = row.last_message
|
|
if (!m) return ''
|
|
return m.sender_id === chat.userId ? t('chat.dm.you', { text: m.content }) : m.content
|
|
}
|
|
|
|
function when(row: ChatConversationRow) {
|
|
const at = row.last_message?.created_at
|
|
if (!at) return ''
|
|
return dayKey(at) === dayKey(new Date()) ? fmtTime(at) : fmtDateShort(at)
|
|
}
|
|
|
|
async function open(row: ChatConversationRow) {
|
|
await chat.openDm(row.id)
|
|
emit('opened', row.id)
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<section class="chat-dms" :aria-label="t('chat.dm.title')">
|
|
<div class="chat-section-head">
|
|
<h2 class="chat-section-title">{{ t('chat.dm.title') }}</h2>
|
|
<button class="chat-icon-btn" type="button" :aria-label="t('chat.dm.new')" :title="t('chat.dm.new')" @click="emit('compose')"><ChatIcon name="plus" /></button>
|
|
</div>
|
|
<div v-if="firstLoad" class="chat-skeletons" aria-busy="true" :aria-label="t('chat.loading')">
|
|
<div class="chat-skeleton"></div>
|
|
<div class="chat-skeleton"></div>
|
|
</div>
|
|
<p v-else-if="failed" class="chat-error" role="alert">
|
|
{{ t('chat.dm.failed') }}
|
|
<button class="chat-link-btn" type="button" @click="chat.bootstrap()">{{ t('chat.retry') }}</button>
|
|
</p>
|
|
<p v-else-if="!rows.length" class="chat-muted chat-dms-empty">{{ t('chat.dm.empty') }}</p>
|
|
<ul v-else class="chat-rows">
|
|
<li v-for="row in rows" :key="row.id">
|
|
<button
|
|
class="chat-row chat-dm-row"
|
|
:class="{ 'is-unread': row.unread > 0, 'is-active': chat.activeId === row.id }"
|
|
type="button"
|
|
:aria-current="chat.activeId === row.id ? 'true' : undefined"
|
|
@click="open(row)"
|
|
>
|
|
<ChatAvatar :user-id="row.other_user_id" :member="chat.members.get(row.other_user_id)" :size="24" />
|
|
<span class="chat-dm-text">
|
|
<span class="chat-row-name">{{ name(row) }}</span>
|
|
<span class="chat-dm-preview">{{ preview(row) }}</span>
|
|
</span>
|
|
<span class="chat-dm-meta">
|
|
<time v-if="row.last_message" :datetime="row.last_message.created_at">{{ when(row) }}</time>
|
|
<span v-if="badgeText(row.unread)" class="chat-badge"
|
|
><span aria-hidden="true">{{ badgeText(row.unread) }}</span><span class="chat-sr">, {{ t('chat.unread.messages', { n: row.unread }, row.unread) }}</span></span
|
|
>
|
|
</span>
|
|
</button>
|
|
</li>
|
|
</ul>
|
|
</section>
|
|
</template>
|