import React, { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; import { Row, Col, Card, Statistic, List, Tag, Button, Spin, Empty } from 'antd'; import { CheckCircleOutlined, ClockCircleOutlined, ExclamationCircleOutlined, ScheduleOutlined, RobotOutlined } from '@ant-design/icons'; import axios from 'axios'; import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js'; import { Doughnut } from 'react-chartjs-2'; import MoodSelector from '../components/MoodSelector'; ChartJS.register(ArcElement, Tooltip, Legend); const Dashboard = () => { const [loading, setLoading] = useState(true); const [stats, setStats] = useState({ totalTasks: 0, completedTasks: 0, pendingTasks: 0, importanceCounts: [], }); const [recentTasks, setRecentTasks] = useState([]); const [schedules, setSchedules] = useState([]); const [todayMood, setTodayMood] = useState(null); useEffect(() => { const fetchDashboardData = async () => { try { setLoading(true); // 获取任务数据 const tasksRes = await axios.get('/api/tasks'); const tasks = tasksRes.data; // 获取时间安排数据 const schedulesRes = await axios.get('/api/schedules'); const allSchedules = schedulesRes.data; // 获取心情数据 const moodsRes = await axios.get('/api/moods'); const moods = moodsRes.data; // 计算统计数据 const completedTasks = tasks.filter(task => task.completed).length; const pendingTasks = tasks.filter(task => !task.completed).length; // 按重要性分组 const importanceCounts = Array(5).fill(0); tasks.forEach(task => { if (task.importance >= 1 && task.importance <= 5) { importanceCounts[task.importance - 1]++; } }); // 获取最近任务(按创建日期排序) const sortedTasks = [...tasks].sort((a, b) => new Date(b.created_date) - new Date(a.created_date) ).slice(0, 5); // 当前星期几(1-5对应周一到周五) const today = new Date().getDay(); const dayOfWeek = today === 0 ? 5 : today === 6 ? 5 : today; // 过滤出今天的时间安排 const todaySchedules = allSchedules.filter( schedule => schedule.day_of_week === dayOfWeek ).sort((a, b) => a.time_slot.localeCompare(b.time_slot)); // 查找今天的心情 const todayStr = new Date().toISOString().split('T')[0]; const todayMoodRecord = moods.find(mood => mood.date === todayStr); setStats({ totalTasks: tasks.length, completedTasks, pendingTasks, importanceCounts, }); setRecentTasks(sortedTasks); setSchedules(todaySchedules); setTodayMood(todayMoodRecord?.mood_value || null); } catch (error) { console.error('获取仪表盘数据失败:', error); } finally { setLoading(false); } }; fetchDashboardData(); }, []); // 饼图数据 const chartData = { labels: ['非常低', '低', '中等', '高', '非常高'], datasets: [ { data: stats.importanceCounts, backgroundColor: [ '#e5e7eb', '#bfdbfe', '#c7d2fe', '#fef3c7', '#fee2e2', ], borderColor: [ '#d1d5db', '#93c5fd', '#a5b4fc', '#fde68a', '#fca5a5', ], borderWidth: 1, }, ], }; // 获取重要性标签 const getImportanceTag = (importance) => { const colors = ['default', 'blue', 'purple', 'warning', 'error']; const labels = ['非常低', '低', '中等', '高', '非常高']; return ( {labels[importance - 1] || '未知'} ); }; // 心情选择回调 const handleMoodSelect = async (value) => { try { await axios.post('/api/moods', { mood_value: value }); setTodayMood(value); } catch (error) { console.error('记录心情失败:', error); } }; if (loading) { return (
); } return (

仪表盘

{/* 统计卡片 */} } /> } /> } /> {/* 今日心情 */} {/* 最近任务 */} 查看全部} > {recentTasks.length > 0 ? ( ( {task.name}
{getImportanceTag(task.importance)} {task.completed && 已完成 }
} description={

预计完成时间: {task.expected_completion_date}

{task.notes &&

{task.notes}

}
} /> )} /> ) : ( )} {/* 任务重要性分布 */} {stats.totalTasks > 0 ? (
) : ( )}
{/* 今日安排 */} 查看全部} > {schedules.length > 0 ? ( ( {schedule.time_slot} - {schedule.activity} {schedule.completed && 已完成 } } /> )} /> ) : ( )} {/* AI助手链接 */}
); }; export default Dashboard;