All files / src/shared/ProgressBar GiveProgressBar.tsx

88.88% Statements 16/18
90.9% Branches 30/33
100% Functions 3/3
88.88% Lines 16/18

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                                  63x 63x                     102x 102x   102x 51x   35x   16x       102x 51x   35x   12x           4x       102x                         102x                                                                                                                       63x            
import { useAppTheme } from "@theme/v2/Provider";
import {
  GiveProgressBarType,
  GiveProgressBarVariantType,
} from "./GiveProgressBar.types";
import { useMemo } from "react";
 
interface GiveProgressBarProps {
  value: number;
  innerValue?: number;
  variant: GiveProgressBarVariantType;
  type: GiveProgressBarType;
  showDot?: boolean;
  customColor?: string;
  height?: number;
}
 
const SIMPLE_TYPES = ["default", "onboarding", "kyb", "underwritingV2"];
const BAR_HEIGHT = 8;
 
export default function GiveProgressBar({
  value,
  variant,
  innerValue,
  type,
  showDot = true,
  customColor,
  height,
}: GiveProgressBarProps) {
  const { palette } = useAppTheme();
  const barHeight = height || BAR_HEIGHT;
 
  const completedBackground = useMemo(() => {
    switch (type) {
      case "default":
        return palette.primitive?.neutral[40];
      default:
        return palette.primitive?.success[50];
    }
  }, [type]);
 
  const incompleteStyle = useMemo(() => {
    switch (type) {
      case "default":
        return { backgroundColor: palette.primitive?.neutral[40] };
      case "onboarding":
        return { backgroundColor: palette.icon?.["icon-secondary"] };
      case "kyb":
        return { backgroundColor: palette.primitive?.["moon-purple"]?.[100] };
      case "underwritingV2":
        return { backgroundColor: palette.primitive?.["blue"]?.[100] };
      default:
        return { border: `1px solid ${palette.primitive?.success[50]}` };
    }
  }, [type]);
 
  const STYLES = {
    completed: {
      backgroundColor: completedBackground,
    },
    incomplete: incompleteStyle,
    disabled: {
      backgroundColor: palette.primitive?.transparent["darken-10"],
    },
    custom: {
      background: customColor || palette.primitive?.transparent["darken-10"],
    },
  };
 
  return (
    <div
      data-testid="give-progressbar"
      style={{
        padding: type === "underwriting" ? PADDING[variant] : 0,
        borderRadius: 4,
        backgroundColor:
          type === "underwriting"
            ? "transparent"
            : palette.primitive?.transparent["darken-10"],
        width: type === "underwriting" ? `${Math.floor(value)}%` : "100%",
        display: "flex",
        flexDirection: "row",
        alignItems: "center",
        justifyContent: "space-between",
        position: "relative",
        overflow: "hidden",
      }}
    >
      <div
        style={{
          borderRadius: 4,
          width: SIMPLE_TYPES.includes(type) ? `${Math.floor(value)}%` : "100%",
          height: barHeight,
          ...STYLES[variant],
        }}
      />
      {innerValue && innerValue !== 0 ? (
        <div
          style={{
            borderRadius: 4,
            width: type === "default" ? `${Math.floor(innerValue)}%` : "100%",
            height: barHeight,
            position: "absolute",
            backgroundColor:
              variant === "completed"
                ? palette.primitive?.success[50]
                : palette.icon?.["icon-primary"],
          }}
        />
      ) : null}
      {type === "default" && showDot && (
        <div
          style={{
            position: "absolute",
            backgroundColor:
              value < 80
                ? palette.primitive?.neutral[40]
                : palette.primitive?.neutral[0],
            width: "4px",
            height: "4px",
            borderRadius: "4px",
            right: "20%",
          }}
        />
      )}
    </div>
  );
}
 
const PADDING = {
  completed: "0px 2px 0px 0px",
  incomplete: "2px",
  disabled: "0px 0px 0px 2px",
  custom: "2px",
};