import React from 'react';
import { Progress, Tooltip } from 'antd';
import { typeColors, getUsedValueColor } from './Ring';
export default function Line(props) {
const {
used = 0,
add = 0,
reserved = 0,
limit = 1,
title = '',
secondTitle = t('Quota'),
} = props;
const isLimit = limit !== -1;
const limitStr = !isLimit ? t('Infinity') : limit;
let left = isLimit ? limit - used - reserved - add : 1;
left = left < 0 ? 0 : left;
const usedTip = `${t('Used')}: ${used}`;
const reservedTip = reserved ? `${t('Reserved')}: ${reserved}` : '';
const newTip = `${t('New')}: ${add}`;
const leftTip = `${t('Available')}: ${left}`;
const tips = isLimit ? [usedTip, newTip, leftTip] : [usedTip, newTip];
if (reserved) {
tips.splice(1, 0, reservedTip);
}
const tipTitle = tips.join(' / ');
const allCount = used + reserved + add;
const isOverLimit = isLimit && allCount > limit;
const allPercent = parseInt((allCount / limit) * 100, 10);
const usedPercent = parseInt(((used + reserved) / limit) * 100, 10);
const usedColor = getUsedValueColor(allPercent);
const resourceTitle = (
<span>
{`${title} ${secondTitle}: `}{' '}
<span style={{ color: usedColor }}>{`${allCount}/${limitStr}`}</span>
</span>
);
const progress = isLimit ? (
<Progress
percent={allPercent}
success={{
percent: usedPercent,
strokeColor: typeColors.used,
}}
strokeColor={isOverLimit ? typeColors.error : typeColors.add}
showInfo={false}
/>
) : (
<Progress percent={0} showInfo={false} />
);
return (
<div style={{ width: 150 }}>
<div style={{ fontWeight: 'bold', textAlign: 'center' }}>
{resourceTitle}
</div>
<Tooltip title={tipTitle}>{progress}</Tooltip>
</div>
);
}