2023-03-10 04:42:55 +00:00
|
|
|
import classNames from 'classnames';
|
2020-07-30 03:09:41 +00:00
|
|
|
import { useSpring, animated } from 'react-spring';
|
2022-11-08 06:35:51 +00:00
|
|
|
import { formatNumber } from 'lib/format';
|
2020-07-29 02:04:45 +00:00
|
|
|
import styles from './MetricCard.module.css';
|
|
|
|
|
|
2021-08-12 23:01:51 +00:00
|
|
|
const MetricCard = ({
|
|
|
|
|
value = 0,
|
|
|
|
|
change = 0,
|
|
|
|
|
label,
|
|
|
|
|
reverseColors = false,
|
|
|
|
|
format = formatNumber,
|
2021-12-01 23:07:10 +00:00
|
|
|
hideComparison = false,
|
2023-04-10 03:22:28 +00:00
|
|
|
className,
|
2021-08-12 23:01:51 +00:00
|
|
|
}) => {
|
2020-09-01 04:11:53 +00:00
|
|
|
const props = useSpring({ x: Number(value) || 0, from: { x: 0 } });
|
2021-08-12 23:01:51 +00:00
|
|
|
const changeProps = useSpring({ x: Number(change) || 0, from: { x: 0 } });
|
2020-07-30 03:09:41 +00:00
|
|
|
|
|
|
|
|
return (
|
2023-04-10 03:22:28 +00:00
|
|
|
<div className={classNames(styles.card, className)}>
|
2023-03-09 23:18:54 +00:00
|
|
|
<animated.div className={styles.value}>{props.x.to(x => format(x))}</animated.div>
|
2021-08-12 23:01:51 +00:00
|
|
|
<div className={styles.label}>
|
|
|
|
|
{label}
|
2021-12-01 23:07:10 +00:00
|
|
|
{~~change !== 0 && !hideComparison && (
|
2021-08-12 23:01:51 +00:00
|
|
|
<animated.span
|
2023-03-10 04:42:55 +00:00
|
|
|
className={classNames(styles.change, {
|
|
|
|
|
[styles.positive]: change * (reverseColors ? -1 : 1) >= 0,
|
|
|
|
|
[styles.negative]: change * (reverseColors ? -1 : 1) < 0,
|
|
|
|
|
[styles.plusSign]: change > 0,
|
|
|
|
|
})}
|
2021-08-12 23:01:51 +00:00
|
|
|
>
|
2023-03-09 23:18:54 +00:00
|
|
|
{changeProps.x.to(x => format(x))}
|
2021-08-12 23:01:51 +00:00
|
|
|
</animated.span>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2020-07-30 03:09:41 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
2020-07-29 02:04:45 +00:00
|
|
|
|
|
|
|
|
export default MetricCard;
|