All files / src/shared/GiveAlert GiveAlert.tsx

100% Statements 11/11
100% Branches 26/26
100% Functions 6/6
100% Lines 11/11

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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206                                                                                                      711x                                                                         108x   3663x               711x   711x                                                                                         108x 2184x             519x   519x                           108x 1230x                                                                      
import { Stack, SxProps, StackProps, Box, BoxProps } from "@mui/material";
import GiveButton from "@shared/Button/GiveButton";
import GiveText from "@shared/Text/GiveText";
import { CustomPalette } from "@theme/v2/palette.interface";
import { styled } from "@theme/v2/Provider";
import React from "react";
 
type TAlertType =
  | "default"
  | "blue"
  | "warning"
  | "warning2"
  | "error"
  | "success";
type TAlertVariant = "alert" | "notice";
 
type AlertStyleType = {
  type: TAlertType;
  withBorder?: boolean;
};
 
interface AlertProps extends AlertStyleType {
  Icon?: React.ReactElement;
  label?: string;
  description?: string;
  variant?: TAlertVariant;
  sx?: SxProps;
  iconContainerSx?: SxProps;
  textContainerSx?: SxProps;
  action?: {
    label?: string;
    onClick?: () => void;
  };
  dataTestId?: string;
  capitalize?: boolean;
}
 
export default function GiveAlert({
  type,
  withBorder = false,
  Icon,
  label,
  sx,
  description,
  variant = "notice",
  action,
  iconContainerSx,
  textContainerSx,
  dataTestId,
  capitalize,
}: AlertProps) {
  return (
    <AlertContainer
      variant={variant}
      type={type}
      withBorder={withBorder}
      sx={sx}
    >
      {Icon && (
        <IconContainer type={type} variant={variant} sx={iconContainerSx}>
          {Icon}
        </IconContainer>
      )}
      <Stack gap={1} sx={textContainerSx} data-testid={dataTestId}>
        {label && (
          <GiveText
            variant="bodyS"
            fontWeight={variant === "alert" ? "medium" : "notice"}
            sx={{
              textTransform: capitalize ? "capitalize" : "none",
            }}
          >
            {label}
          </GiveText>
        )}
        {description && <GiveText variant="bodyS">{description}</GiveText>}
        {action?.label && action?.onClick && (
          <GiveButton
            variant="ghost"
            label={action?.label}
            onClick={action?.onClick}
          />
        )}
      </Stack>
    </AlertContainer>
  );
}
 
const AlertContainer = styled(Stack, {
  shouldForwardProp: (prop) =>
    prop !== "type" && prop !== "withBorder" && prop !== "variant",
})<
  StackProps & {
    type: TAlertType;
    withBorder?: boolean;
    variant?: TAlertVariant;
  }
>(({ theme, type, withBorder, variant }) => {
  const { BORDER, BACKGORUND, COLOR, BUTTON } = getColors(theme.palette);
 
  return {
    padding: "12px",
    gap: "12px",
    display: "flex",
    flexDirection: "row",
    alignItems: "center",
    borderRadius: "8px",
    backgroundColor: BACKGORUND[type],
    "& svg": {
      color: COLOR[type],
      height: "24px !important",
      width: "24px !important",
      minWidth: "24px",
    },
    "& p": {
      color: COLOR[type],
    },
    ...(withBorder && {
      border: `1.5px solid ${BORDER[type]}`,
    }),
 
    ...(variant === "alert" && {
      gap: "20px",
      alignItems: "flex-start",
      maxHeight: "none",
      padding: "20px",
      borderRadius: "12px",
      "& svg": {
        color: COLOR[type],
        height: "20px",
        width: "20px",
      },
      "& p": {
        color: theme.palette?.text?.primary,
      },
    }),
 
    "& button": {
      width: "fit-content",
      padding: 0,
      color: BUTTON[type],
    },
  };
});
 
const IconContainer = styled(Box, {
  shouldForwardProp: (prop) => prop !== "type" && prop !== "variant",
})<
  BoxProps & {
    type: TAlertType;
    variant?: TAlertVariant;
  }
>(({ variant, type, theme }) => {
  const { BORDER } = getColors(theme.palette);
 
  return {
    ...(variant === "alert" && {
      borderRadius: "50px",
      border: `1.5px solid ${BORDER[type]}`,
      display: "flex",
      justifyContent: "center",
      alignItems: "center",
      height: "30px",
      width: "30px",
      minWidth: "30px",
    }),
  };
});
 
const getColors = (palette: CustomPalette) => {
  return {
    BORDER: {
      default: palette.primitive?.neutral[50],
      blue: palette.primitive?.blue[100],
      success: palette.primitive?.success[100],
      warning: palette.primitive?.warning[50],
      warning2: palette.primitive?.warning[100],
      error: palette.primitive?.error[100],
    },
    BACKGORUND: {
      default: palette.primitive?.transparent["darken-5"],
      blue: palette.primitive?.blue[10],
      success: palette.primitive?.success[25],
      warning: palette.primitive?.warning[10],
      warning2: palette.primitive?.warning[10],
      error: palette.primitive?.error[25],
    },
    COLOR: {
      default: palette.text.primary,
      blue: palette.primitive?.blue[100],
      success: palette.primitive?.success[100],
      warning: palette.primitive?.warning[50],
      warning2: palette.primitive?.warning[100],
      error: palette.primitive?.error[100],
    },
    BUTTON: {
      default: palette.text.primary,
      blue: palette.primitive?.blue[100],
      success: palette.primitive?.success[100],
      warning: palette.primitive?.warning[100],
      warning2: palette.primitive?.warning[100],
      error: palette.primitive?.error[100],
    },
  };
};