* Fix EmojiFormatter failure * Add notification_emails.warning setting default value * Fix list spec failure and add antennas for spec response * Fix domain block spec failure to add kb custom response * Fix SearchQueryTransformer spec failure * Fix Account#matches_display_name spec failure * Fix UpdateStatusService changes mentions spec failure * Fix RuboCop Lint * Ignore brakeman warning * Fix CI failure for ignore brakeman warning * Fix migration failure * Fix README * Fix migration CI failure * Fix some spec failure * Format code for RuboCop lint failure * Fix ESlint failure * Fix haml-lint failure
96 lines
2.2 KiB
TypeScript
96 lines
2.2 KiB
TypeScript
import { memo } from 'react';
|
|
|
|
import { FormattedMessage, FormattedNumber } from 'react-intl';
|
|
|
|
import { toShortNumber, pluralReady, DECIMAL_UNITS } from '../utils/numbers';
|
|
|
|
type ShortNumberRenderer = (
|
|
displayNumber: JSX.Element,
|
|
pluralReady: number,
|
|
) => JSX.Element;
|
|
|
|
interface ShortNumberProps {
|
|
value: number;
|
|
isHide: boolean;
|
|
renderer?: ShortNumberRenderer;
|
|
children?: ShortNumberRenderer;
|
|
}
|
|
|
|
export const ShortNumberRenderer: React.FC<ShortNumberProps> = ({
|
|
value,
|
|
isHide,
|
|
renderer,
|
|
children,
|
|
}) => {
|
|
const shortNumber = toShortNumber(value);
|
|
const [, division] = shortNumber;
|
|
|
|
if (children && renderer) {
|
|
console.warn(
|
|
'Both renderer prop and renderer as a child provided. This is a mistake and you really should fix that. Only renderer passed as a child will be used.',
|
|
);
|
|
}
|
|
|
|
const customRenderer = children ?? renderer ?? null;
|
|
|
|
const displayNumber = !isHide ? (
|
|
<ShortNumberCounter value={shortNumber} />
|
|
) : (
|
|
<span>-</span>
|
|
);
|
|
|
|
return (
|
|
customRenderer?.(displayNumber, pluralReady(value, division)) ??
|
|
displayNumber
|
|
);
|
|
};
|
|
export const ShortNumber = memo(ShortNumberRenderer);
|
|
|
|
interface ShortNumberCounterProps {
|
|
value: number[];
|
|
}
|
|
const ShortNumberCounter: React.FC<ShortNumberCounterProps> = ({ value }) => {
|
|
const [rawNumber, unit, maxFractionDigits = 0] = value;
|
|
|
|
const count = (
|
|
<FormattedNumber
|
|
value={rawNumber}
|
|
maximumFractionDigits={maxFractionDigits}
|
|
/>
|
|
);
|
|
|
|
const values = { count, rawNumber };
|
|
|
|
switch (unit) {
|
|
case DECIMAL_UNITS.THOUSAND: {
|
|
return (
|
|
<FormattedMessage
|
|
id='units.short.thousand'
|
|
defaultMessage='{count}K'
|
|
values={values}
|
|
/>
|
|
);
|
|
}
|
|
case DECIMAL_UNITS.MILLION: {
|
|
return (
|
|
<FormattedMessage
|
|
id='units.short.million'
|
|
defaultMessage='{count}M'
|
|
values={values}
|
|
/>
|
|
);
|
|
}
|
|
case DECIMAL_UNITS.BILLION: {
|
|
return (
|
|
<FormattedMessage
|
|
id='units.short.billion'
|
|
defaultMessage='{count}B'
|
|
values={values}
|
|
/>
|
|
);
|
|
}
|
|
// Not sure if we should go farther - @Sasha-Sorokin
|
|
default:
|
|
return count;
|
|
}
|
|
};
|