Files
Jakub Zych 764cb42894 refactor: the DM-only conversation-actions rule as a helper
dmActionsTarget (app/lib/slots.ts) answers the open DM's row or null;
ChatHeader renders the slot from it. Same behaviour, testable outside a
component.
2026-11-01 19:17:42 +01:00

114 lines
4.7 KiB
Vue

<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), the channel's pins and leave actions and, for DMs only, the
// conversation-actions slot (a call button, for example). Under it, the
// connection strip shows whenever realtime is not connected. Presence is
// not announced to screen readers.
//
// Slot conversation-actions: { conversation } (the open DM's row), rendered
// only when the open conversation is a DM.
import { badgeText } from '../../lib/chatUnread'
import { shortName } from '../../lib/chatFormat'
import { dmActionsTarget } from '../../lib/slots'
const props = withDefaults(defineProps<{ back?: boolean; pinsOpen?: boolean }>(), { back: true, pinsOpen: false })
const emit = defineEmits<{ back: []; pins: [] }>()
const { t } = useI18n()
const chat = useChat()
const channel = computed(() => chat.activeChannel)
const dm = computed(() => chat.activeDm)
/** The open DM's row, for the DM-only slot (null in a channel). */
const conversation = computed(() => dmActionsTarget(dm.value))
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">
<slot v-if="conversation" name="conversation-actions" :conversation="conversation" />
<button
v-if="channel"
class="chat-icon-btn"
type="button"
:aria-pressed="pinsOpen ? 'true' : 'false'"
:aria-label="t('chat.pins.title')"
:title="t('chat.pins.title')"
@click="emit('pins')"
>
<ChatIcon name="pin" />
</button>
<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>