import { useState, useEffect } from 'react' import { Users, Zap, Activity as ActivityIcon, Database, Loader2 } from 'lucide-react' import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/Card' import { apiClient } from '@/services/api' import type { DashboardStats, Activity } from '@/types' // Fallback data when API is not available const fallbackStats: DashboardStats = { totalUsers: 1234, activeUsers: 856, systemLoad: 32, dbStatus: '正常', userGrowth: 65, // Single value for chart } const fallbackActivities: Activity[] = [ { id: 1, user: 'john@example.com', action: '登录系统', time: '5 分钟前', status: 'success' }, { id: 2, user: 'jane@example.com', action: '更新资料', time: '15 分钟前', status: 'success' }, { id: 3, user: 'admin@example.com', action: '创建用户', time: '1 小时前', status: 'success' }, { id: 4, user: 'bob@example.com', action: '修改密码', time: '2 小时前', status: 'success' }, { id: 5, user: 'alice@example.com', action: '登录失败', time: '3 小时前', status: 'error' }, ] // Chart data (12 months) const chartData = [65, 72, 68, 80, 75, 85, 82, 90, 88, 95, 92, 100] export function DashboardPage() { const [isLoading, setIsLoading] = useState(true) const [stats, setStats] = useState(null) const [activities, setActivities] = useState([]) const [error, setError] = useState(null) useEffect(() => { loadDashboardData() }, []) const loadDashboardData = async () => { try { setIsLoading(true) setError(null) // 调用仪表盘 API 获取真实统计数据 const [statsResponse, activitiesResponse] = await Promise.all([ apiClient.getDashboardStats().catch(() => null), apiClient.getRecentActivities(5).catch(() => null), ]) if (statsResponse?.success && statsResponse.data) { setStats(statsResponse.data) } else { setStats(fallbackStats) } if (activitiesResponse?.success && activitiesResponse.data) { setActivities(activitiesResponse.data) } else { setActivities(fallbackActivities) } } catch (err) { console.error('加载仪表盘数据失败:', err) setError('加载数据失败') setStats(fallbackStats) setActivities(fallbackActivities) } finally { setIsLoading(false) } } // Format number with commas const formatNumber = (num: number): string => { return num.toLocaleString('zh-CN') } // Calculate growth percentage (mock calculation) const calculateGrowth = (current: number): string => { const growth = Math.floor(Math.random() * 20) - 5 return growth >= 0 ? `+${growth}%` : `${growth}%` } const statsConfig = [ { title: '总用户数', value: stats ? formatNumber(stats.totalUsers) : '-', change: calculateGrowth(stats?.totalUsers || 0), icon: Users, color: 'from-sky-500 to-blue-600', }, { title: '活跃用户', value: stats ? formatNumber(stats.activeUsers) : '-', change: calculateGrowth(stats?.activeUsers || 0), icon: ActivityIcon, color: 'from-green-500 to-emerald-600', }, { title: '系统负载', value: stats ? `${stats.systemLoad}%` : '-', change: '-5%', icon: Zap, color: 'from-amber-500 to-orange-600', }, { title: '数据库状态', value: stats?.dbStatus || '正常', change: '稳定', icon: Database, color: 'from-purple-500 to-violet-600', }, ] if (isLoading) { return (
) } return (
{error && (
{error}
)} {/* Stats Grid */}
{statsConfig.map((stat, index) => (

{stat.title}

{stat.value}

{stat.change}

))}
{/* Main Content Grid */}
{/* Chart */} 用户增长趋势
{chartData.map((height, i) => (
{height}%
))}
1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月
{/* Recent Activity */} 最近活动
{activities.map((activity) => (

{activity.user}

{activity.action}

{activity.time}
))}
{/* Quick Actions */} 快捷操作
{[ { label: '添加用户', icon: Users, action: 'users' }, { label: '系统设置', icon: Zap, action: 'settings' }, { label: '数据备份', icon: Database, action: 'backup' }, { label: '查看日志', icon: ActivityIcon, action: 'logs' }, ].map((item, index) => ( ))}
) }