fix(*): 全量 UI 审查与 undefined 展示异常修复
This commit is contained in:
+37
-27
@@ -30,6 +30,7 @@ import {
|
||||
showSuccess,
|
||||
showError,
|
||||
} from './utils';
|
||||
import { formatPercent, toFiniteNumber } from './safeDisplay.js';
|
||||
import {
|
||||
STORAGE_KEYS,
|
||||
DEFAULT_TIME_INTERVALS,
|
||||
@@ -217,37 +218,46 @@ export const renderMonitorList = (
|
||||
grouped[g].push(m);
|
||||
});
|
||||
|
||||
const renderItem = (monitor, idx) => (
|
||||
<div key={idx} className='p-2 hover:bg-white rounded-lg transition-colors'>
|
||||
<div className='flex items-center justify-between mb-1'>
|
||||
<div className='flex items-center gap-2'>
|
||||
<div
|
||||
className='w-2 h-2 rounded-full flex-shrink-0'
|
||||
style={{ backgroundColor: getUptimeStatusColor(monitor.status) }}
|
||||
/>
|
||||
<span className='text-sm font-medium text-gray-900'>
|
||||
{monitor.name}
|
||||
const renderItem = (monitor, idx) => {
|
||||
const uptime = Math.max(
|
||||
0,
|
||||
Math.min(1, toFiniteNumber(monitor.uptime) ?? 0),
|
||||
);
|
||||
return (
|
||||
<div
|
||||
key={idx}
|
||||
className='p-2 hover:bg-white rounded-lg transition-colors'
|
||||
>
|
||||
<div className='flex items-center justify-between mb-1'>
|
||||
<div className='flex items-center gap-2'>
|
||||
<div
|
||||
className='w-2 h-2 rounded-full flex-shrink-0'
|
||||
style={{ backgroundColor: getUptimeStatusColor(monitor.status) }}
|
||||
/>
|
||||
<span className='text-sm font-medium text-gray-900'>
|
||||
{monitor.name}
|
||||
</span>
|
||||
</div>
|
||||
<span className='text-xs text-gray-500'>
|
||||
{formatPercent(uptime * 100, 2)}
|
||||
</span>
|
||||
</div>
|
||||
<span className='text-xs text-gray-500'>
|
||||
{((monitor.uptime || 0) * 100).toFixed(2)}%
|
||||
</span>
|
||||
</div>
|
||||
<div className='flex items-center gap-2'>
|
||||
<span className='text-xs text-gray-500'>
|
||||
{getUptimeStatusText(monitor.status)}
|
||||
</span>
|
||||
<div className='flex-1'>
|
||||
<Progress
|
||||
percent={(monitor.uptime || 0) * 100}
|
||||
showInfo={false}
|
||||
aria-label={`${monitor.name} uptime`}
|
||||
stroke={getUptimeStatusColor(monitor.status)}
|
||||
/>
|
||||
<div className='flex items-center gap-2'>
|
||||
<span className='text-xs text-gray-500'>
|
||||
{getUptimeStatusText(monitor.status)}
|
||||
</span>
|
||||
<div className='flex-1'>
|
||||
<Progress
|
||||
percent={uptime * 100}
|
||||
showInfo={false}
|
||||
aria-label={`${monitor.name} uptime`}
|
||||
stroke={getUptimeStatusColor(monitor.status)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
);
|
||||
};
|
||||
|
||||
return Object.entries(grouped).map(([gname, list]) => (
|
||||
<div key={gname || 'default'} className='mb-2'>
|
||||
|
||||
+28
-29
@@ -56,6 +56,7 @@ import { Lock, Bot as FastGPT, Image as Jimeng } from 'lucide-react';
|
||||
import { Modal, Tag, Typography } from '@/components/ui';
|
||||
import { copy, getCurrencySymbol, showSuccess } from './utils';
|
||||
import { visit } from 'unist-util-visit';
|
||||
import { toFiniteNumber } from './safeDisplay';
|
||||
|
||||
// Full Avatar/Text/Combine variants are loaded only when configured icons render.
|
||||
const LobeHubIcon = lazy(() => import('../components/common/LobeHubIcon'));
|
||||
@@ -783,35 +784,30 @@ export function renderNumber(num) {
|
||||
}
|
||||
|
||||
export function getQuotaPerUnit() {
|
||||
let quotaPerUnit = localStorage.getItem('quota_per_unit');
|
||||
quotaPerUnit = parseFloat(quotaPerUnit);
|
||||
return quotaPerUnit;
|
||||
const quotaPerUnit = toFiniteNumber(localStorage.getItem('quota_per_unit'));
|
||||
return quotaPerUnit !== null && quotaPerUnit > 0 ? quotaPerUnit : 500000;
|
||||
}
|
||||
|
||||
export function renderUnitWithQuota(quota) {
|
||||
let quotaPerUnit = localStorage.getItem('quota_per_unit');
|
||||
quotaPerUnit = parseFloat(quotaPerUnit);
|
||||
quota = parseFloat(quota);
|
||||
return quotaPerUnit * quota;
|
||||
return getQuotaPerUnit() * (toFiniteNumber(quota) ?? 0);
|
||||
}
|
||||
|
||||
export function getQuotaWithUnit(quota, digits = 6) {
|
||||
let quotaPerUnit = localStorage.getItem('quota_per_unit');
|
||||
quotaPerUnit = parseFloat(quotaPerUnit);
|
||||
return (quota / quotaPerUnit).toFixed(digits);
|
||||
return ((toFiniteNumber(quota) ?? 0) / getQuotaPerUnit()).toFixed(digits);
|
||||
}
|
||||
|
||||
export function renderQuotaWithAmount(amount) {
|
||||
const normalizedAmount = toFiniteNumber(amount) ?? 0;
|
||||
const quotaDisplayType = localStorage.getItem('quota_display_type') || 'USD';
|
||||
if (quotaDisplayType === 'TOKENS') {
|
||||
return renderNumber(renderUnitWithQuota(amount));
|
||||
return renderNumber(renderUnitWithQuota(normalizedAmount));
|
||||
}
|
||||
if (quotaDisplayType === 'CNY') {
|
||||
return getCurrencySymbol('CNY') + amount;
|
||||
return getCurrencySymbol('CNY') + normalizedAmount;
|
||||
} else if (quotaDisplayType === 'CUSTOM') {
|
||||
return getCurrencySymbol('CUSTOM') + amount;
|
||||
return getCurrencySymbol('CUSTOM') + normalizedAmount;
|
||||
}
|
||||
return getCurrencySymbol('USD') + amount;
|
||||
return getCurrencySymbol('USD') + normalizedAmount;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -830,11 +826,8 @@ export function getCurrencyConfig() {
|
||||
try {
|
||||
if (statusStr) {
|
||||
const s = JSON.parse(statusStr);
|
||||
// 注意:如果设置了汇率值(包括1),使用设置值;只有未设置(undefined/null)时才用默认值1
|
||||
rate =
|
||||
s?.usd_exchange_rate !== undefined && s?.usd_exchange_rate !== null
|
||||
? s.usd_exchange_rate
|
||||
: 1;
|
||||
const parsedRate = toFiniteNumber(s?.usd_exchange_rate);
|
||||
rate = parsedRate !== null && parsedRate > 0 ? parsedRate : 1;
|
||||
}
|
||||
} catch (e) {}
|
||||
} else if (quotaDisplayType === 'CUSTOM') {
|
||||
@@ -842,7 +835,8 @@ export function getCurrencyConfig() {
|
||||
if (statusStr) {
|
||||
const s = JSON.parse(statusStr);
|
||||
symbol = getCurrencySymbol('CUSTOM', s?.custom_currency_symbol);
|
||||
rate = s?.custom_currency_exchange_rate || 1;
|
||||
const parsedRate = toFiniteNumber(s?.custom_currency_exchange_rate);
|
||||
rate = parsedRate !== null && parsedRate > 0 ? parsedRate : 1;
|
||||
}
|
||||
} catch (e) {
|
||||
symbol = getCurrencySymbol('CUSTOM');
|
||||
@@ -860,18 +854,18 @@ export function getCurrencyConfig() {
|
||||
*/
|
||||
export function convertUSDToCurrency(usdAmount, digits = 2) {
|
||||
const { symbol, rate } = getCurrencyConfig();
|
||||
const convertedAmount = usdAmount * rate;
|
||||
const convertedAmount = (toFiniteNumber(usdAmount) ?? 0) * rate;
|
||||
return symbol + convertedAmount.toFixed(digits);
|
||||
}
|
||||
|
||||
export function renderQuota(quota, digits = 2) {
|
||||
let quotaPerUnit = localStorage.getItem('quota_per_unit');
|
||||
const normalizedQuota = toFiniteNumber(quota) ?? 0;
|
||||
const quotaDisplayType = localStorage.getItem('quota_display_type') || 'USD';
|
||||
quotaPerUnit = parseFloat(quotaPerUnit);
|
||||
const quotaPerUnit = getQuotaPerUnit();
|
||||
if (quotaDisplayType === 'TOKENS') {
|
||||
return renderNumber(quota);
|
||||
return renderNumber(normalizedQuota);
|
||||
}
|
||||
const resultUSD = quota / quotaPerUnit;
|
||||
const resultUSD = normalizedQuota / quotaPerUnit;
|
||||
let symbol = getCurrencySymbol('USD');
|
||||
let value = resultUSD;
|
||||
if (quotaDisplayType === 'CNY') {
|
||||
@@ -880,7 +874,8 @@ export function renderQuota(quota, digits = 2) {
|
||||
try {
|
||||
if (statusStr) {
|
||||
const s = JSON.parse(statusStr);
|
||||
usdRate = s?.usd_exchange_rate || 1;
|
||||
const parsedRate = toFiniteNumber(s?.usd_exchange_rate);
|
||||
usdRate = parsedRate !== null && parsedRate > 0 ? parsedRate : 1;
|
||||
}
|
||||
} catch (e) {}
|
||||
value = resultUSD * usdRate;
|
||||
@@ -893,14 +888,15 @@ export function renderQuota(quota, digits = 2) {
|
||||
if (statusStr) {
|
||||
const s = JSON.parse(statusStr);
|
||||
symbolCustom = getCurrencySymbol('CUSTOM', s?.custom_currency_symbol);
|
||||
rate = s?.custom_currency_exchange_rate || rate;
|
||||
const parsedRate = toFiniteNumber(s?.custom_currency_exchange_rate);
|
||||
rate = parsedRate !== null && parsedRate > 0 ? parsedRate : 1;
|
||||
}
|
||||
} catch (e) {}
|
||||
value = resultUSD * rate;
|
||||
symbol = symbolCustom;
|
||||
}
|
||||
const fixedResult = value.toFixed(digits);
|
||||
if (parseFloat(fixedResult) === 0 && quota > 0 && value > 0) {
|
||||
if (parseFloat(fixedResult) === 0 && normalizedQuota > 0 && value > 0) {
|
||||
const minValue = Math.pow(10, -digits);
|
||||
return symbol + minValue.toFixed(digits);
|
||||
}
|
||||
@@ -947,7 +943,10 @@ export function renderExportQuota(quota, digits = 6) {
|
||||
if (!Number.isFinite(quotaPerUnit) || quotaPerUnit <= 0) {
|
||||
quotaPerUnit = 500000;
|
||||
}
|
||||
return getCurrencySymbol() + (quota / quotaPerUnit).toFixed(digits);
|
||||
return (
|
||||
getCurrencySymbol() +
|
||||
((toFiniteNumber(quota) ?? 0) / quotaPerUnit).toFixed(digits)
|
||||
);
|
||||
}
|
||||
|
||||
function isValidGroupRatio(ratio) {
|
||||
|
||||
@@ -18,7 +18,8 @@ For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
|
||||
export const toFiniteNumber = (value) => {
|
||||
if (value === null || value === undefined || value === '') return null;
|
||||
if (value === null || value === undefined) return null;
|
||||
if (typeof value === 'string' && value.trim() === '') return null;
|
||||
const number = Number(value);
|
||||
return Number.isFinite(number) ? number : null;
|
||||
};
|
||||
@@ -28,6 +29,21 @@ export const formatPercent = (value, digits = 1, fallback = '-') => {
|
||||
return number === null ? fallback : `${number.toFixed(digits)}%`;
|
||||
};
|
||||
|
||||
export const formatNumber = (value, digits = 2, fallback = '-') => {
|
||||
const number = toFiniteNumber(value);
|
||||
return number === null ? fallback : number.toFixed(digits);
|
||||
};
|
||||
|
||||
export const formatCurrency = (
|
||||
value,
|
||||
symbol = '¥',
|
||||
digits = 2,
|
||||
fallback = '-',
|
||||
) => {
|
||||
const formatted = formatNumber(value, digits, null);
|
||||
return formatted === null ? fallback : symbol + formatted;
|
||||
};
|
||||
|
||||
export const formatRatioPercent = (part, total, digits = 1, fallback = '-') => {
|
||||
const numerator = toFiniteNumber(part);
|
||||
const denominator = toFiniteNumber(total);
|
||||
|
||||
Reference in New Issue
Block a user