/* 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 { formatPercent } from '../../helpers/safeDisplay'; 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 ( {icon} {title} ) } styles={{ body: { padding: '16px' } }} >
{t('暂无数据')}
); } // 计算最大值用于进度条 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 ( {globalIndex + 1} ); }, }, { title: t('渠道名称'), dataIndex: 'channel_name', key: 'channel_name', render: (text) => ( {text || t('未知渠道')} ), }, { title: t('消耗额度'), dataIndex: 'quota', key: 'quota', width: 180, render: (val, record) => (
{renderQuota(val || 0)}
), }, { title: t('请求次数'), dataIndex: 'count', key: 'count', width: 100, align: 'right', render: (val) => ( {(val || 0).toLocaleString()} ), }, { title: t('占比'), dataIndex: 'percent', key: 'percent', width: 80, align: 'right', render: (val) => ( {formatPercent(val || 0, 1)} ), }, ]; return ( {icon} {title} {I18N_TECHNICAL_TEXT.TOP} {data.length} ) } styles={{ body: { padding: '0' } }} > ({ ...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' /> ); }; export default ChannelConsumption;