All files / src/components/common/Toast ToastMessage.tsx

100% Statements 31/31
96.66% Branches 29/30
100% Functions 8/8
100% Lines 29/29

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                                                              2x                   10x 10x   10x 11x   10x 9x     10x 10x 2x 2x 2x     10x   10x 10x 1x       10x                                                                                                                                                             2x 9x   3x   3x   1x   2x       2x 10x   4x   3x   1x   2x          
import { Text } from "@common/Text";
import { Stack, Box } from "@mui/material";
import { palette } from "@palette";
import {
  ErrorIcon,
  InfoIcon,
  CheckRoundIcon,
  WarningIcon,
} from "@assets/icons";
import { useEffect, useRef } from "react";
import { toast } from "react-toastify";
import { useAppSelector } from "@redux/hooks";
import { selectModals } from "@redux/slices/app";
 
export type TToastOptions = {
  titleClamp?: number;
  action?: any;
  onClickSnackbar?: () => void;
};
 
interface IToastMessageProps {
  message: string;
  type: string;
  description: string | React.ReactNode;
  isDesktop?: boolean;
  icon?: React.ReactNode;
  hasColorIndicator?: boolean;
  toastId?: string;
  options?: TToastOptions;
}
 
export const ToastMessage = ({
  type,
  message,
  description,
  isDesktop = true,
  icon,
  hasColorIndicator = true,
  toastId,
  options,
}: IToastMessageProps) => {
  const isModalOpen = useRef<boolean>(false);
  const modals = useAppSelector(selectModals) as Record<string, any>;
 
  const checkIfModalIsOpen = () =>
    Object.values(modals).some((modal) => modal.visible);
 
  useEffect(() => {
    isModalOpen.current = checkIfModalIsOpen();
  }, []);
 
  useEffect(() => {
    if (type === "Error" && isModalOpen.current) {
      const isOpen = checkIfModalIsOpen();
      isModalOpen.current = isOpen;
      if (!isOpen) toast.dismiss(toastId);
    }
  }, [modals]);
  const titleClamp = options?.titleClamp || 2;
 
  const onClick = () => {
    if (typeof options?.onClickSnackbar === "function") {
      return options?.onClickSnackbar;
    }
  };
 
  return (
    <Stack
      onClick={onClick()}
      direction="row"
      gap={1}
      alignItems="stretch"
      height="100%"
    >
      {hasColorIndicator && (
        <Box
          sx={{
            borderRadius: "4px",
            width: "8px",
            minWidth: "8px",
            backgroundColor: getIndicatorColor(type),
          }}
        />
      )}
      <Box
        display="flex"
        alignItems="center"
        justifyContent="center"
        sx={{
          marginInline: "4px 8px",
        }}
      >
        {icon ? icon : getToastIcon(type)}
      </Box>
      <Stack
        direction="column"
        gap="2px"
        justifyContent="center"
        sx={{
          paddingBlock: "5px",
        }}
      >
        <Text
          lineHeight="19px"
          fontSize="16px"
          fontFamily="Give Whyte"
          fontStyle="normal"
          color={palette.neutral.white}
          letterSpacing="-0.01em"
          fontWeight="regular"
          sx={{
            maxHeight: `${Math.max(titleClamp * 19, 57)}px`,
            overflow: "hidden",
            display: "-webkit-box",
            WebkitLineClamp: titleClamp,
            WebkitBoxOrient: "vertical",
          }}
        >
          {isDesktop ? message : description}
        </Text>
        {isDesktop && (
          <Text
            color={palette.gray.main}
            fontFamily="Give Whyte"
            fontStyle="normal"
            lineHeight="17px"
            fontSize="14px"
            fontWeight="light"
            sx={{
              maxHeight: 51,
              overflow: "hidden",
              display: "-webkit-box",
              WebkitLineClamp: 3,
              WebkitBoxOrient: "vertical",
            }}
          >
            {description}
          </Text>
        )}
      </Stack>
      {options?.action ? options.action : null}
    </Stack>
  );
};
 
const getIndicatorColor = (type: string): string => {
  switch (type) {
    case "Success":
      return "#088745";
    case "Error":
      return "#D80D0D";
    case "Warning":
      return "#FF8124";
    default:
      return "#6D9CF8";
  }
};
 
const getToastIcon = (type: string): JSX.Element => {
  switch (type) {
    case "Success":
      return <CheckRoundIcon fill={palette.neutral.white} />;
    case "Error":
      return <ErrorIcon fill={palette.neutral.white} />;
    case "Warning":
      return <WarningIcon fill={palette.neutral.white} />;
    default:
      return <InfoIcon path_fill={palette.neutral.white} />;
  }
};
 
export default ToastMessage;