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

138 lines
4.3 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import React, { useEffect, useRef, useState } from 'react';
import { InputNumber, withField } from '@/components/ui';
import { useTranslation } from 'react-i18next';
import { getQuotaPerUnit } from '../../../helpers';
/**
* 金额额度输入组件
*
* 对外的 value/onChange 始终使用系统内部额度单位(token,与后端一致),
* UI 上则以人民币金额输入/展示(quotaPerUnit 额度 = ¥1,默认 500000),
* 后端无需任何改动。
*
* quota_display_type = TOKENS 时退化为原始额度输入,行为与旧版一致。
*
* @param {number|string|null} value 内部额度值(token 单位)
* @param {(quota:number|null)=>void} onChange 变更回调,参数为换算后的内部额度值;清空时为 null
* @param {number} [minQuota] 最小值(token 单位)
* @param {number} [maxQuota] 最大值(token 单位)
* @param {string} [placeholder] 占位文案,缺省根据货币模式自动生成
* 其余属性透传给 Semi InputNumber(size/style/disabled/prefix 等)
*/
const FALLBACK_QUOTA_PER_UNIT = 500000;
const CURRENCY_SYMBOL = '¥';
function getSafeQuotaPerUnit() {
const qpu = getQuotaPerUnit();
return Number.isFinite(qpu) && qpu > 0 ? qpu : FALLBACK_QUOTA_PER_UNIT;
}
function isTokensMode() {
return (localStorage.getItem('quota_display_type') || 'USD') === 'TOKENS';
}
/** 内部额度 → 显示金额(人民币元),保留 6 位小数避免浮点噪声 */
export function quotaToMoney(quota) {
const n = typeof quota === 'string' ? parseFloat(quota) : quota;
if (n === null || n === undefined || !Number.isFinite(n)) return null;
return Number((n / getSafeQuotaPerUnit()).toFixed(6));
}
/** 显示金额(人民币元)→ 内部额度(取整,支持负数) */
export function moneyToQuota(money) {
const n = typeof money === 'string' ? parseFloat(money) : money;
if (n === null || n === undefined || !Number.isFinite(n)) return null;
return Math.round(n * getSafeQuotaPerUnit());
}
const QuotaInput = ({
value,
onChange,
minQuota,
maxQuota,
placeholder,
...rest
}) => {
const { t } = useTranslation();
const tokensMode = isTokensMode();
const toDisplay = (q) => {
if (q === null || q === undefined || q === '') return null;
if (tokensMode) {
const n = typeof q === 'string' ? parseFloat(q) : q;
return Number.isFinite(n) ? n : null;
}
return quotaToMoney(q);
};
const [innerValue, setInnerValue] = useState(() => toDisplay(value));
// 记录组件自己最近一次上抛的额度值:外部值变化若并非来自本组件输入,
// 才回写显示值,避免用户输入"1."之类中间态被换算打断
const lastEmittedRef = useRef(
value === null || value === undefined || value === ''
? null
: Number(value),
);
useEffect(() => {
const normalized =
value === null || value === undefined || value === ''
? null
: Number(value);
if (normalized !== lastEmittedRef.current) {
lastEmittedRef.current = normalized;
setInnerValue(toDisplay(value));
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value]);
const handleChange = (v) => {
setInnerValue(v);
const n = typeof v === 'string' ? parseFloat(v) : v;
let quota;
if (n === null || n === undefined || !Number.isFinite(n)) {
quota = null;
} else {
quota = tokensMode ? Math.round(n) : moneyToQuota(n);
}
lastEmittedRef.current = quota;
onChange?.(quota);
};
const min =
minQuota === undefined
? undefined
: tokensMode
? minQuota
: quotaToMoney(minQuota);
const max =
maxQuota === undefined
? undefined
: tokensMode
? maxQuota
: quotaToMoney(maxQuota);
return (
<InputNumber
value={innerValue}
onChange={handleChange}
min={min}
max={max}
prefix={tokensMode ? undefined : CURRENCY_SYMBOL}
placeholder={
placeholder ?? (tokensMode ? t('请输入额度') : t('请输入金额'))
}
{...rest}
/>
);
};
/** Semi Form 版本:表单值仍为内部额度单位,可直接配合 field/rules 使用 */
export const FormQuotaInput = withField(QuotaInput, {
valueKey: 'value',
onKeyChangeFnName: 'onChange',
});
export default QuotaInput;