All files / src/shared/UnderwritingScore CircularScore.tsx

90.9% Statements 20/22
82.14% Branches 23/28
100% Functions 7/7
100% Lines 19/19

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 107 108 109 110 111 112 113 114 115 116 117 118                                                  24x 24x   24x 24x 24x   24x 23x   11x     1x     24x 24x                   24x         59x                             24x                               33x             12x                         33x 129x 24x              
import { BoxProps } from "@mui/material";
import { CircularProgress } from "@mui/material";
import { Box } from "@mui/material";
import { GiveProgressBarType } from "@shared/ProgressBar/GiveProgressBar.types";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { useMemo } from "react";
interface Props {
  handleClick?: () => void;
  disabled?: boolean;
  score: number;
  pendingScore?: number;
  simple?: boolean;
  defaultThreshold?: number;
  type?: GiveProgressBarType;
}
 
export default function CircularScore({
  handleClick,
  disabled,
  score,
  pendingScore,
  simple = false,
  defaultThreshold = 80,
  type,
}: Props) {
  const { palette } = useAppTheme();
  const isLowScore = !score || score < defaultThreshold;
 
  const activeBackground = useMemo(() => {
    Iif (type === "kyb") return palette.primitive?.["moon-purple"]?.[100];
    Iif (type === "underwritingV2") return palette.primitive?.["blue"]?.[100];
 
    if (isLowScore) {
      if (simple) return palette.icon?.["icon-secondary"];
 
      return palette.icon?.["icon-primary"];
    }
 
    return palette.primitive?.success[50];
  }, [type, simple, isLowScore]);
 
  const colors = useMemo(() => {
    return {
      background: palette.surface?.tertiary,
      active: activeBackground,
      circle: isLowScore
        ? palette.icon?.["icon-secondary"]
        : palette.icon?.["remain-light"],
      pending: isLowScore ? palette.primitive?.neutral[40] : "transparent",
    };
  }, [score, activeBackground]);
 
  const renderCircularProgress = (
    value: number | undefined,
    color: string | undefined,
    zIndex: number,
  ) => (
    <CircularProgress
      variant="determinate"
      value={value}
      size={24}
      thickness={7}
      sx={{
        color,
        zIndex,
        position: "absolute",
        "& .MuiCircularProgress-circle": {
          strokeLinecap: "round",
        },
      }}
    />
  );
  return (
    <CircleContainer
      data-testid="circular-score-container"
      onClick={handleClick}
      disabled={disabled}
    >
      {renderCircularProgress(100, colors.background, 1)}
      {renderCircularProgress(score, colors.active, 2)}
      {!!pendingScore &&
        pendingScore > 0 &&
        renderCircularProgress(score + pendingScore, colors.pending, 1)}
      {!simple && <ProgressCircle bg={colors.circle} top="4px" zIndex={2} />}
    </CircleContainer>
  );
}
 
const ProgressCircle = styled(Box)<
  BoxProps & {
    bg: string | undefined;
    right?: string;
    top?: string;
    zIndex?: number;
  }
>(({ bg, top = "11px", zIndex }) => ({
  width: "3px",
  height: "3px",
  borderRadius: "50%",
  background: bg || "#E9E9E9",
  position: "absolute",
 
  top,
  ...(zIndex && { zIndex }),
  maxWidth: "100%",
  right: "calc(100% - 6px)",
}));
 
const CircleContainer = styled(Box, {
  shouldForwardProp: (prop) => prop !== "disabled",
})<{ disabled?: boolean }>(({ disabled }) => ({
  position: "relative",
  width: "24px",
  height: "24px",
  paddingTop: "1px",
  ...(disabled && { pointerEvents: "none" }),
}));