refactor(frontend): 重构前端第二版

This commit is contained in:
sophie committed 2026-06-11 13:47:06 +08:00
1 parent 2631f78b00
commit 4b562cfa82
205 files changed
+15399 -12015

No files matched your search

@@ -0,0 +1,332 @@
/**
* features/chat/components/ChatSidebar.vue
* 桌面端聊天侧栏:Agent 信息、会话历史、Agent 切换面板与底部用户菜单。
*/
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue';
import {
BookOpen,
Briefcase,
LayoutGrid,
LogOut,
MessageSquareText,
Moon,
PanelLeftClose,
PanelLeftOpen,
Plus,
Settings,
Sparkles,
Sun,
X,
} from 'lucide-vue-next';
import LocaleToggleButton from '@/shared/components/LocaleToggleButton.vue';
import type { DisplayHistoryItem } from '../types/chat.types';
export interface ChatSidebarAgent {
id: number;
name: string;
icon_url?: string;
}
const collapsed = defineModel<boolean>('collapsed', { required: true });
const agentPanelOpen = defineModel<boolean>('agentPanelOpen', { required: true });
const { currentUser } = defineProps<{
isOpen: boolean;
currentAgent: { name?: string } | null;
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 userInitial = (fallback: string) => {
const name = String(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">
<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">
{{ currentAgent?.name === '人事规章' ? 'HR' : 'AI' }}
</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 && agentPanelOpen" class="chat-agent-panel">
<div class="mb-2 flex items-center gap-1.5 text-xs font-semibold text-muted">
<LayoutGrid class="h-3.5 w-3.5" /> {{ $t('chat.agents') }}
</div>
<div class="grid grid-cols-2 gap-x-2 gap-y-1.5">
<button
v-for="agent in userAgents"
:key="agent.id"
type="button"
class="grid w-full min-w-0 grid-cols-[1rem_1fr] items-center gap-1.5 rounded-xl px-2 py-2 text-left text-xs font-medium text-secondary transition-colors hover-surface-soft hover:shadow-sm"
@click="emit('switch-agent', agent)"
>
<div
v-if="agent.icon_url"
class="h-4 w-4 shrink-0 rounded bg-contain bg-center bg-no-repeat"
:style="{ backgroundImage: `url(${agent.icon_url})` }"
/>
<div v-else class="flex h-4 w-4 shrink-0 items-center justify-center rounded bg-brand-100 text-[9px] font-bold text-brand-600">
{{ agent.name.charAt(0) }}
</div>
<span class="min-w-0 truncate">{{ agent.name }}</span>
</button>
</div>
</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
v-if="currentUser?.role === 'admin'"
type="button"
class="flex w-full items-center gap-2 px-4 py-2 text-left text-sm text-secondary transition-colors hover:bg-surface-soft"
@click="onGoAdmin"
>
<Settings class="h-4 w-4" />
{{ $t('workspace.nav.admin') }}
</button>
<button
type="button"
class="flex w-full items-center gap-2 border-t border-subtle 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 gap-3">
<button
type="button"
:class="agentPanelOpen && userAgents.length > 0 ? 'text-brand-600' : ''"
class="transition-colors hover:text-secondary"
:title="userAgents.length === 0 ? $t('chat.noAvailableAgents') : (agentPanelOpen ? $t('chat.collapseAgents') : $t('chat.expandAgents'))"
@click="userAgents.length === 0 ? undefined : (agentPanelOpen = !agentPanelOpen)"
>
<LayoutGrid class="h-4 w-4" />
</button>
<button
type="button"
class="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="transition-colors hover:text-secondary" :title="$t('chat.generalFeedback')" @click="emit('open-feedback')">
<MessageSquareText class="h-4 w-4" />
</button>
<button type="button" class="transition-colors hover:text-secondary" :title="$t('workspace.title')" @click="emit('go-workspace')">
<Briefcase class="h-4 w-4" />
</button>
</div>
</div>
</div>
</template>
</aside>
</template>