import { useSpring } from '@react-spring/web'; import { Column, Text } from '@umami/react-zen'; import { AnimatedDiv } from '@/components/common/AnimatedDiv'; import { ChangeLabel } from '@/components/metrics/ChangeLabel'; import { formatNumber } from '@/lib/format'; export interface MetricCardProps { value: number; previousValue?: number; change?: number; label?: string; reverseColors?: boolean; formatValue?: (n: any) => string; showLabel?: boolean; showChange?: boolean; } export const MetricCard = ({ value = 0, change = 0, label, reverseColors = false, formatValue = formatNumber, showLabel = true, showChange = false, }: MetricCardProps) => { const diff = value - change; const pct = diff !== 0 ? ((value - diff) / diff) * 100 : value !== 0 ? 100 : 0; const props = useSpring({ x: Number(value) || 0, from: { x: 0 } }); const changeProps = useSpring({ x: Number(pct) || 0, from: { x: 0 } }); return ( {showLabel && ( {label} )} {props?.x?.to(x => formatValue(x))} {showChange && ( {changeProps?.x?.to(x => `${Math.abs(~~x)}%`)} )} ); };