Files
monkey-ai-web/src/components/table/admin-tokens/AdminTokensColumnDefs.jsx
T
zhanjunhao 2aa9d73325 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

244 lines
5.7 KiB
React

import React from 'react';
import { Button, Switch, Tag as AntTag } from 'antd';
import { Tag, Typography, Popover } from '@/components/ui';
import {
renderGroup,
renderQuota,
isAdmin,
timestamp2string,
} from '../../../helpers';
const { Text } = Typography;
const statusMeta = {
3: { color: 'error', label: '已过期' },
4: { color: 'warning', label: '已耗尽' },
};
const renderStatus = (status, record, t, showEnableDisableModal) => {
const meta =
statusMeta[status] ||
(![1, 2].includes(status) ? { color: 'error', label: '未知状态' } : null);
return (
<div className='admin-tokens-status'>
<Switch
size='small'
checked={status === 1}
checkedChildren={t('启用')}
unCheckedChildren={t('禁用')}
aria-label={status === 1 ? t('禁用') : t('启用')}
disabled={!isAdmin() || Boolean(record.DeletedAt)}
onChange={() =>
showEnableDisableModal(record, status === 1 ? 'disable' : 'enable')
}
/>
{meta && (
<AntTag color={meta.color} variant='filled'>
{t(meta.label)}
</AntTag>
)}
</div>
);
};
/**
* 用户列:display_name 主显,user_id 与 username 小字辅显
*/
const renderUserCell = (text, record, t) => {
const displayName = record.display_name || record.username || t('未知用户');
return (
<div className='flex flex-col leading-tight'>
<Text strong className='!text-sm'>
{displayName}
</Text>
<div className='flex items-center gap-1 text-xs text-[var(--ui-color-text-2)]'>
<span>#{record.user_id}</span>
{record.username && record.username !== displayName && (
<span className='truncate max-w-[120px]'>@{record.username}</span>
)}
</div>
</div>
);
};
const renderKey = (text) => {
// 后端已脱敏,前端再加 sk- 前缀
return <code className='admin-tokens-key'>sk-{text}</code>;
};
const renderQuotaCell = (text, record, t) => {
if (record.unlimited_quota) {
return (
<AntTag color='success' variant='filled'>
{t('无限制')}
</AntTag>
);
}
const used = record.used_quota || 0;
const remain = record.remain_quota || 0;
const popoverContent = (
<div className='text-xs p-2'>
<div>
{t('已用')}: {renderQuota(used)}
</div>
<div>
{t('剩余')}: {renderQuota(remain)}
</div>
</div>
);
return (
<Popover content={popoverContent} position='top'>
<span>{renderQuota(remain)}</span>
</Popover>
);
};
const renderTime = (ts, t) => {
if (!ts || ts === 0)
return <span className='text-[var(--ui-color-text-2)]'>—</span>;
if (ts === -1) return <span>{t('永不过期')}</span>;
return <span>{timestamp2string(ts)}</span>;
};
const renderScope = (orgId, t) => {
if (!orgId || orgId === 0) {
return (
<Tag color='blue' shape='circle' size='small'>
{t('个人')}
</Tag>
);
}
return (
<Tag color='purple' shape='circle' size='small'>
{t('企业')} #{orgId}
</Tag>
);
};
const renderOperations = (
text,
record,
{ showEditTokenModal, showDeleteTokenModal, t },
) => {
if (record.DeletedAt) return <></>;
if (!isAdmin()) return null;
return (
<div className='admin-tokens-row-actions'>
<Button
size='small'
type='link'
onClick={() => showEditTokenModal(record)}
>
{t('编辑')}
</Button>
<Button
size='small'
type='link'
danger
onClick={() => showDeleteTokenModal(record)}
>
{t('删除')}
</Button>
</div>
);
};
export const getAdminTokensColumns = ({
t,
showEditTokenModal,
showEnableDisableModal,
showDeleteTokenModal,
}) => {
return [
{
title: 'ID',
dataIndex: 'id',
width: 70,
},
{
title: t('用户'),
dataIndex: 'user',
width: 180,
render: (text, record) => renderUserCell(text, record, t),
},
{
title: t('名称'),
dataIndex: 'name',
render: (text) => (
<span
className='admin-tokens-name truncate inline-block max-w-[160px]'
title={text}
>
{text || <Text type='tertiary'>—</Text>}
</span>
),
},
{
title: t('密钥'),
dataIndex: 'key',
render: renderKey,
},
{
title: t('状态'),
dataIndex: 'status',
width: 150,
render: (text, record) =>
renderStatus(text, record, t, showEnableDisableModal),
},
{
title: t('归属'),
dataIndex: 'organization_id',
width: 100,
render: (text) => renderScope(text, t),
},
{
title: t('分组'),
dataIndex: 'group',
width: 110,
render: (text) => <div>{renderGroup(text)}</div>,
},
{
title: t('剩余额度'),
dataIndex: 'remain_quota',
width: 110,
render: (text, record) => renderQuotaCell(text, record, t),
},
{
title: t('已用额度'),
dataIndex: 'used_quota',
width: 110,
render: (text) => <span>{renderQuota(text || 0)}</span>,
},
{
title: t('创建时间'),
dataIndex: 'created_time',
width: 160,
render: (text) => renderTime(text, t),
},
{
title: t('过期时间'),
dataIndex: 'expired_time',
width: 160,
render: (text) => renderTime(text, t),
},
{
title: t('最近访问'),
dataIndex: 'accessed_time',
width: 160,
render: (text) => renderTime(text, t),
},
{
title: t('操作'),
dataIndex: 'operate',
fixed: 'right',
width: 130,
render: (text, record) =>
renderOperations(text, record, {
showEditTokenModal,
showDeleteTokenModal,
t,
}),
},
];
};