Files
monkey-ai-web/src/components/analytics/OverviewCards.jsx
T
zhanjunhao 9760e28cb4 fix(*): 全量 UI 审查与 undefined 展示异常修复
分析模块:补齐看板、排行、趋势及报表数值兜底
企业与用户中心:修复统计、成员、技能及资金字段展示
充值与交易:修复充值、发票、订单、订阅及支付字段拼接
平台页面:修复渠道、模型、服务监控与状态空值展示
通用能力:新增安全展示工具,补齐九语言文案与回归测试
文档记录:更新修复范围、影响及回滚说明
2026-11-20 10:18:17 +08:00

171 lines
5.2 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 { formatNumber, formatPercent } from '../../helpers/safeDisplay';
import React from 'react';
import { Card } from '@/components/ui';
import {
Users,
UserPlus,
DollarSign,
Zap,
Activity,
Percent,
TrendingUp,
TrendingDown,
} from 'lucide-react';
import { renderQuota, timestamp2string } from '../../helpers';
const OverviewCards = ({ overview, loading = false, t }) => {
if (!overview) {
return (
<div className='grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4'>
{[...Array(6)].map((_, i) => (
<Card key={i} className='animate-pulse'>
<div className='h-20 bg-gray-200 dark:bg-gray-700 rounded'></div>
</Card>
))}
</div>
);
}
const cards = [
{
title: t('今日活跃'),
value: overview.dau?.toLocaleString() || '0',
change: overview.dau_change,
icon: <Users className='w-5 h-5' />,
color: 'blue',
bgColor: 'bg-blue-50 dark:bg-blue-900/20',
iconBg: 'bg-blue-500',
},
{
title: t('今日新增'),
value: overview.new_users?.toLocaleString() || '0',
change: overview.new_users_change,
icon: <UserPlus className='w-5 h-5' />,
color: 'green',
bgColor: 'bg-green-50 dark:bg-green-900/20',
iconBg: 'bg-green-500',
},
{
title: t('今日充值'),
value: `¥${formatNumber(overview.today_topup, 2, '0.00')}`,
change: overview.today_topup_change,
icon: <DollarSign className='w-5 h-5' />,
color: 'yellow',
bgColor: 'bg-yellow-50 dark:bg-yellow-900/20',
iconBg: 'bg-yellow-500',
},
{
title: t('今日消耗'),
value: renderQuota(overview.today_consume || 0),
change: overview.today_consume_change,
icon: <Zap className='w-5 h-5' />,
color: 'purple',
bgColor: 'bg-brand/5 dark:bg-brand-deep/20',
iconBg: 'bg-brand',
},
{
title: t('请求次数'),
value: overview.today_requests?.toLocaleString() || '0',
change: null,
icon: <Activity className='w-5 h-5' />,
color: 'cyan',
bgColor: 'bg-cyan-50 dark:bg-cyan-900/20',
iconBg: 'bg-cyan-500',
},
{
title: t('付费率'),
value: formatPercent(overview.pay_rate, 1, '0.0%'),
change: null,
icon: <Percent className='w-5 h-5' />,
color: 'pink',
bgColor: 'bg-pink-50 dark:bg-pink-900/20',
iconBg: 'bg-pink-500',
},
];
const renderChange = (change) => {
if (change === null || change === undefined || isNaN(change)) return null;
const isPositive = change >= 0;
return (
<div
className={`flex items-center gap-1 text-xs font-medium ${
isPositive ? 'text-green-500' : 'text-red-500'
}`}
>
{isPositive ? (
<TrendingUp className='w-3 h-3' />
) : (
<TrendingDown className='w-3 h-3' />
)}
<span>
{isPositive ? '+' : ''}
{formatPercent(change, 1)}
</span>
</div>
);
};
return (
<div>
<div
className={`grid grid-cols-2 md:grid-cols-3 lg:grid-cols-6 gap-4 transition-opacity ${
loading ? 'opacity-50' : ''
}`}
>
{cards.map((card, index) => (
<Card
key={index}
className={`${card.bgColor} border-none shadow-sm hover:shadow-md transition-shadow`}
styles={{ body: { padding: '16px' } }}
>
<div className='flex items-start justify-between'>
<div className='flex-1'>
<p className='text-xs text-gray-500 dark:text-gray-400 mb-1'>
{card.title}
</p>
<p className='text-xl font-bold text-gray-800 dark:text-gray-100'>
{card.value}
</p>
{renderChange(card.change)}
</div>
<div
className={`${card.iconBg} p-2 rounded-lg text-white shadow-sm`}
>
{card.icon}
</div>
</div>
</Card>
))}
</div>
{/* 活跃/消耗取自汇总层时会落后一个同步周期(约 30~60 秒),把截止时间标出来,
避免「数字和日志对不上」被当成算错;data_as_of 为 0 表示本次是实时直查。 */}
{overview.data_as_of > 0 && (
<p className='mt-2 text-xs text-gray-400 dark:text-gray-500 text-right'>
{t('活跃与消耗数据截至')} {timestamp2string(overview.data_as_of)}
</p>
)}
</div>
);
};
export default OverviewCards;