Files
monkey-ai-web/src/components/table/model-pricing/market/MarketModelCard.jsx
T

209 lines
6.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 { Copy as ActionCopy } from '@/components/common/icons/ActionIcons';
import React from 'react';
import { Button, Card, Tag, Tooltip } from 'antd';
import { Lock } from 'lucide-react';
import { getDiscountBadgeText } from '../../../../helpers';
import MarketLogo from './MarketLogo';
/** 计费类型文案:与详情「分组价格」的判定顺序一致 */
export const getBillingLabel = (priceData, t) => {
if (priceData?.isPerSecond) return t('按秒计费');
if (priceData?.isPerImageResolution) return t('按分辨率计费');
if (priceData?.isPerToken) return t('按量计费');
return t('按次计费');
};
/**
* 把 calculateModelPrice 的结果整理为原型卡片的价格格:主价格(含官方价删除线)与附加价格行。
* 只做展示映射,不重新计算价格。
*/
const buildCardPrices = (priceData, t) => {
if (priceData.isPerToken) {
return {
tier: false,
main: [
{
key: 'input',
label: t('输入'),
value: priceData.inputPrice,
official: priceData.official?.inputPrice,
},
{
key: 'output',
label: t('输出'),
value: priceData.completionPrice,
official: priceData.official?.completionPrice,
},
],
extras: (priceData.extraPrices || []).map((item) => ({
key: item.key,
label: t(item.labelKey),
value: item.price,
})),
};
}
if (priceData.isPerSecond || priceData.isPerImageResolution) {
const tiers = priceData.isPerSecond
? priceData.videoTiers || []
: priceData.imageTiers || [];
const unit = priceData.isPerSecond ? t('秒') : t('张');
return {
tier: true,
main: tiers.length
? tiers.map((tier) => ({
key: tier.key,
label: `${tier.label} / ${unit}`,
value: tier.price,
}))
: [{ key: 'none', label: getBillingLabel(priceData, t), value: '-' }],
note: getBillingLabel(priceData, t),
};
}
return {
tier: false,
main: [
{
key: 'price',
label: `${t('单价')} / ${t('次')}`,
value: priceData.price,
official: priceData.official?.price,
},
],
note: getBillingLabel(priceData, t),
};
};
const trackPointer = (e) => {
const rect = e.currentTarget.getBoundingClientRect();
e.currentTarget.style.setProperty(
'--pointer-x',
`${e.clientX - rect.left}px`,
);
e.currentTarget.style.setProperty('--pointer-y', `${e.clientY - rect.top}px`);
};
const MarketModelCard = ({
model,
priceData,
hidePrice,
onClick,
onCopy,
t,
}) => {
const discountText = getDiscountBadgeText(priceData);
const prices = hidePrice ? null : buildCardPrices(priceData, t);
const handleKeyDown = (e) => {
if (e.target !== e.currentTarget) return;
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
onClick();
}
};
return (
<Card
className='mm-card'
role='button'
tabIndex={0}
aria-label={t('查看 {{name}} 详情', { name: model.model_name })}
onClick={onClick}
onKeyDown={handleKeyDown}
onPointerMove={trackPointer}
>
<div className='mm-card-head'>
<MarketLogo
icon={model.icon || model.vendor_icon}
fallbackText={model.model_name}
/>
<h3 title={model.model_name}>{model.model_name}</h3>
{model.level_locked && (
<Tooltip title={t('当前等级无权调用此模型,升级等级后可用')}>
<Tag icon={<Lock size={11} />} bordered={false}>
{t('仅可见')}
</Tag>
</Tooltip>
)}
<Tooltip title={t('复制模型名称')}>
<Button
type='text'
aria-label={t('复制模型名称')}
icon={<ActionCopy size={16} strokeWidth={1.65} />}
onClick={(e) => {
e.stopPropagation();
onCopy(model.model_name);
}}
/>
</Tooltip>
</div>
{prices && (
<>
<div className={`mm-prices${prices.tier ? ' mm-tier-prices' : ''}`}>
{prices.main.map((item) => (
<div key={item.key}>
<span>{item.label}</span>
<strong>{item.value}</strong>
{item.official && <del>{item.official}</del>}
</div>
))}
</div>
<div className='mm-cache'>
{prices.extras?.map((item) => (
<span key={item.key}>
{item.label} <b>{item.value}</b>
</span>
))}
{prices.note && <span>{prices.note}</span>}
</div>
<div className='mm-card-foot'>
{priceData?.usedGroup ? (
<span
title={t('该价格按 {{group}} 分组倍率计算', {
group: priceData.usedGroup,
})}
>
{priceData.usedGroup}
</span>
) : (
<span />
)}
{discountText ? (
<Tooltip
title={t(
'相比官方牌价的折扣,按当前计价分组实付价计算(已含您的等级折扣),仅供参考',
)}
>
<Tag bordered={false}>{discountText}</Tag>
</Tooltip>
) : (
<span>{getBillingLabel(priceData, t)}</span>
)}
</div>
</>
)}
</Card>
);
};
export default React.memo(MarketModelCard);