161 lines
4.9 KiB
React
161 lines
4.9 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 from 'react';
|
||
import { Card, Table } from '@/components/ui';
|
||
import { renderQuota } from '../../helpers';
|
||
|
||
const UserTopupRanking = ({ data, title, icon, t, embedded = false }) => {
|
||
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 dataWithRank = data.map((item, idx) => ({ ...item, _rank: idx + 1 }));
|
||
|
||
// 排名样式
|
||
const getRankBadge = (rank) => {
|
||
if (rank === 1)
|
||
return 'bg-gradient-to-r from-yellow-400 to-yellow-500 text-white';
|
||
if (rank === 2)
|
||
return 'bg-gradient-to-r from-gray-300 to-gray-400 text-white';
|
||
if (rank === 3)
|
||
return 'bg-gradient-to-r from-orange-400 to-orange-500 text-white';
|
||
return 'bg-gray-200 text-gray-600';
|
||
};
|
||
|
||
const columns = [
|
||
{
|
||
title: t('排名'),
|
||
dataIndex: '_rank',
|
||
width: 70,
|
||
render: (rank) => (
|
||
<div
|
||
className={`w-7 h-7 rounded-full flex items-center justify-center text-xs font-bold ${getRankBadge(rank)}`}
|
||
>
|
||
{rank}
|
||
</div>
|
||
),
|
||
},
|
||
{
|
||
title: t('用户昵称'),
|
||
dataIndex: 'display_name',
|
||
width: 160,
|
||
render: (text, record) => (
|
||
<div className='flex flex-col'>
|
||
<span
|
||
className='font-medium text-sm truncate'
|
||
title={text || `User ${record.user_id}`}
|
||
>
|
||
{text || `User ${record.user_id}`}
|
||
</span>
|
||
<span className='text-xs text-gray-400 truncate'>
|
||
ID: {record.user_id}
|
||
</span>
|
||
</div>
|
||
),
|
||
},
|
||
{
|
||
title: t('充值总额'),
|
||
dataIndex: 'total_topup',
|
||
width: 130,
|
||
sorter: (a, b) => a.total_topup - b.total_topup,
|
||
defaultSortOrder: 'descend',
|
||
render: (value) => (
|
||
<span className='font-semibold text-red-500'>
|
||
¥{(value || 0).toFixed(2)}
|
||
</span>
|
||
),
|
||
},
|
||
{
|
||
title: t('历史消耗'),
|
||
dataIndex: 'used_quota',
|
||
width: 130,
|
||
sorter: (a, b) => a.used_quota - b.used_quota,
|
||
render: (value) => (
|
||
<span className='text-orange-600 dark:text-orange-400'>
|
||
{renderQuota(value || 0)}
|
||
</span>
|
||
),
|
||
},
|
||
{
|
||
title: t('当前余额'),
|
||
dataIndex: 'balance',
|
||
width: 130,
|
||
sorter: (a, b) => a.balance - b.balance,
|
||
render: (value) => (
|
||
<span className='font-medium text-green-600 dark:text-green-400'>
|
||
{renderQuota(value || 0)}
|
||
</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'>
|
||
{t('共')} {data.length} {t('人')}
|
||
</span>
|
||
</div>
|
||
)
|
||
}
|
||
styles={{ body: { padding: '0' } }}
|
||
>
|
||
<Table
|
||
// 列宽合计远超窄屏视口;没有 scroll 时表格会压缩到 min-content 硬塞进去,
|
||
// 末尾的操作列落在视口外,且 iOS 上滚动条不可见、毫无可拖动提示
|
||
scroll={{ x: 'max-content' }}
|
||
columns={columns}
|
||
dataSource={dataWithRank}
|
||
rowKey='user_id'
|
||
pagination={
|
||
data.length > 20 ? { pageSize: 20, showSizeChanger: true } : false
|
||
}
|
||
size='small'
|
||
bordered={false}
|
||
style={{ borderRadius: 0 }}
|
||
/>
|
||
</Card>
|
||
);
|
||
};
|
||
|
||
export default UserTopupRanking;
|