All files / src/components/common Figure.tsx

88.88% Statements 16/18
33.33% Branches 6/18
77.77% Functions 7/9
88.23% Lines 15/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106              13x   13x 26x   26x 13x     13x     26x                           13x   13x                       3x                                                 13x 34x   3x                   13x 13x                                      
import { Stack, styled } from "@mui/material";
import { Text } from "@common/Text";
import { palette } from "@palette";
import { ITextProps } from "@common/Text/Text";
import { memo, useEffect, useState } from "react";
import { parseAmount } from "@utils/index";
 
export const DELAYED_RENDER = 1;
 
export const Delayed = ({ children, waitBeforeShow = DELAYED_RENDER }: any) => {
  const [isShown, setIsShown] = useState(false);
 
  useEffect(() => {
    const timer = setTimeout(() => {
      setIsShown(true);
    }, waitBeforeShow);
    return () => clearTimeout(timer);
  }, [waitBeforeShow]);
 
  return isShown ? children : null;
};
 
export type FigureProps = ITextProps & {
  sup?: boolean;
  value: number | string;
  fontSize?: number | string;
  color?: string;
  isAmount?: boolean;
  percent?: boolean;
  isText?: boolean;
  ratio?: number | string | undefined;
  ratioStyle?: any;
};
export const DECIMAL_SIZE_RATIO = 0.75;
 
const Figure = ({
  sup,
  value,
  fontSize = 32,
  isAmount,
  color = palette.neutral[750],
  percent,
  fontWeight = "light",
  ratio,
  ratioStyle,
  ...props
}: FigureProps) => {
  return (
    <Stack direction="row" alignItems="flex-end" gap={1}>
      <ValueText
        color={color}
        fontSize={fontSize}
        fontWeight={fontWeight}
        {...props}
      >
        {isAmount || percent ? parseAmount(value) : value}
      </ValueText>
      {ratio && (
        <RatioText
          color={color}
          paddingBottom="4px"
          fontSize={ratioStyle ? ratioStyle.fontSize : "18px"}
          fontWeight={ratioStyle ? ratioStyle.fontWeight : 400}
          lineHeight={ratioStyle ? ratioStyle.lineHeight : "100%"}
        >
          {`(${ratio}%)`}
        </RatioText>
      )}
    </Stack>
  );
};
 
const ValueText = styled(Text, {
  shouldForwardProp: (prop) => prop !== "color",
})<ITextProps & { fontSize: number | string; color: string }>(
  ({ fontSize, color }) => ({
    flexGrow: 1,
    fontSize,
    background: color,
    WebkitBackgroundClip: "text",
    WebkitTextFillColor: "transparent",
    lineHeight: "100%",
  }),
);
 
const RatioText = styled(Text, {
  shouldForwardProp: (prop) => prop !== "color",
})<
  ITextProps & {
    color: string;
    fontSize: string | number;
    fontWeight: number | string;
    lineHeight: string;
  }
>(({ color, fontSize, fontWeight, lineHeight }) => ({
  flexGrow: 1,
  fontSize: fontSize,
  fontWeight: fontWeight,
  lineHeight: lineHeight,
  background: color,
  WebkitBackgroundClip: "text",
  WebkitTextFillColor: "transparent",
}));
 
export default memo(Figure);