/*
Copyright (C) 2025 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import React, { useContext, useEffect, useState } from 'react';
import { Select } from 'antd';
import UserCenterPageHeader from '../../components/common/UserCenterPageHeader';
import dashboardCompute from '../../assets/prototype/dashboard-compute.svg';
import { Button, Progress, Tag } from '@/components/ui';
import { IconFilter, IconRefresh } from '@/components/ui/icons';
import { Sparkles, Crown, Zap, Clock, TrendingUp } from 'lucide-react';
import { UserContext } from '../../context/User';
import { StatusContext } from '../../context/Status';
import StatsCards from '../../components/dashboard/StatsCards';
import ChartsPanel from '../../components/dashboard/ChartsPanel';
import SearchModal from '../../components/dashboard/modals/SearchModal';
import NoticeModal from '../../components/layout/NoticeModal';
import { API, renderQuota } from '../../helpers';
import { useIsMobile } from '../../hooks/common/useIsMobile';
import { useDashboardData } from '../../hooks/dashboard/useDashboardData';
import { useDashboardStats } from '../../hooks/dashboard/useDashboardStats';
import { useDashboardCharts } from '../../hooks/dashboard/useDashboardCharts';
import { CHART_CONFIG, CARD_PROPS } from '../../constants/dashboard.constants';
import { getTrendSpec } from '../../helpers/dashboard';
const UserDashboard = () => {
// ========== Context ==========
const [userState, userDispatch] = useContext(UserContext);
const [statusState] = useContext(StatusContext);
// ========== 公告弹窗状态 ==========
const [noticeVisible, setNoticeVisible] = useState(false);
const isMobile = useIsMobile();
// ========== 会员额度数据 ==========
const [membershipInfo, setMembershipInfo] = useState(null);
const [membershipLoading, setMembershipLoading] = useState(true);
const fetchMembershipInfo = async () => {
setMembershipLoading(true);
try {
const res = await API.get('/api/user/membership/info');
const { success, data } = res.data;
if (success && data?.has_membership) {
setMembershipInfo(data);
}
} catch (err) {
console.error('获取会员信息失败:', err);
} finally {
setMembershipLoading(false);
}
};
// ========== 主要数据管理 ==========
const dashboardData = useDashboardData(userState, userDispatch, statusState);
// ========== 图表管理 ==========
const dashboardCharts = useDashboardCharts(
dashboardData.dataExportDefaultTime,
dashboardData.setTrendData,
dashboardData.setConsumeQuota,
dashboardData.setTimes,
dashboardData.setConsumeTokens,
dashboardData.setPieData,
dashboardData.setLineData,
dashboardData.setModelColors,
dashboardData.t,
);
// ========== 统计数据 ==========
const { groupedStatsData } = useDashboardStats(
userState,
dashboardData.consumeQuota,
dashboardData.consumeTokens,
dashboardData.times,
dashboardData.trendData,
dashboardData.performanceMetrics,
dashboardData.navigate,
dashboardData.t,
dashboardData.tokenUsage,
);
// ========== 数据处理 ==========
const initChart = async () => {
await Promise.all([
dashboardData.loadQuotaData().then((data) => {
if (data && data.length > 0) {
dashboardCharts.updateChartData(data);
}
}),
dashboardData.loadTokenUsageData(),
dashboardData.getUserData(),
fetchMembershipInfo(),
]);
};
const handleSearchConfirm = async () => {
const [data] = await Promise.all([
dashboardData.loadQuotaData(),
dashboardData.loadTokenUsageData(),
]);
if (data && data.length > 0) {
dashboardCharts.updateChartData(data);
}
dashboardData.handleCloseModal();
};
// ========== Effects ==========
useEffect(() => {
initChart();
}, []);
// ========== 公告弹窗检查 ==========
useEffect(() => {
const checkNoticeAndShow = async () => {
const lastCloseDate = localStorage.getItem('notice_close_date');
const today = new Date().toDateString();
if (lastCloseDate !== today) {
try {
const res = await API.get('/api/notice');
const { success, data } = res.data;
if (success && data && data.trim() !== '') {
setNoticeVisible(true);
}
} catch (error) {
console.error('获取公告失败:', error);
}
}
};
checkNoticeAndShow();
}, []);
return (
{/* 原型 .dashboard-hero 装饰层:渐变、网格线、底部渐隐与右侧插画(纯视觉,不参与交互) */}
{/* 公告弹窗 */}
setNoticeVisible(false)}
isMobile={isMobile}
/>
{/* 页面背景由父级 UserCenter shell 的 PageBackground 统一提供,此处不再叠加装饰 blob */}
{/* 主内容 - flex 列布局,数据分析区撑满剩余视口 */}
{/* 时间粒度:与筛选弹窗中的同名下拉共用 handleInputChange / 本地存储;
新粒度在下一次“刷新 / 筛选数据”加载时生效,不新增请求触发 */}
}
/>
{/* 详细统计卡片 —— 扁平化:去掉外层白 panel,实底卡直接浮在页面 bg-glow 上 */}
{/* 会员额度使用卡片(原型无此状态:按统一规范使用语义底色/边框,无渐变、光晕与悬停位移) */}
{!membershipLoading && membershipInfo && (
{dashboardData.t('会员权益')}
{membershipInfo.plan_name}
{dashboardData.t('剩余')} {membershipInfo.remaining_days}{' '}
{dashboardData.t('天')}
{dashboardData.t('今日剩余')}
{renderQuota(membershipInfo.membership_quota)}
{membershipInfo.daily_quota > 0 && (
)}
{dashboardData.t('今日消耗')}
{renderQuota(
Math.max(
0,
membershipInfo.daily_quota -
membershipInfo.membership_quota,
),
)}
{membershipInfo.daily_quota > 0 && (
{dashboardData.t('使用率')}{' '}
{Math.round(
Math.max(
0,
(membershipInfo.daily_quota -
membershipInfo.membership_quota) /
membershipInfo.daily_quota,
) * 100,
)}
%
)}
{dashboardData.t('每日总额')}
{renderQuota(membershipInfo.daily_quota)}
{dashboardData.t('每日自动重置')}
{dashboardData.t('累计消耗')}
{renderQuota(membershipInfo.membership_used_quota || 0)}
{dashboardData.t('会员期间总消耗')}
)}
{/* 模型数据分析图表 - 自适应填满剩余视口高度。扁平化:去外层白 panel,图卡自身实底 */}
{/* 区块标题 */}
{dashboardData.t('用量洞察')}
);
};
export default UserDashboard;