refactor: extract client join and chat panels and fit narrow forms

This commit is contained in:
illusia committed 2026-10-01 18:00:31 +08:00
1 parent a2f91e606f
commit a416e36bd1
7 files changed
+337 -233

No files matched your search

@@ -0,0 +1,72 @@
<template>
<section :class="['chat-panel', { 'mobile-section-hidden': mobileHidden }]" data-ws-part="voice.chat">
<div class="chat-tabs" data-ws-part="voice.chat.tabs" role="tablist" :aria-label="t('chatTabs')">
<button type="button" data-ws-part="voice.chat.tab" :data-ws-state="chatTab === 'channel' ? 'active' : 'idle'" :class="{ active: chatTab === 'channel' }" @click="chatTab = 'channel'"><Icon name="hash" :size="15" /> {{ currentChannelName }}</button>
<button type="button" data-ws-part="voice.chat.tab" :data-ws-state="chatTab === 'server' ? 'active' : 'idle'" :class="{ active: chatTab === 'server' }" @click="chatTab = 'server'"><Icon name="server" :size="15" /> {{ t('serverChat') }}</button>
<button v-for="conversation in privateConversations" :key="conversation.key" type="button" data-ws-part="voice.chat.tab" :data-ws-state="chatTab === 'private' && privateConversationKey === conversation.key ? 'active' : 'idle'" :class="{ active: chatTab === 'private' && privateConversationKey === conversation.key }" @click="openConversation(conversation)"><Icon name="message" :size="15" /> {{ conversation.name }}</button>
<button type="button" data-ws-part="voice.chat.tab" :data-ws-state="chatTab === 'events' ? 'active' : 'idle'" :class="{ active: chatTab === 'events' }" @click="chatTab = 'events'"><Icon name="bell" :size="15" /> {{ t('eventLog') }}</button>
</div>
<div class="section-heading chat-heading" data-ws-part="voice.chat.heading"><div><span class="section-kicker">{{ chatTabLabel }}</span><h2><Icon :name="chatTab === 'server' ? 'server' : chatTab === 'events' ? 'bell' : chatTab === 'private' ? 'message' : 'hash'" :size="20" /> {{ chatTitle }}</h2></div><span class="section-counter">{{ chatTab === 'events' ? t('eventCount', { count: serverEvents.length }) : t('messageCount', { count: visibleChatMessages.length }) }}</span></div>
<div ref="chatListEl" class="message-list" data-ws-part="voice.chat.messages">
<div v-if="chatTab === 'events'">
<article v-for="event in serverEvents" :key="event.id" class="event-row" data-ws-part="voice.chat.event"><time>{{ formatTime(event.timestamp) }}</time><span>{{ event.message }}</span></article>
<div v-if="!serverEvents.length" class="chat-empty" data-ws-part="voice.chat.empty" data-ws-state="events-empty"><div class="chat-empty-icon"><Icon name="bell" :size="24" /></div><strong>{{ t('noEvents') }}</strong><span>{{ t('noEventsLead') }}</span></div>
</div>
<div v-else-if="!visibleChatMessages.length" class="chat-empty" data-ws-part="voice.chat.empty" data-ws-state="messages-empty"><strong>{{ chatTab === 'private' ? t('privateChatStart') : t('chatStart') }}</strong><span>{{ chatTab === 'private' ? t('privateChatStartLead') : t('chatStartLead') }}</span></div>
<template v-for="message in visibleChatMessages" :key="message.id">
<article v-if="chatTab !== 'events'" :class="['message-row', { mine: message.isSelf }]" data-ws-part="voice.chat.message" :data-ws-state="message.isSelf ? 'mine' : 'other'">
<div class="message-avatar" data-ws-part="voice.chat.message-avatar" :style="avatarStyle(message.invokerName, message.isSelf, messageAvatar(message))">{{ messageAvatar(message) ? '' : avatarInitial(message.invokerName) }}</div>
<div class="message-body" data-ws-part="voice.chat.message-body"><div class="message-meta"><strong>{{ message.isSelf ? t('you') : message.invokerName }}</strong><time>{{ formatTime(message.timestamp) }}</time></div><div class="message-bubble" data-ws-part="voice.chat.message-bubble">{{ message.message }}</div></div>
</article>
</template>
</div>
<form v-if="chatTab !== 'events'" class="message-composer" data-ws-part="voice.chat.composer" @submit.prevent="submitMessage">
<input v-model="messageDraft" maxlength="500" :placeholder="chatPlaceholder" :aria-label="t('send')" />
<button class="send-button" type="submit" :disabled="!canSendChat || !messageDraft.trim()" :title="t('send')"><Icon name="send" :size="18" /></button>
</form>
<p v-if="chatStatus" class="chat-status" data-ws-part="voice.chat.status" role="status">{{ chatStatus }}</p>
</section>
</template>
<script setup lang="ts">
import type { CSSProperties } from "vue";
import Icon from "../Icon.vue";
import type { useWebClientChat } from "../../composables/useWebClientChat.js";
import type { ChatMessage, ServerEvent } from "../../composables/useVoiceWebSocket.js";
type ChatPanelModel = Pick<ReturnType<typeof useWebClientChat>,
| "tab" | "privateConversationKey" | "canSend" | "status" | "messageDraft"
| "listElement" | "conversations" | "visibleMessages" | "tabLabel" | "title"
| "placeholder" | "openConversation" | "submitMessage"
>;
const props = defineProps<{
model: ChatPanelModel;
t: (key: string, variables?: Record<string, string | number>) => string;
currentChannelName: string;
mobileHidden: boolean;
serverEvents: readonly ServerEvent[];
avatarStyle: (name: string, isSelf?: boolean, avatar?: string) => CSSProperties;
avatarInitial: (name: string) => string;
messageAvatar: (message: ChatMessage) => string;
formatTime: (timestamp: number) => string;
}>();
// The page owns one stable controller across room and mobile view changes.
// Keep its list ref attached to the rendered scroller, including unmount cleanup.
const {
tab: chatTab,
privateConversationKey,
canSend: canSendChat,
status: chatStatus,
messageDraft,
listElement: chatListEl,
conversations: privateConversations,
visibleMessages: visibleChatMessages,
tabLabel: chatTabLabel,
title: chatTitle,
placeholder: chatPlaceholder,
openConversation,
submitMessage,
} = props.model;
</script>