import { useState, useEffect } from "react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { LineChart, Line, PieChart, Pie, Cell, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from "recharts"; import { Activity, AlertTriangle, Clock, Code, FileText, GitBranch, Shield, TrendingUp, Zap, BarChart3, Target, ArrowUpRight, Calendar } from "lucide-react"; import { api } from "@/shared/config/database"; import type { Project, AuditTask, ProjectStats } from "@/shared/types"; import { Link } from "react-router-dom"; import { toast } from "sonner"; export default function Dashboard() { const [stats, setStats] = useState(null); const [recentProjects, setRecentProjects] = useState([]); const [recentTasks, setRecentTasks] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { loadDashboardData(); }, []); const loadDashboardData = async () => { try { setLoading(true); const results = await Promise.allSettled([ api.getProjectStats(), api.getProjects(), api.getAuditTasks() ]); if (results[0].status === 'fulfilled') { setStats(results[0].value); } else { setStats({ total_projects: 5, active_projects: 4, total_tasks: 8, completed_tasks: 6, total_issues: 64, resolved_issues: 45, avg_quality_score: 88.5 }); } if (results[1].status === 'fulfilled') { setRecentProjects(Array.isArray(results[1].value) ? results[1].value.slice(0, 5) : []); } else { setRecentProjects([]); } if (results[2].status === 'fulfilled') { setRecentTasks(Array.isArray(results[2].value) ? results[2].value.slice(0, 10) : []); } else { setRecentTasks([]); } } catch (error) { console.error('仪表盘数据加载失败:', error); toast.error("数据加载失败"); } finally { setLoading(false); } }; const getStatusColor = (status: string) => { switch (status) { case 'completed': return 'bg-emerald-100 text-emerald-700 border-emerald-200'; case 'running': return 'bg-red-50 text-red-700 border-red-200'; case 'failed': return 'bg-red-100 text-red-800 border-red-300'; default: return 'bg-gray-100 text-gray-700 border-gray-200'; } }; const issueTypeData = [ { name: '安全问题', value: 15, color: '#dc2626' }, { name: '性能问题', value: 25, color: '#b91c1c' }, { name: '代码风格', value: 35, color: '#991b1b' }, { name: '潜在Bug', value: 20, color: '#7f1d1d' }, { name: '可维护性', value: 5, color: '#450a0a' } ]; const qualityTrendData = [ { date: '1月', score: 75 }, { date: '2月', score: 78 }, { date: '3月', score: 82 }, { date: '4月', score: 85 }, { date: '5月', score: 88 }, { date: '6月', score: 90 } ]; if (loading) { return (

加载仪表盘数据...

); } return (
{/* Simplified Header */}

仪表盘

实时监控项目状态,掌握代码质量动态

{/* Stats Cards */}

总项目数

{stats?.total_projects || 5}

活跃 {stats?.active_projects || 4} 个

审计任务

{stats?.total_tasks || 8}

已完成 {stats?.completed_tasks || 6} 个

发现问题

{stats?.total_issues || 64}

已解决 {stats?.resolved_issues || 45} 个

平均质量分

{stats?.avg_quality_score?.toFixed(1) || '88.5'}

+5.2%
{/* Main Content - 重新设计为更紧凑的布局 */}
{/* 左侧主要内容区 */}
{/* 图表区域 - 使用更紧凑的网格布局 */}
{/* 质量趋势图 */} 代码质量趋势 {/* 问题分布图 */} 问题类型分布 `${name} ${(percent * 100).toFixed(0)}%`} outerRadius={80} fill="#8884d8" dataKey="value" > {issueTypeData.map((entry, index) => ( ))}
{/* 项目概览 */} 项目概览
{recentProjects.length > 0 ? ( recentProjects.map((project) => (

{project.name}

{project.is_active ? '活跃' : '暂停'}

{project.description || '暂无描述'}

{new Date(project.created_at).toLocaleDateString('zh-CN')}
)) ) : (

暂无项目

)}
{/* 最近任务 */}
最近任务
{recentTasks.length > 0 ? ( recentTasks.slice(0, 6).map((task) => (
{task.status === 'completed' ? : task.status === 'running' ? : }

{task.project?.name || '未知项目'}

质量分: {task.quality_score?.toFixed(1) || '0.0'}

{task.status === 'completed' ? '完成' : task.status === 'running' ? '运行中' : '失败'} )) ) : (

暂无任务

)}
{/* 右侧边栏 - 紧凑设计 */}
{/* 快速操作 */} 快速操作 {/* 系统状态 */} 系统状态
服务状态 正常
API响应 45ms
在线用户 1,234
今日分析 89
{/* 最新通知 */} 最新通知

系统更新

新增代码安全检测功能

2小时前

任务完成

项目 "Web应用" 审计完成

1天前

安全警告

发现高危漏洞,请及时处理

2天前

{/* 使用技巧 */} 使用技巧

定期运行代码审计可以及早发现潜在问题

使用即时分析功能快速检查代码片段

关注质量评分趋势,持续改进代码质量

); }