All files / src/features/TransactionPanel/components RiskLevelProgress.tsx

78.57% Statements 11/14
57.69% Branches 15/26
100% Functions 3/3
78.57% Lines 11/14

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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                            36x 36x   36x   108x         36x 36x   36x                                                     36x 36x   36x   36x                                                                                                                                                            
import { Stack, Box } from "@mui/material";
import {
  CaretRightIcon,
  ShieldCheckIcon,
  ShieldWarningIcon,
} from "@phosphor-icons/react";
import GiveText from "@shared/Text/GiveText";
import { useAppTheme } from "@theme/v2/Provider";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { RiskLevelProgressProps } from "../utils";
import { RiskLevelIndicator, RiskProgressBars } from "./RiskLevel.atoms";
 
export default function RiskLevelProgress({
  level,
  total = 3,
  width = "100%",
  showArrow = true,
  onClick,
  showLabel,
  sx,
  counter,
}: RiskLevelProgressProps) {
  const { palette } = useAppTheme();
  const { isMobileView } = useCustomThemeV2();
 
  const segments = Array(total)
    .fill(0)
    .map((_, index) => ({
      active: index < level,
      id: index + 1,
    }));
 
  const getRiskColor = (level: number) => {
    switch (level) {
      case 0:
        return {
          primary: palette.primitive?.success[100],
          bg: palette.primitive?.success[25],
          textColor: "success1",
        };
      case 1:
        return {
          primary: palette.primitive?.warning[50],
          bg: palette.primitive?.warning[25],
          textColor: "warning1",
        };
      case 2:
        return {
          primary: palette.primitive?.warning[100],
          bg: palette.primitive?.warning[25],
          textColor: "warning",
        };
      case 3:
      default:
        return {
          primary: palette.primitive?.error[50],
          bg: palette.primitive?.error[25],
          textColor: "error1",
        };
    }
  };
 
  const riskColors = getRiskColor(level);
  const ShieldIcon = level === 0 ? ShieldCheckIcon : ShieldWarningIcon;
 
  const shouldShowLabel = !isMobileView || showLabel;
 
  return (
    <>
      <Stack
        data-testid="risk-level-progress"
        direction="row"
        alignItems="center"
        justifyContent="space-between"
        width={width}
        sx={{
          paddingY: "16px",
          cursor: onClick ? "pointer" : "default",
          ...sx,
        }}
        onClick={onClick}
      >
        {shouldShowLabel && (
          <Stack
            direction="row"
            alignItems="center"
            gap={1}
            flexGrow={1}
            flex={1}
          >
            <ShieldIcon
              size={18}
              color={riskColors.primary}
              data-testid="risk-level-icon"
            />
            <GiveText
              variant="bodyS"
              color={riskColors.textColor as any}
              data-testid="risk-level-text"
            >
              Risk Level
            </GiveText>
          </Stack>
        )}
 
        <Stack
          direction="row"
          alignItems="center"
          gap="20px"
          flex={1}
          minWidth={shouldShowLabel ? "50%" : "100%"}
          paddingLeft={shouldShowLabel ? "16px" : "0"}
        >
          <RiskLevelIndicator
            level={level}
            bgColor={riskColors.bg}
            textColor={riskColors.textColor}
          />
          <RiskProgressBars
            segments={segments}
            activeColor={riskColors.primary}
          />
          {showArrow && (
            <CaretRightIcon
              size={18}
              color={palette.icon?.["icon-secondary"]}
              data-testid="risk-level-icon"
            />
          )}
        </Stack>
      </Stack>
      {counter && (
        <Box
          display="flex"
          justifyContent="flex-end"
          width="100%"
          mt={1}
          pl={shouldShowLabel ? 2 : 0}
        >
          <Box width="50%">{counter}</Box>
        </Box>
      )}
    </>
  );
}