Files
monkey-ai-web/src/components/analytics/ChannelConsumption.jsx
T
zhanjunhao 67969a041d fix(i18n): 补齐九种语言的翻译并修复国际化检查
补齐页面缺译,统一动态文案的整句插值方式。
2026-11-17 14:10:25 +08:00

168 lines
4.8 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 { I18N_TECHNICAL_TEXT } from '../../i18n/technicalText.js';
import React, { useState } from 'react';
import { Card, Table, Progress } from '@/components/ui';
import { renderQuota } from '../../helpers';
const ChannelConsumption = ({ data, title, icon, t, embedded = false }) => {
const [currentPage, setCurrentPage] = useState(1);
const [pageSize, setPageSize] = useState(10);
if (!data || data.length === 0) {
return (
<Card
className={`shadow-sm hover:shadow-md transition-shadow ${embedded ? 'analytics-fold-content' : ''}`}
title={
embedded ? undefined : (
<div className='flex items-center gap-2'>
<span className='text-brand'>{icon}</span>
<span className='font-semibold'>{title}</span>
</div>
)
}
styles={{ body: { padding: '16px' } }}
>
<div className='h-[300px] flex items-center justify-center text-gray-400'>
{t('暂无数据')}
</div>
</Card>
);
}
// 计算最大值用于进度条
const maxQuota = Math.max(...data.map((item) => item.quota || 0));
// 计算全局序号
const getGlobalIndex = (pageIndex) =>
(currentPage - 1) * pageSize + pageIndex;
const columns = [
{
title: '#',
dataIndex: 'index',
key: 'index',
width: 50,
render: (_, record, index) => {
const globalIndex = getGlobalIndex(index);
return (
<span
className={`font-medium ${globalIndex < 3 ? 'text-orange-500' : 'text-gray-500'}`}
>
{globalIndex + 1}
</span>
);
},
},
{
title: t('渠道名称'),
dataIndex: 'channel_name',
key: 'channel_name',
render: (text) => (
<span className='font-medium truncate' title={text}>
{text || t('未知渠道')}
</span>
),
},
{
title: t('消耗额度'),
dataIndex: 'quota',
key: 'quota',
width: 180,
render: (val, record) => (
<div className='flex items-center gap-2'>
<Progress
percent={(val / maxQuota) * 100}
showInfo={false}
size='small'
style={{ width: 80, height: 6 }}
stroke='#3b82f6'
/>
<span className='text-sm font-medium text-gray-700 dark:text-gray-300'>
{renderQuota(val || 0)}
</span>
</div>
),
},
{
title: t('请求次数'),
dataIndex: 'count',
key: 'count',
width: 100,
align: 'right',
render: (val) => (
<span className='text-gray-600'>{(val || 0).toLocaleString()}</span>
),
},
{
title: t('占比'),
dataIndex: 'percent',
key: 'percent',
width: 80,
align: 'right',
render: (val) => (
<span className='text-gray-500'>{(val || 0).toFixed(1)}%</span>
),
},
];
return (
<Card
className={`shadow-sm hover:shadow-md transition-shadow ${embedded ? 'analytics-fold-content' : ''}`}
title={
embedded ? undefined : (
<div className='flex items-center gap-2'>
<span className='text-brand'>{icon}</span>
<span className='font-semibold'>{title}</span>
<span className='text-xs text-gray-400 ml-2'>
{I18N_TECHNICAL_TEXT.TOP} {data.length}
</span>
</div>
)
}
styles={{ body: { padding: '0' } }}
>
<Table
columns={columns}
dataSource={data.map((record, index) => ({
...record,
key: `${record.channel_name}-${index}`,
}))}
pagination={{
currentPage,
pageSize,
showSizeChanger: true,
pageSizeOpts: [10, 20, 30],
showTotal: (total) => t('共 {{total}} 条', { total, count: total }),
onPageChange: (page) => setCurrentPage(page),
onPageSizeChange: (size) => {
setPageSize(size);
setCurrentPage(1);
},
}}
size='small'
rowKey='key'
/>
</Card>
);
};
export default ChannelConsumption;