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
This commit is contained in:
94
app/components/Chat/Header.vue
Normal file
94
app/components/Chat/Header.vue
Normal file
@@ -0,0 +1,94 @@
|
||||
<script setup lang="ts">
|
||||
// The open conversation's name, presence (channels) or online dot (DMs),
|
||||
// the back button of narrow layouts (with the unread count of everything
|
||||
// else) and the channel's leave action. Under it, the connection strip
|
||||
// shows whenever realtime is not connected. Presence is not announced to
|
||||
// screen readers.
|
||||
import { badgeText } from '../../lib/chatUnread'
|
||||
import { shortName } from '../../lib/chatFormat'
|
||||
|
||||
const props = withDefaults(defineProps<{ back?: boolean }>(), { back: true })
|
||||
const emit = defineEmits<{ back: [] }>()
|
||||
const { t } = useI18n()
|
||||
const chat = useChat()
|
||||
|
||||
const channel = computed(() => chat.activeChannel)
|
||||
const dm = computed(() => chat.activeDm)
|
||||
const dmName = computed(() => (dm.value ? shortName(chat.members.get(dm.value.other_user_id), '', t('chat.someone')) : ''))
|
||||
const title = computed(() => (channel.value ? `# ${channel.value.name}` : dmName.value))
|
||||
const eyebrow = computed(() => (channel.value ? t('chat.header.channel') : t('chat.header.dm')))
|
||||
|
||||
/** The connection strip copy; none when connected (or before the chat connects). */
|
||||
const strip = computed(() => {
|
||||
switch (chat.connection) {
|
||||
case 'connecting':
|
||||
return t('chat.connection.connecting')
|
||||
case 'reconnecting':
|
||||
return t('chat.connection.reconnecting')
|
||||
case 'unavailable':
|
||||
return t('chat.connection.unavailable')
|
||||
default:
|
||||
return ''
|
||||
}
|
||||
})
|
||||
|
||||
/** What waits elsewhere: unread DMs and mentions outside the open conversation. */
|
||||
const elsewhere = computed(() => {
|
||||
const open = chat.activeId
|
||||
let n = chat.attention
|
||||
if (open !== null) n -= chat.isChannel(open) ? chat.mentionsOf(open) : chat.unreadOf(open)
|
||||
return Math.max(0, n)
|
||||
})
|
||||
|
||||
const stack = computed(() => chat.othersOnline.slice(0, 3))
|
||||
const more = computed(() => Math.max(0, chat.othersOnline.length - 3))
|
||||
const dmOnline = computed(() => !!dm.value && chat.isOnline(dm.value.other_user_id))
|
||||
const leaving = ref(false)
|
||||
|
||||
async function leave() {
|
||||
if (!channel.value || leaving.value) return
|
||||
leaving.value = true
|
||||
try {
|
||||
if (await chat.leaveChannel(channel.value)) emit('back')
|
||||
} finally {
|
||||
leaving.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<header class="chat-head">
|
||||
<div class="chat-head-row">
|
||||
<button v-if="props.back" class="chat-icon-btn chat-back" type="button" :aria-label="t('chat.header.back')" @click="emit('back')">
|
||||
<ChatIcon name="arrow-left" />
|
||||
<span v-if="badgeText(elsewhere)" class="chat-badge"
|
||||
><span aria-hidden="true">{{ badgeText(elsewhere) }}</span><span class="chat-sr">, {{ t('chat.unread.messages', { n: elsewhere }, elsewhere) }}</span></span
|
||||
>
|
||||
</button>
|
||||
<div class="chat-head-name">
|
||||
<span class="chat-eyebrow">{{ eyebrow }}</span>
|
||||
<h2 class="chat-title">{{ title }}</h2>
|
||||
</div>
|
||||
<div class="chat-presence" aria-hidden="true">
|
||||
<template v-if="channel && chat.presenceAvailable">
|
||||
<span v-if="stack.length" class="chat-presence-stack">
|
||||
<ChatAvatar v-for="id in stack" :key="id" :user-id="id" :member="chat.members.get(id)" :size="24" />
|
||||
</span>
|
||||
<span v-if="more" class="chat-presence-text">+{{ more }}</span>
|
||||
<span class="chat-presence-dot"></span>
|
||||
<span class="chat-presence-text">{{ chat.onlineCount > 1 ? t('chat.header.online', { n: chat.onlineCount }) : t('chat.header.onlyYou') }}</span>
|
||||
</template>
|
||||
<template v-else-if="dmOnline">
|
||||
<span class="chat-presence-dot"></span>
|
||||
<span class="chat-presence-text">{{ t('chat.header.dmOnline') }}</span>
|
||||
</template>
|
||||
</div>
|
||||
<div class="chat-head-actions">
|
||||
<button v-if="channel" class="chat-icon-btn" type="button" :aria-label="t('chat.header.leave')" :title="t('chat.header.leave')" :disabled="leaving" @click="leave">
|
||||
<ChatIcon name="out" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
<p v-if="strip" class="chat-strip">{{ strip }}</p>
|
||||
</template>
|
||||
Reference in New Issue
Block a user