2020-07-30 05:09:41 +02:00
|
|
|
import { useSpring, animated } from 'react-spring';
|
2022-11-08 07:35:51 +01:00
|
|
|
import { formatNumber } from 'lib/format';
|
2020-07-29 04:04:45 +02:00
|
|
|
import styles from './MetricCard.module.css';
|
|
|
|
|
2021-08-13 01:01:51 +02:00
|
|
|
const MetricCard = ({
|
|
|
|
value = 0,
|
|
|
|
change = 0,
|
|
|
|
label,
|
|
|
|
reverseColors = false,
|
|
|
|
format = formatNumber,
|
2021-12-02 00:07:10 +01:00
|
|
|
hideComparison = false,
|
2021-08-13 01:01:51 +02:00
|
|
|
}) => {
|
2020-09-01 06:11:53 +02:00
|
|
|
const props = useSpring({ x: Number(value) || 0, from: { x: 0 } });
|
2021-08-13 01:01:51 +02:00
|
|
|
const changeProps = useSpring({ x: Number(change) || 0, from: { x: 0 } });
|
2020-07-30 05:09:41 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.card}>
|
|
|
|
<animated.div className={styles.value}>{props.x.interpolate(x => format(x))}</animated.div>
|
2021-08-13 01:01:51 +02:00
|
|
|
<div className={styles.label}>
|
|
|
|
{label}
|
2021-12-02 00:07:10 +01:00
|
|
|
{~~change !== 0 && !hideComparison && (
|
2021-08-13 01:01:51 +02:00
|
|
|
<animated.span
|
|
|
|
className={`${styles.change} ${
|
|
|
|
change >= 0
|
|
|
|
? !reverseColors
|
|
|
|
? styles.positive
|
|
|
|
: styles.negative
|
|
|
|
: !reverseColors
|
|
|
|
? styles.negative
|
|
|
|
: styles.positive
|
2022-08-26 16:40:32 +02:00
|
|
|
} ${change >= 0 ? styles.plusSign : ''}`}
|
2021-08-13 01:01:51 +02:00
|
|
|
>
|
2022-08-26 16:40:32 +02:00
|
|
|
{changeProps.x.interpolate(x => format(x))}
|
2021-08-13 01:01:51 +02:00
|
|
|
</animated.span>
|
|
|
|
)}
|
|
|
|
</div>
|
2020-07-30 05:09:41 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2020-07-29 04:04:45 +02:00
|
|
|
|
|
|
|
export default MetricCard;
|