Statistic.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { useIntl } from "react-intl"
  2. import { formatNumber, percIncrease } from "../utils/numbers"
  3. import SkeletonText from "./SkeletonText"
  4. const Statistic: React.FC<{
  5. Icon?: (props: React.SVGProps<SVGElement>) => React.ReactElement
  6. label?: any
  7. currentValue?: number
  8. prevValue?: number
  9. }> = ({ Icon, label, currentValue, prevValue }) => {
  10. const intl = useIntl()
  11. const change = currentValue ? percIncrease(prevValue, currentValue) : 0
  12. return (
  13. <div className="flex items-center">
  14. <div className="relative flex h-12 w-12 shrink-0 items-center justify-center rounded-md bg-blurple-500 text-white">
  15. {Icon && <Icon className="h-5 w-5" />}
  16. </div>
  17. <div className="grow px-3">
  18. <span className="b3 !font-extrabold text-gray-1">
  19. {label || <SkeletonText className="w-[16ch]" />}
  20. </span>
  21. <span className="block">
  22. <span className="h6">
  23. {currentValue ? (
  24. formatNumber(currentValue, intl.locale)
  25. ) : (
  26. <SkeletonText className="w-[4ch]" />
  27. )}
  28. </span>
  29. {change > 0 && (
  30. <span className="b3 px-1 !font-extrabold text-nightshade-300">
  31. +{Math.round(change * 100)}%
  32. </span>
  33. )}
  34. </span>
  35. </div>
  36. </div>
  37. )
  38. }
  39. export default Statistic