Files
syops/frontend/src/features/chat/components/ChatSidebar.vue
T

349 lines
15 KiB
Vue

/**
* features/chat/components/ChatSidebar.vue
* 用途:桌面端聊天侧栏,承载 Agent 信息、会话历史、Agent 切换面板与底部用户菜单。
* 依赖:lucide-vue-next、LocaleToggleButton、ExternalSystemLinks、AgentCategoryList、chat.types。
* 逻辑:由聊天页面传入当前 Agent、历史记录与用户可用 Agent;通过事件把新会话、切换 Agent、反馈、进入管理控制台等操作回传给页面。
* 管理员通过底部右侧工具图标进入管理控制台;用户下拉菜单仅保留退出登录。
* 关联服务入口与主题/语言等底栏图标统一为同尺寸 inline icon 按钮,避免错位。
*/
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
import {
BookOpen,
Briefcase,
ChevronDown,
ChevronUp,
LayoutGrid,
LogOut,
MessageSquareText,
Moon,
PanelLeftClose,
PanelLeftOpen,
Plus,
Settings,
Sparkles,
Sun,
X,
} from 'lucide-vue-next';
import LocaleToggleButton from '@/shared/components/LocaleToggleButton.vue';
import { ExternalSystemLinks } from '@/features/sso';
import AgentCategoryList from '@/shared/components/AgentCategoryList.vue';
import { isHrFocusedAgent, type AgentRegistryFields } from '@/shared/utils/agent-registry.utils';
import type { AgentWithCategory } from '@/shared/utils/agent-category.utils';
import type { DisplayHistoryItem } from '../types/chat.types';
export interface ChatSidebarAgent extends AgentWithCategory {}
const collapsed = defineModel<boolean>('collapsed', { required: true });
const agentPanelOpen = defineModel<boolean>('agentPanelOpen', { required: true });
const props = defineProps<{
isOpen: boolean;
currentAgent: AgentRegistryFields | null;
currentAgentId: number;
agentDescription: string;
hasAgentGuides: boolean;
displayHistory: DisplayHistoryItem[];
activeHistoryKey: string;
userAgents: ChatSidebarAgent[];
currentUser: Record<string, unknown> | null;
appliedTheme: string;
}>();
const emit = defineEmits<{
'new-chat': [];
'open-guide': [];
'history-select': [item: DisplayHistoryItem];
'clear-all-history': [];
'delete-history-item': [id: string, event: Event];
'switch-agent': [agent: ChatSidebarAgent];
'toggle-theme': [];
'open-feedback': [];
'go-workspace': [];
'go-admin': [];
logout: [];
}>();
const showUserDropdown = ref(false);
const userMenuRef = ref<HTMLElement | null>(null);
const handleDocumentPointerDown = (event: MouseEvent | TouchEvent) => {
const target = event.target as Node | null;
if (!target) return;
if (showUserDropdown.value && userMenuRef.value && !userMenuRef.value.contains(target)) {
showUserDropdown.value = false;
}
};
onMounted(() => {
document.addEventListener('mousedown', handleDocumentPointerDown);
document.addEventListener('touchstart', handleDocumentPointerDown, { passive: true });
});
onBeforeUnmount(() => {
document.removeEventListener('mousedown', handleDocumentPointerDown);
document.removeEventListener('touchstart', handleDocumentPointerDown);
});
const onNewChat = () => {
emit('new-chat');
};
const onLogout = () => {
showUserDropdown.value = false;
emit('logout');
};
const onGoAdmin = () => {
showUserDropdown.value = false;
emit('go-admin');
};
const agentAvatarLabel = computed(() => (isHrFocusedAgent(props.currentAgent) ? 'HR' : 'AI'));
const userInitial = (fallback: string) => {
const name = String(props.currentUser?.username ?? '').trim();
return (name || fallback).charAt(0).toUpperCase();
};
</script>
<template>
<aside
class="glass-panel fixed inset-y-0 left-0 z-50 flex h-full flex-col border-r border-default bg-surface transition-all duration-300 ease-in-out md:relative md:translate-x-0"
:class="[
isOpen ? 'translate-x-0 w-[86vw] max-w-[300px]' : '-translate-x-full w-[86vw] max-w-[300px]',
collapsed ? 'md:w-[72px]' : 'md:w-80',
]"
>
<template v-if="collapsed">
<div class="flex h-full w-full flex-col items-center py-4">
<button type="button" class="mb-6 text-muted transition-colors hover:text-secondary" @click="collapsed = false">
<PanelLeftOpen class="h-5 w-5" />
</button>
<div class="mb-4 flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-gradient-to-br from-brand-500 via-brand-600 to-sky-600 text-white shadow-sm shadow-brand-500/25">
<Sparkles class="h-5 w-5" />
</div>
<button type="button" class="chat-sidebar-btn-icon" :title="$t('chat.createNewChat')" @click="onNewChat">
<Plus class="h-5 w-5" />
</button>
<div class="mt-auto flex flex-col items-center gap-6 pb-2 text-muted">
<ExternalSystemLinks
compact
:show-label="false"
button-class="inline-flex items-center justify-center transition-colors hover:text-secondary"
icon-class="h-5 w-5 shrink-0"
/>
<button type="button" class="transition-colors hover:text-secondary"><Sparkles class="h-5 w-5" /></button>
<div class="flex h-8 w-8 items-center justify-center rounded-full bg-brand-600 text-sm font-bold text-white shadow-sm shadow-brand-500/20">
{{ userInitial('I') }}
</div>
<LocaleToggleButton variant="sidebar" icon-size="md" />
<button type="button" class="transition-colors hover:text-secondary" :title="$t('chat.generalFeedback')" @click="emit('open-feedback')">
<MessageSquareText class="h-5 w-5" />
</button>
<button type="button" class="transition-colors hover:text-secondary" :title="$t('workspace.title')" @click="emit('go-workspace')">
<Briefcase class="h-5 w-5" />
</button>
</div>
</div>
</template>
<template v-else>
<div class="flex items-center justify-between p-4">
<div class="flex w-full min-w-0 items-center gap-3">
<div class="flex h-8 w-8 shrink-0 items-center justify-center rounded-xl bg-gradient-to-br from-brand-500 via-brand-600 to-sky-600 text-sm font-bold text-white shadow-sm shadow-brand-500/25">
{{ agentAvatarLabel }}
</div>
<div class="group relative flex min-w-0 flex-1 flex-col justify-center">
<div class="flex items-center gap-2">
<span class="truncate font-bold text-primary">{{ currentAgent?.name || 'AI' }}</span>
<button
v-if="hasAgentGuides"
type="button"
class="flex items-center gap-1.5 rounded-lg border border-brand-300 bg-brand-100 px-3 py-1.5 text-xs font-bold text-brand-700 shadow-sm transition-all hover:border-brand-400 hover:bg-brand-200"
@click="emit('open-guide')"
>
<BookOpen class="h-4 w-4" />
学习之窗
</button>
</div>
<span v-if="agentDescription" class="mt-1 block cursor-help truncate text-[11px] text-muted">
{{ agentDescription }}
</span>
<div
v-if="agentDescription"
class="pointer-events-none invisible absolute left-0 top-[calc(100%+0.25rem)] z-[100] w-[20rem] rounded-xl border border-default bg-surface p-3 opacity-0 shadow-xl transition-all duration-200 group-hover:visible group-hover:opacity-100"
>
<div class="text-[11px] leading-relaxed text-secondary">
{{ agentDescription }}
</div>
</div>
</div>
</div>
<button type="button" class="ml-2 shrink-0 text-muted transition-colors hover:text-secondary" @click="collapsed = true">
<PanelLeftClose class="h-5 w-5" />
</button>
</div>
<div class="px-4 pb-4">
<button type="button" class="chat-sidebar-btn" @click="onNewChat">
<Plus class="h-4 w-4" /> {{ $t('chat.createNewChat') }}
</button>
</div>
<div class="flex items-center justify-between px-4 py-2">
<span class="text-xs font-semibold text-slate-500 dark:text-slate-400">{{ $t('chat.history') }}</span>
<button
v-if="displayHistory.length > 0"
type="button"
class="text-xs text-slate-400 transition-colors hover:text-slate-600 dark:hover:text-slate-200"
@click="emit('clear-all-history')"
>
{{ $t('chat.clearAll') }}
</button>
</div>
<div class="scrollbar-thin flex-1 overflow-y-auto px-2">
<div v-if="displayHistory.length === 0" class="py-8 text-center text-sm italic text-slate-400">
{{ $t('chat.noHistory') }}
</div>
<div v-else class="space-y-0.5">
<div
v-for="item in displayHistory"
:key="item.conversation_id"
class="group relative flex w-full items-center justify-between rounded-lg transition-colors"
:class="activeHistoryKey === item.conversation_id ? 'bg-brand-50 dark:bg-brand-900/20' : 'hover:bg-slate-50 dark:hover:bg-slate-800/50'"
>
<button
type="button"
class="flex-1 truncate px-3 py-2.5 pr-8 text-left text-sm"
:class="activeHistoryKey === item.conversation_id
? 'font-medium text-brand-700 dark:text-brand-300'
: 'text-slate-600 dark:text-slate-300'"
@click="emit('history-select', item)"
>
{{ item.title || $t('chat.newChat') }}
</button>
<button
v-if="!item.__runId"
type="button"
class="absolute right-2 p-1.5 text-muted opacity-0 transition-opacity hover:text-red-500 group-hover:opacity-100"
:title="$t('chat.clearOneRecord')"
@click="emit('delete-history-item', item.conversation_id, $event)"
>
<X class="h-4 w-4" />
</button>
</div>
</div>
</div>
<div class="mt-auto flex flex-col p-4">
<div
v-if="userAgents.length > 0"
class="chat-agent-panel"
:class="{ 'chat-agent-panel--collapsed': !agentPanelOpen }"
>
<div class="flex items-center justify-between gap-2" :class="agentPanelOpen ? 'mb-2' : 'mb-0'">
<div class="flex items-center gap-1.5 text-xs font-semibold text-muted">
<LayoutGrid class="h-3.5 w-3.5" /> {{ $t('chat.agents') }}
<span class="font-normal opacity-70">({{ userAgents.length }})</span>
</div>
<button
type="button"
class="inline-flex h-5 w-5 shrink-0 items-center justify-center rounded-md text-brand-600 transition-colors hover:bg-brand-50 hover:text-brand-700 dark:hover:bg-brand-900/30"
:title="agentPanelOpen ? $t('chat.collapseAgents') : $t('chat.expandAgents')"
:aria-label="agentPanelOpen ? $t('chat.collapseAgents') : $t('chat.expandAgents')"
@click="agentPanelOpen = !agentPanelOpen"
>
<ChevronUp v-if="agentPanelOpen" class="h-4 w-4" />
<ChevronDown v-else class="h-4 w-4" />
</button>
</div>
<AgentCategoryList
v-show="agentPanelOpen"
:agents="userAgents"
:active-agent-id="currentAgentId"
compact
@select="emit('switch-agent', $event)"
/>
</div>
<div ref="userMenuRef" class="relative flex items-center justify-between text-muted">
<button
type="button"
class="flex h-10 min-w-[2.5rem] items-center justify-center rounded-full bg-brand-600 px-3 text-sm font-bold text-white shadow-sm shadow-brand-500/20 transition-colors hover:bg-brand-700"
@click="showUserDropdown = !showUserDropdown"
>
退出
</button>
<div
v-if="showUserDropdown"
class="absolute bottom-full left-0 z-50 mb-2 w-56 animate-fade-in rounded-xl border border-subtle bg-surface py-1 shadow-xl"
>
<div class="flex items-center gap-3 border-b border-subtle px-4 py-3">
<div class="flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-brand-600 text-lg font-bold text-white shadow-sm shadow-brand-500/20">
{{ userInitial('U') }}
</div>
<div class="overflow-hidden">
<div class="truncate text-sm font-bold text-primary">{{ currentUser?.username || 'User' }}</div>
<div v-if="(currentUser as any)?.employee_id" class="truncate text-xs text-muted">
{{ $t('chat.employeeId') }}: {{ (currentUser as any).employee_id }}
</div>
<div v-else class="truncate text-xs text-muted">{{ currentUser?.role }}</div>
</div>
</div>
<button
type="button"
class="flex w-full items-center gap-2 px-4 py-2 text-left text-sm text-red-600 transition-colors hover:bg-red-50"
@click="onLogout"
>
<LogOut class="h-4 w-4" />
{{ $t('chat.logout') }}
</button>
</div>
<div class="flex items-center gap-3">
<ExternalSystemLinks
compact
:show-label="false"
button-class="inline-flex items-center justify-center transition-colors hover:text-secondary"
icon-class="h-4 w-4 shrink-0"
/>
<button
type="button"
class="inline-flex items-center justify-center transition-colors hover:text-secondary"
:title="appliedTheme === 'dark' ? ($t('theme.switchToLight') || 'Switch to light mode') : ($t('theme.switchToDark') || 'Switch to dark mode')"
@click="emit('toggle-theme')"
>
<Sun v-if="appliedTheme === 'dark'" class="h-4 w-4" />
<Moon v-else class="h-4 w-4" />
</button>
<LocaleToggleButton variant="sidebar" />
<button type="button" class="inline-flex items-center justify-center transition-colors hover:text-secondary" :title="$t('chat.generalFeedback')" @click="emit('open-feedback')">
<MessageSquareText class="h-4 w-4" />
</button>
<button type="button" class="inline-flex items-center justify-center transition-colors hover:text-secondary" :title="$t('workspace.title')" @click="emit('go-workspace')">
<Briefcase class="h-4 w-4" />
</button>
<button
v-if="currentUser?.role === 'admin'"
type="button"
class="inline-flex items-center justify-center transition-colors hover:text-secondary"
:title="$t('admin.title')"
@click="onGoAdmin"
>
<Settings class="h-4 w-4" />
</button>
</div>
</div>
</div>
</template>
</aside>
</template>