luom2645's picture
Upload folder using huggingface_hub
9dd9a58 verified
Raw History Blame Contribute Delete
16.5 kB
import React, { useState, useEffect } from 'react';
import { useNotification } from '../../contexts/NotificationContext';
import { SecurityEvent, SystemStats } from '../../types';
import {
UsersIcon,
KeyIcon,
ShieldExclamationIcon,
DocumentChartBarIcon,
EyeIcon,
CheckIcon
} from '@heroicons/react/24/outline';
import LoadingSpinner from '../UI/LoadingSpinner';
const AdminPanel: React.FC = () => {
const { showNotification } = useNotification();
const [activeTab, setActiveTab] = useState('overview');
const [systemStats, setSystemStats] = useState<SystemStats>({
cpu_usage: 35,
memory_usage: 4.2,
memory_total: 16,
active_users: 156,
total_novels: 1247,
ai_requests_today: 3421,
storage_used: 156.7,
storage_total: 500
});
const [securityEvents, setSecurityEvents] = useState<SecurityEvent[]>([
{
id: '1',
event_type: 'login_failure',
severity: 'medium',
user_id: 'user_123',
ip_address: '192.168.1.100',
description: '多次密码错误尝试',
resolved: false,
created_at: new Date().toISOString()
},
{
id: '2',
event_type: 'suspicious_activity',
severity: 'high',
user_id: 'user_456',
ip_address: '10.0.0.50',
description: '异常高频API调用',
resolved: false,
created_at: new Date(Date.now() - 3600000).toISOString()
},
{
id: '3',
event_type: 'device_change',
severity: 'low',
user_id: 'user_789',
ip_address: '172.16.0.25',
description: '用户更换了登录设备',
resolved: true,
resolved_at: new Date(Date.now() - 1800000).toISOString(),
created_at: new Date(Date.now() - 7200000).toISOString()
}
]);
const [isLoading, setIsLoading] = useState(false);
// 模拟系统状态实时更新
useEffect(() => {
const interval = setInterval(() => {
setSystemStats(prev => ({
...prev,
cpu_usage: Math.max(10, Math.min(90, prev.cpu_usage + (Math.random() - 0.5) * 15)),
memory_usage: Math.max(2, Math.min(14, prev.memory_usage + (Math.random() - 0.5) * 0.8)),
ai_requests_today: prev.ai_requests_today + Math.floor(Math.random() * 3)
}));
}, 5000);
return () => clearInterval(interval);
}, []);
const handleResolveEvent = async (eventId: string) => {
setIsLoading(true);
try {
await new Promise(resolve => setTimeout(resolve, 1000));
setSecurityEvents(prev =>
prev.map(event =>
event.id === eventId
? { ...event, resolved: true, resolved_at: new Date().toISOString() }
: event
)
);
showNotification('处理成功', '安全事件已标记为已解决', 'success');
} catch (error) {
showNotification('处理失败', '无法更新事件状态', 'error');
} finally {
setIsLoading(false);
}
};
const getSeverityColor = (severity: SecurityEvent['severity']) => {
switch (severity) {
case 'critical': return 'text-danger-red';
case 'high': return 'text-warning-yellow';
case 'medium': return 'text-accent-blue';
case 'low': return 'text-success-green';
default: return 'text-text-secondary';
}
};
const getSeverityText = (severity: SecurityEvent['severity']) => {
switch (severity) {
case 'critical': return '紧急';
case 'high': return '高危';
case 'medium': return '中危';
case 'low': return '低危';
default: return '未知';
}
};
const getEventTypeText = (type: SecurityEvent['event_type']) => {
switch (type) {
case 'login_failure': return '登录失败';
case 'suspicious_activity': return '可疑活动';
case 'api_abuse': return 'API滥用';
case 'unauthorized_access': return '未授权访问';
case 'device_change': return '设备更换';
case 'privilege_escalation': return '权限提升';
default: return '未知事件';
}
};
const tabs = [
{ id: 'overview', label: '系统概览', icon: DocumentChartBarIcon },
{ id: 'users', label: '用户管理', icon: UsersIcon },
{ id: 'licenses', label: '许可证管理', icon: KeyIcon },
{ id: 'security', label: '安全监控', icon: ShieldExclamationIcon }
];
const renderTabContent = () => {
switch (activeTab) {
case 'overview':
return (
<div className="space-y-6">
<h2 className="text-2xl font-semibold text-text-highlight">系统概览</h2>
{/* 关键指标 */}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
<div className="glass-panel p-6 text-center">
<div className="text-3xl font-bold text-accent-cyan mb-2">
{systemStats.active_users}
</div>
<p className="text-text-secondary">活跃用户</p>
</div>
<div className="glass-panel p-6 text-center">
<div className="text-3xl font-bold text-success-green mb-2">
{systemStats.total_novels.toLocaleString()}
</div>
<p className="text-text-secondary">总小说数</p>
</div>
<div className="glass-panel p-6 text-center">
<div className="text-3xl font-bold text-warning-yellow mb-2">
{systemStats.ai_requests_today.toLocaleString()}
</div>
<p className="text-text-secondary">今日AI请求</p>
</div>
<div className="glass-panel p-6 text-center">
<div className="text-3xl font-bold text-text-highlight mb-2">
{Math.round(systemStats.cpu_usage)}%
</div>
<p className="text-text-secondary">CPU使用率</p>
</div>
</div>
{/* 系统资源 */}
<div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<div className="glass-panel p-6">
<h3 className="text-lg font-semibold text-text-highlight mb-4">系统资源</h3>
<div className="space-y-4">
<div>
<div className="flex justify-between mb-2">
<span className="text-text-secondary">CPU 使用率</span>
<span className="text-text-highlight">{Math.round(systemStats.cpu_usage)}%</span>
</div>
<div className="progress-bar">
<div
className="progress-fill"
style={{ width: `${systemStats.cpu_usage}%` }}
></div>
</div>
</div>
<div>
<div className="flex justify-between mb-2">
<span className="text-text-secondary">内存使用</span>
<span className="text-text-highlight">
{systemStats.memory_usage.toFixed(1)}GB / {systemStats.memory_total}GB
</span>
</div>
<div className="progress-bar">
<div
className="progress-fill"
style={{ width: `${(systemStats.memory_usage / systemStats.memory_total) * 100}%` }}
></div>
</div>
</div>
<div>
<div className="flex justify-between mb-2">
<span className="text-text-secondary">存储使用</span>
<span className="text-text-highlight">
{systemStats.storage_used}GB / {systemStats.storage_total}GB
</span>
</div>
<div className="progress-bar">
<div
className="progress-fill"
style={{ width: `${(systemStats.storage_used / systemStats.storage_total) * 100}%` }}
></div>
</div>
</div>
</div>
</div>
<div className="glass-panel p-6">
<h3 className="text-lg font-semibold text-text-highlight mb-4">最近安全事件</h3>
<div className="space-y-3">
{securityEvents.slice(0, 3).map(event => (
<div key={event.id} className="flex items-center justify-between p-3 bg-secondary-bg/50 rounded-lg">
<div className="flex-1">
<div className="flex items-center space-x-2">
<span className={`text-xs font-medium ${getSeverityColor(event.severity)}`}>
{getSeverityText(event.severity)}
</span>
<span className="text-sm text-text-primary">
{getEventTypeText(event.event_type)}
</span>
</div>
<p className="text-xs text-text-secondary mt-1">
{event.description}
</p>
</div>
<div className={`w-2 h-2 rounded-full ${event.resolved ? 'bg-success-green' : 'bg-danger-red'}`}></div>
</div>
))}
</div>
</div>
</div>
</div>
);
case 'users':
return (
<div className="space-y-6">
<h2 className="text-2xl font-semibold text-text-highlight">用户管理</h2>
<div className="text-center py-12">
<UsersIcon className="w-16 h-16 text-text-secondary mx-auto mb-4" />
<p className="text-text-secondary">用户管理功能开发中...</p>
</div>
</div>
);
case 'licenses':
return (
<div className="space-y-6">
<h2 className="text-2xl font-semibold text-text-highlight">许可证管理</h2>
<div className="text-center py-12">
<KeyIcon className="w-16 h-16 text-text-secondary mx-auto mb-4" />
<p className="text-text-secondary">许可证管理功能开发中...</p>
</div>
</div>
);
case 'security':
return (
<div className="space-y-6">
<h2 className="text-2xl font-semibold text-text-highlight">安全监控</h2>
{/* 筛选和操作 */}
<div className="flex items-center justify-between">
<div className="flex space-x-4">
<select className="cyber-input w-40">
<option value="all">所有事件</option>
<option value="unresolved">未解决</option>
<option value="resolved">已解决</option>
</select>
<select className="cyber-input w-32">
<option value="all">所有级别</option>
<option value="critical">紧急</option>
<option value="high">高危</option>
<option value="medium">中危</option>
<option value="low">低危</option>
</select>
</div>
<button className="cyber-btn cyber-btn-secondary">
刷新数据
</button>
</div>
{/* 安全事件列表 */}
<div className="glass-panel p-6">
<div className="space-y-4">
{securityEvents.map(event => (
<div
key={event.id}
className={`
p-4 rounded-lg border transition-all duration-200
${
event.resolved
? 'border-success-green/30 bg-success-green/10'
: event.severity === 'critical' || event.severity === 'high'
? 'border-danger-red/30 bg-danger-red/10'
: 'border-border-color bg-secondary-bg/30'
}
`}
>
<div className="flex items-start justify-between">
<div className="flex-1">
<div className="flex items-center space-x-3 mb-2">
<span className={`px-2 py-1 rounded text-xs font-medium ${getSeverityColor(event.severity)} bg-current/20`}>
{getSeverityText(event.severity)}
</span>
<span className="text-sm font-medium text-text-highlight">
{getEventTypeText(event.event_type)}
</span>
{event.resolved && (
<CheckIcon className="w-4 h-4 text-success-green" />
)}
</div>
<p className="text-text-secondary mb-3">{event.description}</p>
<div className="grid grid-cols-1 md:grid-cols-3 gap-2 text-xs text-text-secondary">
<div>
<span className="font-medium">用户ID:</span> {event.user_id || 'N/A'}
</div>
<div>
<span className="font-medium">IP地址:</span> {event.ip_address || 'N/A'}
</div>
<div>
<span className="font-medium">时间:</span> {new Date(event.created_at).toLocaleString('zh-CN')}
</div>
</div>
</div>
<div className="flex space-x-2 ml-4">
<button className="p-2 text-text-secondary hover:text-accent-cyan transition-colors" title="查看详情">
<EyeIcon className="w-4 h-4" />
</button>
{!event.resolved && (
<button
onClick={() => handleResolveEvent(event.id)}
disabled={isLoading}
className="p-2 text-text-secondary hover:text-success-green transition-colors disabled:opacity-50"
title="标记为已解决"
>
{isLoading ? <LoadingSpinner size="sm" /> : <CheckIcon className="w-4 h-4" />}
</button>
)}
</div>
</div>
</div>
))}
</div>
</div>
</div>
);
default:
return null;
}
};
return (
<div className="space-y-6">
{/* 页面标题 */}
<div className="text-center">
<h1 className="text-4xl font-bold text-text-highlight mb-4">
<span className="bg-gradient-to-r from-accent-cyan to-accent-blue bg-clip-text text-transparent">
管理控制台
</span>
</h1>
<p className="text-lg text-text-secondary">
系统管理和安全监控
</p>
</div>
{/* 导航标签 */}
<div className="glass-panel p-4">
<nav className="flex space-x-1">
{tabs.map(tab => {
const Icon = tab.icon;
return (
<button
key={tab.id}
onClick={() => setActiveTab(tab.id)}
className={`
flex items-center space-x-2 px-6 py-3 rounded-lg transition-all duration-200
${
activeTab === tab.id
? 'bg-accent-cyan/20 text-accent-cyan border border-accent-cyan/50'
: 'text-text-secondary hover:text-text-primary hover:bg-border-color/30'
}
`}
>
<Icon className="w-5 h-5" />
<span className="font-medium">{tab.label}</span>
</button>
);
})}
</nav>
</div>
{/* 标签内容 */}
<div className="glass-panel p-6">
{renderTabContent()}
</div>
</div>
);
};
export default AdminPanel;