- ChatFloatingPanel: corner button with the DM and mention badge, the DM list and the open DM, a link to the full chat (chatPath) - starts the chat store itself and renders nothing for a non-member
88 lines
3.4 KiB
Vue
88 lines
3.4 KiB
Vue
<script setup lang="ts">
|
|
// The chat on every page: a corner button with the badge of what waits (DM
|
|
// messages plus channel mentions) that opens a compact panel with the DMs,
|
|
// the open DM (messages and composer) and a link to the full chat page.
|
|
// It starts the chat store itself and renders nothing until the bootstrap
|
|
// says the user is a member, so an anonymous visitor (no token) sees
|
|
// nothing. Render it inside <ClientOnly>, and not on the full chat page.
|
|
//
|
|
// Slot conversation-actions: { conversation }, in the header of the open DM.
|
|
import { badgeText } from '../../lib/chatUnread'
|
|
|
|
const props = withDefaults(defineProps<{ chatPath?: string }>(), { chatPath: '/chat' })
|
|
const { t, locale, localeProperties } = useI18n()
|
|
const chat = useChat()
|
|
const uid = useId()
|
|
const open = ref(false)
|
|
const toggle = ref<HTMLButtonElement | null>(null)
|
|
const picker = ref<{ open: () => void } | null>(null)
|
|
const panel = ref<{ focus: () => void } | null>(null)
|
|
|
|
const formatLocale = computed(() => (localeProperties.value as { language?: string } | undefined)?.language || locale.value)
|
|
const badge = computed(() => badgeText(chat.attention))
|
|
/** The open DM, if any; a channel opened on the chat page is not shown here. */
|
|
const dmOpen = computed(() => chat.kind === 'dm' && chat.activeId !== null)
|
|
|
|
onMounted(() => {
|
|
void chat.start(formatLocale.value)
|
|
})
|
|
|
|
function show() {
|
|
open.value = true
|
|
}
|
|
|
|
function hide() {
|
|
open.value = false
|
|
void nextTick(() => toggle.value?.focus())
|
|
}
|
|
|
|
function opened() {
|
|
panel.value?.focus()
|
|
}
|
|
|
|
function back() {
|
|
chat.closeThread()
|
|
chat.close()
|
|
}
|
|
|
|
onBeforeUnmount(() => {
|
|
if (open.value) chat.panelVisible = false
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<div v-if="chat.member === true" class="chat-floating" @keydown.esc="hide">
|
|
<section v-if="open" :id="`chat-floating-${uid}`" class="chat-floating-box" :aria-labelledby="`chat-floating-h-${uid}`">
|
|
<header class="chat-floating-head">
|
|
<h2 :id="`chat-floating-h-${uid}`" class="chat-floating-title">{{ t('chat.floating.title') }}</h2>
|
|
<NuxtLink class="chat-link-btn" :to="props.chatPath">{{ t('chat.floating.full') }}</NuxtLink>
|
|
<button class="chat-icon-btn" type="button" :aria-label="t('chat.floating.close')" @click="hide"><ChatIcon name="close" /></button>
|
|
</header>
|
|
<div class="chat-floating-body">
|
|
<ChatThreadPane v-if="chat.thread && dmOpen" :key="chat.thread.rootId" />
|
|
<ChatPanel v-else-if="dmOpen" ref="panel" :visible="open" @back="back">
|
|
<template #conversation-actions="slotProps"><slot name="conversation-actions" v-bind="slotProps" /></template>
|
|
</ChatPanel>
|
|
<ChatDmList v-else @compose="picker?.open()" @opened="opened" />
|
|
</div>
|
|
<ChatMemberPicker ref="picker" @started="opened" />
|
|
<ChatNotices />
|
|
</section>
|
|
<button
|
|
ref="toggle"
|
|
class="chat-floating-toggle"
|
|
type="button"
|
|
:aria-expanded="open ? 'true' : 'false'"
|
|
:aria-controls="open ? `chat-floating-${uid}` : undefined"
|
|
:aria-label="open ? t('chat.floating.close') : t('chat.floating.open')"
|
|
:title="open ? t('chat.floating.close') : t('chat.floating.open')"
|
|
@click="open ? hide() : show()"
|
|
>
|
|
<ChatIcon :name="open ? 'close' : 'chat'" />
|
|
<span v-if="badge" class="chat-badge"
|
|
><span aria-hidden="true">{{ badge }}</span><span class="chat-sr">, {{ t('chat.floating.waiting', { n: chat.attention }, chat.attention) }}</span></span
|
|
>
|
|
</button>
|
|
</div>
|
|
</template>
|