Files
aiops/frontend/src/components/layout/Header.tsx
T
attacker 97c4207ff8 refactor: 标准化项目结构 frontend/ + backend/ + SQLite + run.sh
- 目录重构: app/ -> frontend/,与 backend/ 并列
- 数据库: 默认 SQLite(零配置),修复 DSN() bug
- run.sh: 一键启动前后端,支持 start/stop/restart/status
- Makefile: install/dev/build/lint/clean 快捷命令
- vite.config.ts: 配置 dev server 端口 5173 + API 代理到 8080
- config.yaml: SQLite 默认配置,开箱即用
- docs/: 完整开发文档(项目结构/API/数据库/规范/部署)
- 修复: GORM foreign key 关联声明
- 修复: go get gorm.io/driver/sqlite 依赖
2026-06-13 23:34:23 +08:00

263 lines
11 KiB
TypeScript

import { useState } from 'react';
import { NavLink } from 'react-router-dom';
import { cn } from '@/lib/utils';
import { useThemeStore, useTabStore, useNotificationStore } from '@/store';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Badge } from '@/components/ui/badge';
import { Separator } from '@/components/ui/separator';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import {
Search,
Bell,
Menu,
X,
Plus,
Settings,
User,
LogOut,
CheckCheck,
AlertTriangle,
Info,
CheckCircle,
Terminal,
Rocket,
Server,
Cloud,
} from 'lucide-react';
import { formatDistanceToNow } from '@/lib/utils';
export function Header() {
const { sidebarCollapsed, toggleSidebar } = useThemeStore();
const { tabs, activeTab, removeTab, setActiveTab } = useTabStore();
const { notifications, unreadCount, markAsRead, markAllAsRead } = useNotificationStore();
const [searchValue, setSearchValue] = useState('');
const getNotificationIcon = (type: string) => {
switch (type) {
case 'error':
return <AlertTriangle className="h-4 w-4 text-red-500" />;
case 'warning':
return <AlertTriangle className="h-4 w-4 text-amber-500" />;
case 'success':
return <CheckCircle className="h-4 w-4 text-emerald-500" />;
default:
return <Info className="h-4 w-4 text-blue-500" />;
}
};
return (
<header className="fixed top-0 right-0 left-0 z-30 h-14 bg-slate-900/95 backdrop-blur supports-[backdrop-filter]:bg-slate-900/60 border-b border-slate-800 transition-all duration-300"
style={{ marginLeft: sidebarCollapsed ? '4rem' : '16rem' }}
>
<div className="flex items-center justify-between h-full px-4">
{/* Left: Toggle & Search */}
<div className="flex items-center gap-3 flex-1">
{sidebarCollapsed && (
<Button
variant="ghost"
size="icon"
className="h-8 w-8 text-slate-400 hover:text-slate-100"
onClick={toggleSidebar}
>
<Menu className="h-4 w-4" />
</Button>
)}
{/* Global Search */}
<div className="relative w-96 max-w-full">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-slate-500" />
<Input
type="text"
placeholder="搜索服务器、应用、工单、文档..."
value={searchValue}
onChange={(e) => setSearchValue(e.target.value)}
className="pl-9 h-9 bg-slate-800/50 border-slate-700 text-slate-200 placeholder:text-slate-500 focus-visible:ring-blue-500/30"
/>
<kbd className="absolute right-3 top-1/2 -translate-y-1/2 px-1.5 py-0.5 text-xs font-mono text-slate-500 bg-slate-800 rounded">
⌘K
</kbd>
</div>
{/* Quick Actions */}
<div className="flex items-center gap-1">
<Button variant="ghost" size="sm" className="h-8 gap-1.5 text-slate-400 hover:text-slate-100">
<Terminal className="h-4 w-4" />
<span className="hidden lg:inline">WebShell</span>
</Button>
<Button variant="ghost" size="sm" className="h-8 gap-1.5 text-slate-400 hover:text-slate-100">
<Rocket className="h-4 w-4" />
<span className="hidden lg:inline">发布</span>
</Button>
</div>
</div>
{/* Right: Actions */}
<div className="flex items-center gap-2">
{/* Create New */}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon" className="h-8 w-8 text-slate-400 hover:text-slate-100">
<Plus className="h-4 w-4" />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-48 bg-slate-800 border-slate-700">
<DropdownMenuLabel className="text-slate-300">快速创建</DropdownMenuLabel>
<DropdownMenuSeparator className="bg-slate-700" />
<DropdownMenuItem className="text-slate-300 focus:bg-slate-700 focus:text-slate-100">
<Server className="h-4 w-4 mr-2" />
添加服务器
</DropdownMenuItem>
<DropdownMenuItem className="text-slate-300 focus:bg-slate-700 focus:text-slate-100">
<Cloud className="h-4 w-4 mr-2" />
添加K8s集群
</DropdownMenuItem>
<DropdownMenuItem className="text-slate-300 focus:bg-slate-700 focus:text-slate-100">
<Rocket className="h-4 w-4 mr-2" />
创建发布
</DropdownMenuItem>
<DropdownMenuItem className="text-slate-300 focus:bg-slate-700 focus:text-slate-100">
<Terminal className="h-4 w-4 mr-2" />
创建工单
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
{/* Notifications */}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon" className="h-8 w-8 text-slate-400 hover:text-slate-100 relative">
<Bell className="h-4 w-4" />
{unreadCount > 0 && (
<span className="absolute -top-0.5 -right-0.5 h-4 w-4 rounded-full bg-red-500 text-[10px] font-medium text-white flex items-center justify-center">
{unreadCount > 99 ? '99+' : unreadCount}
</span>
)}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-80 bg-slate-800 border-slate-700">
<DropdownMenuLabel className="flex items-center justify-between text-slate-300">
<span>通知</span>
{unreadCount > 0 && (
<Button
variant="ghost"
size="sm"
className="h-6 text-xs text-slate-400 hover:text-slate-100"
onClick={markAllAsRead}
>
<CheckCheck className="h-3 w-3 mr-1" />
全部已读
</Button>
)}
</DropdownMenuLabel>
<DropdownMenuSeparator className="bg-slate-700" />
<div className="max-h-80 overflow-y-auto">
{notifications.length === 0 ? (
<div className="py-8 text-center text-slate-500">
<Bell className="h-8 w-8 mx-auto mb-2 opacity-50" />
<p className="text-sm">暂无通知</p>
</div>
) : (
notifications.slice(0, 10).map((notification) => (
<DropdownMenuItem
key={notification.id}
className={cn(
'flex items-start gap-3 p-3 cursor-pointer focus:bg-slate-700',
!notification.read && 'bg-slate-700/30'
)}
onClick={() => markAsRead(notification.id)}
>
{getNotificationIcon(notification.type)}
<div className="flex-1 min-w-0">
<p className={cn('text-sm font-medium', !notification.read ? 'text-slate-200' : 'text-slate-400')}>
{notification.title}
</p>
<p className="text-xs text-slate-500 mt-0.5 line-clamp-2">{notification.message}</p>
<p className="text-xs text-slate-600 mt-1">
{formatDistanceToNow(notification.timestamp)}
</p>
</div>
{!notification.read && (
<span className="w-2 h-2 rounded-full bg-blue-500 mt-1" />
)}
</DropdownMenuItem>
))
)}
</div>
</DropdownMenuContent>
</DropdownMenu>
<Separator orientation="vertical" className="h-6 bg-slate-800" />
{/* User Menu */}
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" className="h-8 gap-2 px-2 text-slate-300 hover:text-slate-100">
<div className="w-6 h-6 rounded-full bg-blue-600 flex items-center justify-center text-white text-xs font-medium">
管
</div>
<span className="hidden md:inline text-sm">管理员</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-48 bg-slate-800 border-slate-700">
<DropdownMenuLabel className="text-slate-300">我的账号</DropdownMenuLabel>
<DropdownMenuSeparator className="bg-slate-700" />
<DropdownMenuItem className="text-slate-300 focus:bg-slate-700 focus:text-slate-100">
<User className="h-4 w-4 mr-2" />
个人中心
</DropdownMenuItem>
<DropdownMenuItem className="text-slate-300 focus:bg-slate-700 focus:text-slate-100">
<Settings className="h-4 w-4 mr-2" />
账号设置
</DropdownMenuItem>
<DropdownMenuSeparator className="bg-slate-700" />
<DropdownMenuItem className="text-red-400 focus:bg-slate-700 focus:text-red-400">
<LogOut className="h-4 w-4 mr-2" />
退出登录
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</div>
</div>
{/* Tabs */}
{tabs.length > 0 && (
<div className="flex items-center h-9 px-2 bg-slate-950 border-b border-slate-800 overflow-x-auto">
{tabs.map((tab) => (
<div
key={tab.id}
className={cn(
'group flex items-center gap-2 px-3 py-1.5 text-sm border-r border-slate-800 cursor-pointer transition-colors whitespace-nowrap',
activeTab === tab.id
? 'bg-slate-900 text-slate-100'
: 'text-slate-500 hover:text-slate-300 hover:bg-slate-900/50'
)}
onClick={() => setActiveTab(tab.id)}
>
<span>{tab.title}</span>
{tab.closable && (
<button
className="opacity-0 group-hover:opacity-100 p-0.5 rounded hover:bg-slate-700 transition-all"
onClick={(e) => {
e.stopPropagation();
removeTab(tab.id);
}}
>
<X className="h-3 w-3" />
</button>
)}
</div>
))}
</div>
)}
</header>
);
}