2023-03-10 04:42:55 +00:00
|
|
|
import classNames from 'classnames';
|
2023-10-12 07:03:10 +00:00
|
|
|
import { useSpring, animated } from '@react-spring/web';
|
2022-11-08 06:35:51 +00:00
|
|
|
import { formatNumber } from 'lib/format';
|
2024-05-27 00:26:15 +00:00
|
|
|
import ChangeLabel from 'components/metrics/ChangeLabel';
|
2020-07-29 02:04:45 +00:00
|
|
|
import styles from './MetricCard.module.css';
|
|
|
|
|
|
2023-12-03 11:07:03 +00:00
|
|
|
export interface MetricCardProps {
|
|
|
|
|
value: number;
|
2024-05-23 07:17:20 +00:00
|
|
|
previousValue?: number;
|
2023-12-03 11:07:03 +00:00
|
|
|
change?: number;
|
2024-05-23 07:17:20 +00:00
|
|
|
label?: string;
|
2023-12-03 11:07:03 +00:00
|
|
|
reverseColors?: boolean;
|
2024-05-27 04:30:03 +00:00
|
|
|
formatValue?: typeof formatNumber;
|
2024-05-23 07:17:20 +00:00
|
|
|
showLabel?: boolean;
|
|
|
|
|
showChange?: boolean;
|
|
|
|
|
showPrevious?: boolean;
|
2023-12-03 11:07:03 +00:00
|
|
|
className?: string;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-21 15:00:42 +00:00
|
|
|
export const MetricCard = ({
|
2021-08-12 23:01:51 +00:00
|
|
|
value = 0,
|
|
|
|
|
change = 0,
|
|
|
|
|
label,
|
|
|
|
|
reverseColors = false,
|
2024-05-27 04:30:03 +00:00
|
|
|
formatValue = formatNumber,
|
2024-05-23 07:17:20 +00:00
|
|
|
showLabel = true,
|
2024-05-23 07:58:31 +00:00
|
|
|
showChange = false,
|
2024-05-23 07:17:20 +00:00
|
|
|
showPrevious = false,
|
2023-04-10 03:22:28 +00:00
|
|
|
className,
|
2023-12-03 11:07:03 +00:00
|
|
|
}: MetricCardProps) => {
|
2024-05-27 04:30:03 +00:00
|
|
|
const diff = value - change;
|
|
|
|
|
const pct = ((value - diff) / diff) * 100;
|
2020-09-01 04:11:53 +00:00
|
|
|
const props = useSpring({ x: Number(value) || 0, from: { x: 0 } });
|
2024-05-27 04:30:03 +00:00
|
|
|
const changeProps = useSpring({ x: Number(pct) || 0, from: { x: 0 } });
|
|
|
|
|
const prevProps = useSpring({ x: Number(diff) || 0, from: { x: 0 } });
|
2020-07-30 03:09:41 +00:00
|
|
|
|
|
|
|
|
return (
|
2024-05-23 07:17:20 +00:00
|
|
|
<div className={classNames(styles.card, className, showPrevious && styles.compare)}>
|
|
|
|
|
{showLabel && <div className={styles.label}>{label}</div>}
|
2024-06-21 06:26:36 +00:00
|
|
|
<animated.div className={styles.value} title={value?.toString()}>
|
2024-05-27 04:30:03 +00:00
|
|
|
{props?.x?.to(x => formatValue(x))}
|
2024-05-22 04:15:31 +00:00
|
|
|
</animated.div>
|
2024-05-23 07:17:20 +00:00
|
|
|
{showChange && (
|
2024-05-28 04:23:06 +00:00
|
|
|
<ChangeLabel
|
|
|
|
|
className={styles.change}
|
|
|
|
|
value={change}
|
|
|
|
|
title={formatValue(change)}
|
|
|
|
|
reverseColors={reverseColors}
|
|
|
|
|
>
|
|
|
|
|
<animated.span>{changeProps?.x?.to(x => `${Math.abs(~~x)}%`)}</animated.span>
|
2024-05-27 00:26:15 +00:00
|
|
|
</ChangeLabel>
|
2024-05-23 07:17:20 +00:00
|
|
|
)}
|
|
|
|
|
{showPrevious && (
|
2024-05-28 04:23:06 +00:00
|
|
|
<animated.div className={classNames(styles.value, styles.prev)} title={diff.toString()}>
|
2024-05-27 04:30:03 +00:00
|
|
|
{prevProps?.x?.to(x => formatValue(x))}
|
2024-05-23 07:17:20 +00:00
|
|
|
</animated.div>
|
|
|
|
|
)}
|
2020-07-30 03:09:41 +00:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
2020-07-29 02:04:45 +00:00
|
|
|
|
|
|
|
|
export default MetricCard;
|