/*
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 { Button, Progress } from '@douyinfe/semi-ui';
import { IconFilter, IconRefresh } from '@douyinfe/semi-icons';
import {
BarChart2,
Sparkles,
Activity,
Calendar,
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 (
{/* 公告弹窗 */}
setNoticeVisible(false)}
isMobile={isMobile}
/>
{/* 页面背景由父级 UserCenter shell 的 PageBackground 统一提供,此处不再叠加装饰 blob */}
{/* 主内容 - flex 列布局,数据分析区撑满剩余视口 */}
{/* Header - 更现代的设计 */}
{/* 动态图标容器 */}
{dashboardData.t('用量洞察中心')}
{/* LIVE 实时脉冲指示 */}
LIVE
{dashboardData.t('实时追踪您的每一次调用与消费')}
{/* 操作按钮组 */}
{/* 时间范围显示 */}
{dashboardData.dataExportDefaultTime}
{/* 刷新按钮 */}
}
onClick={initChart}
loading={dashboardData.loading || dashboardData.tokenUsageLoading}
>
{dashboardData.t('刷新')}
{/* 筛选按钮 */}
}
onClick={dashboardData.showSearchModal}
className='!rounded-xl !bg-gradient-to-r !from-brand-from !via-brand-via !to-brand-to !border-0 hover:!opacity-90 !transition-all !shadow-md !shadow-brand/30'
style={{ color: '#ffffff' }}
>
{dashboardData.t('筛选数据')}
{/* 详细统计卡片 —— 扁平化:去掉外层白 panel,实底卡直接浮在页面 bg-glow 上 */}
{/* 区块标题 */}
{dashboardData.t('核心指标')}
{/* 会员额度使用卡片 —— 扁平化:去掉外层白 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;