447 lines
21 KiB
React
447 lines
21 KiB
React
/*
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
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 (
|
|
<div className='p-4 md:p-5 h-full overflow-y-auto relative'>
|
|
{/* 公告弹窗 */}
|
|
<NoticeModal
|
|
visible={noticeVisible}
|
|
onClose={() => setNoticeVisible(false)}
|
|
isMobile={isMobile}
|
|
/>
|
|
|
|
{/* 页面背景由父级 UserCenter shell 的 PageBackground 统一提供,此处不再叠加装饰 blob */}
|
|
|
|
{/* 主内容 - flex 列布局,数据分析区撑满剩余视口 */}
|
|
<div className='relative z-10 flex flex-col min-h-full'>
|
|
{/* Header - 更现代的设计 */}
|
|
<div className='flex flex-col lg:flex-row justify-between items-start lg:items-center gap-3 mb-5'>
|
|
<div className='flex items-center gap-4'>
|
|
{/* 动态图标容器 */}
|
|
<div className='relative group'>
|
|
<div className='absolute inset-0 bg-gradient-to-br from-brand-from via-brand-via to-brand-to rounded-2xl blur-lg opacity-50 group-hover:opacity-70 transition-opacity' />
|
|
<div className='relative p-3 bg-gradient-to-br from-brand-from via-brand-via to-brand-to rounded-2xl shadow-lg shadow-brand/25'>
|
|
<BarChart2 size={22} className='text-white' />
|
|
<Sparkles
|
|
size={11}
|
|
className='absolute -top-1 -right-1 text-amber-300 animate-pulse'
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div className='flex items-center gap-2'>
|
|
<h1 className='text-xl md:text-2xl font-bold bg-clip-text text-transparent bg-gradient-to-r from-brand-deep via-brand to-brand-via dark:from-brand-from dark:via-brand-via dark:to-brand-to tracking-tight'>
|
|
{dashboardData.t('用量洞察中心')}
|
|
</h1>
|
|
{/* LIVE 实时脉冲指示 */}
|
|
<span className='inline-flex items-center gap-1 px-1.5 py-0.5 rounded-md bg-emerald-50 dark:bg-emerald-950/40 border border-emerald-200/60 dark:border-emerald-800/40'>
|
|
<span className='relative flex h-1.5 w-1.5'>
|
|
<span className='absolute inline-flex h-full w-full rounded-full bg-emerald-400 opacity-75 animate-ping' />
|
|
<span className='relative inline-flex h-1.5 w-1.5 rounded-full bg-emerald-500' />
|
|
</span>
|
|
<span className='text-[10px] font-bold text-emerald-700 dark:text-emerald-300 tracking-wider'>
|
|
LIVE
|
|
</span>
|
|
</span>
|
|
</div>
|
|
<p className='text-xs md:text-sm text-gray-500 dark:text-gray-400 mt-0.5 flex items-center gap-1.5'>
|
|
<Activity size={12} className='text-brand' />
|
|
{dashboardData.t('实时追踪您的每一次调用与消费')}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
|
|
{/* 操作按钮组 */}
|
|
<div className='flex items-center gap-3'>
|
|
{/* 时间范围显示 */}
|
|
<div className='hidden md:flex items-center gap-2 px-4 py-2 bg-white/60 dark:bg-neutral-800/70 backdrop-blur-sm rounded-xl border border-gray-200/50 dark:border-neutral-600/50'>
|
|
<Calendar size={14} className='text-gray-400' />
|
|
<span className='text-sm text-gray-600 dark:text-gray-300'>
|
|
{dashboardData.dataExportDefaultTime}
|
|
</span>
|
|
</div>
|
|
|
|
{/* 刷新按钮 */}
|
|
<Button
|
|
icon={<IconRefresh />}
|
|
onClick={initChart}
|
|
loading={dashboardData.loading || dashboardData.tokenUsageLoading}
|
|
>
|
|
{dashboardData.t('刷新')}
|
|
</Button>
|
|
|
|
{/* 筛选按钮 */}
|
|
<Button
|
|
type='tertiary'
|
|
icon={<IconFilter style={{ color: '#ffffff' }} />}
|
|
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' }}
|
|
>
|
|
<span className='hidden sm:inline ml-1'>
|
|
{dashboardData.t('筛选数据')}
|
|
</span>
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
<SearchModal
|
|
searchModalVisible={dashboardData.searchModalVisible}
|
|
handleSearchConfirm={handleSearchConfirm}
|
|
handleCloseModal={dashboardData.handleCloseModal}
|
|
isMobile={dashboardData.isMobile}
|
|
isAdminUser={dashboardData.isAdminUser}
|
|
inputs={dashboardData.inputs}
|
|
dataExportDefaultTime={dashboardData.dataExportDefaultTime}
|
|
timeOptions={dashboardData.timeOptions}
|
|
handleInputChange={dashboardData.handleInputChange}
|
|
t={dashboardData.t}
|
|
/>
|
|
|
|
{/* 详细统计卡片 —— 扁平化:去掉外层白 panel,实底卡直接浮在页面 bg-glow 上 */}
|
|
<div className='mb-5'>
|
|
{/* 区块标题 */}
|
|
<div className='flex items-center gap-2 mb-3'>
|
|
<div className='w-1 h-4 bg-gradient-to-b from-brand-from via-brand-via to-brand-to rounded-full' />
|
|
<h2 className='text-base font-semibold text-gray-900 dark:text-white tracking-wide'>
|
|
{dashboardData.t('核心指标')}
|
|
</h2>
|
|
</div>
|
|
<StatsCards
|
|
groupedStatsData={groupedStatsData}
|
|
loading={dashboardData.loading || dashboardData.tokenUsageLoading}
|
|
getTrendSpec={getTrendSpec}
|
|
CARD_PROPS={CARD_PROPS}
|
|
CHART_CONFIG={CHART_CONFIG}
|
|
/>
|
|
</div>
|
|
|
|
{/* 会员额度使用卡片 —— 扁平化:去掉外层白 panel,实底卡浮在 bg-glow 上 */}
|
|
{!membershipLoading && membershipInfo && (
|
|
<div className='mb-5'>
|
|
{/* 区块标题 */}
|
|
<div className='flex items-center justify-between mb-3'>
|
|
<div className='flex items-center gap-2'>
|
|
<div className='w-1 h-4 bg-gradient-to-b from-brand-from via-brand-via to-brand-to rounded-full' />
|
|
<h2 className='text-base font-semibold text-gray-900 dark:text-white tracking-wide'>
|
|
{dashboardData.t('会员权益')}
|
|
</h2>
|
|
<span className='text-xs text-gray-400 bg-gray-100 dark:bg-neutral-800 px-2 py-0.5 rounded-full'>
|
|
{membershipInfo.plan_name}
|
|
</span>
|
|
</div>
|
|
<div className='flex items-center gap-1.5 text-xs text-gray-400'>
|
|
<Clock size={12} />
|
|
<span>
|
|
{dashboardData.t('剩余')} {membershipInfo.remaining_days}{' '}
|
|
{dashboardData.t('天')}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* 卡片网格 */}
|
|
<div className='grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3'>
|
|
{/* 今日可用额度 —— 青 */}
|
|
<div className='group/m relative overflow-hidden rounded-xl bg-white dark:bg-neutral-900/90 border border-gray-100 dark:border-neutral-800 shadow-[0_2px_12px_-2px_rgba(0,0,0,0.06)] dark:shadow-[0_2px_12px_-2px_rgba(0,0,0,0.4)] p-3 hover:shadow-[0_12px_28px_-6px_rgba(0,0,0,0.12)] hover:-translate-y-0.5 transition-all duration-300 ease-out'>
|
|
<div className='pointer-events-none absolute top-0 left-0 right-0 h-px bg-gradient-to-r from-cyan-400 to-sky-500 opacity-70' />
|
|
<div className='absolute -top-6 -right-6 w-20 h-20 bg-gradient-to-br from-cyan-400/25 to-sky-300/5 rounded-full blur-2xl group-hover/m:scale-110 transition-transform duration-500' />
|
|
<div className='relative'>
|
|
<div className='flex items-center gap-2 mb-2'>
|
|
<div className='w-8 h-8 rounded-lg bg-gradient-to-br from-cyan-500 to-sky-600 flex items-center justify-center shadow-md shadow-cyan-500/25'>
|
|
<Sparkles size={14} className='text-white' />
|
|
</div>
|
|
<span className='text-xs text-gray-500 dark:text-gray-400'>
|
|
{dashboardData.t('今日剩余')}
|
|
</span>
|
|
</div>
|
|
<div className='text-xl font-bold text-gray-900 dark:text-white mb-2'>
|
|
{renderQuota(membershipInfo.membership_quota)}
|
|
</div>
|
|
{membershipInfo.daily_quota > 0 && (
|
|
<Progress
|
|
percent={Math.min(
|
|
100,
|
|
Math.max(
|
|
0,
|
|
Math.round(
|
|
(membershipInfo.membership_quota /
|
|
membershipInfo.daily_quota) *
|
|
100,
|
|
),
|
|
),
|
|
)}
|
|
showInfo={false}
|
|
stroke='#06b6d4'
|
|
style={{ height: 4 }}
|
|
/>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* 今日已用额度 —— 天蓝 */}
|
|
<div className='group/m relative overflow-hidden rounded-xl bg-white dark:bg-neutral-900/90 border border-gray-100 dark:border-neutral-800 shadow-[0_2px_12px_-2px_rgba(0,0,0,0.06)] dark:shadow-[0_2px_12px_-2px_rgba(0,0,0,0.4)] p-3 hover:shadow-[0_12px_28px_-6px_rgba(0,0,0,0.12)] hover:-translate-y-0.5 transition-all duration-300 ease-out'>
|
|
<div className='pointer-events-none absolute top-0 left-0 right-0 h-px bg-gradient-to-r from-sky-400 to-blue-500 opacity-70' />
|
|
<div className='absolute -top-6 -right-6 w-20 h-20 bg-gradient-to-br from-sky-400/25 to-blue-300/5 rounded-full blur-2xl group-hover/m:scale-110 transition-transform duration-500' />
|
|
<div className='relative'>
|
|
<div className='flex items-center gap-2 mb-2'>
|
|
<div className='w-8 h-8 rounded-lg bg-gradient-to-br from-sky-500 to-blue-600 flex items-center justify-center shadow-md shadow-sky-500/25'>
|
|
<Zap size={14} className='text-white' />
|
|
</div>
|
|
<span className='text-xs text-gray-500 dark:text-gray-400'>
|
|
{dashboardData.t('今日消耗')}
|
|
</span>
|
|
</div>
|
|
<div className='text-xl font-bold text-gray-900 dark:text-white mb-2'>
|
|
{renderQuota(
|
|
Math.max(
|
|
0,
|
|
membershipInfo.daily_quota -
|
|
membershipInfo.membership_quota,
|
|
),
|
|
)}
|
|
</div>
|
|
{membershipInfo.daily_quota > 0 && (
|
|
<div className='text-xs text-gray-400'>
|
|
{dashboardData.t('使用率')}{' '}
|
|
{Math.round(
|
|
Math.max(
|
|
0,
|
|
(membershipInfo.daily_quota -
|
|
membershipInfo.membership_quota) /
|
|
membershipInfo.daily_quota,
|
|
) * 100,
|
|
)}
|
|
%
|
|
</div>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* 每日额度上限 —— 蓝 */}
|
|
<div className='group/m relative overflow-hidden rounded-xl bg-white dark:bg-neutral-900/90 border border-gray-100 dark:border-neutral-800 shadow-[0_2px_12px_-2px_rgba(0,0,0,0.06)] dark:shadow-[0_2px_12px_-2px_rgba(0,0,0,0.4)] p-3 hover:shadow-[0_12px_28px_-6px_rgba(0,0,0,0.12)] hover:-translate-y-0.5 transition-all duration-300 ease-out'>
|
|
<div className='pointer-events-none absolute top-0 left-0 right-0 h-px bg-gradient-to-r from-blue-400 to-indigo-500 opacity-70' />
|
|
<div className='absolute -top-6 -right-6 w-20 h-20 bg-gradient-to-br from-blue-400/25 to-indigo-300/5 rounded-full blur-2xl group-hover/m:scale-110 transition-transform duration-500' />
|
|
<div className='relative'>
|
|
<div className='flex items-center gap-2 mb-2'>
|
|
<div className='w-8 h-8 rounded-lg bg-gradient-to-br from-blue-500 to-indigo-600 flex items-center justify-center shadow-md shadow-blue-500/25'>
|
|
<Crown size={14} className='text-white' />
|
|
</div>
|
|
<span className='text-xs text-gray-500 dark:text-gray-400'>
|
|
{dashboardData.t('每日总额')}
|
|
</span>
|
|
</div>
|
|
<div className='text-xl font-bold text-gray-900 dark:text-white mb-2'>
|
|
{renderQuota(membershipInfo.daily_quota)}
|
|
</div>
|
|
<div className='text-xs text-gray-400'>
|
|
{dashboardData.t('每日自动重置')}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* 累计已用额度 —— 靛 */}
|
|
<div className='group/m relative overflow-hidden rounded-xl bg-white dark:bg-neutral-900/90 border border-gray-100 dark:border-neutral-800 shadow-[0_2px_12px_-2px_rgba(0,0,0,0.06)] dark:shadow-[0_2px_12px_-2px_rgba(0,0,0,0.4)] p-3 hover:shadow-[0_12px_28px_-6px_rgba(0,0,0,0.12)] hover:-translate-y-0.5 transition-all duration-300 ease-out'>
|
|
<div className='pointer-events-none absolute top-0 left-0 right-0 h-px bg-gradient-to-r from-indigo-400 to-violet-500 opacity-70' />
|
|
<div className='absolute -top-6 -right-6 w-20 h-20 bg-gradient-to-br from-indigo-400/25 to-violet-300/5 rounded-full blur-2xl group-hover/m:scale-110 transition-transform duration-500' />
|
|
<div className='relative'>
|
|
<div className='flex items-center gap-2 mb-2'>
|
|
<div className='w-8 h-8 rounded-lg bg-gradient-to-br from-indigo-500 to-violet-600 flex items-center justify-center shadow-md shadow-indigo-500/25'>
|
|
<TrendingUp size={14} className='text-white' />
|
|
</div>
|
|
<span className='text-xs text-gray-500 dark:text-gray-400'>
|
|
{dashboardData.t('累计消耗')}
|
|
</span>
|
|
</div>
|
|
<div className='text-xl font-bold text-gray-900 dark:text-white mb-2'>
|
|
{renderQuota(membershipInfo.membership_used_quota || 0)}
|
|
</div>
|
|
<div className='text-xs text-gray-400'>
|
|
{dashboardData.t('会员期间总消耗')}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* 模型数据分析图表 - 自适应填满剩余视口高度。扁平化:去外层白 panel,图卡自身实底 */}
|
|
<div className='flex-1 flex flex-col min-h-[420px]'>
|
|
{/* 区块标题 */}
|
|
<div className='flex items-center gap-2 mb-3 shrink-0'>
|
|
<div className='w-1 h-4 bg-gradient-to-b from-brand-from via-brand-via to-brand-to rounded-full' />
|
|
<h2 className='text-base font-semibold text-gray-900 dark:text-white tracking-wide'>
|
|
{dashboardData.t('用量洞察')}
|
|
</h2>
|
|
</div>
|
|
<ChartsPanel
|
|
spec_line={dashboardCharts.spec_line}
|
|
spec_pie={dashboardCharts.spec_pie}
|
|
CHART_CONFIG={CHART_CONFIG}
|
|
hasApiInfoPanel={false}
|
|
t={dashboardData.t}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default UserDashboard;
|