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

91.3% Statements 21/23
75.86% Branches 22/29
75% Functions 6/8
94.44% Lines 17/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 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                                                                  493x                       493x                 144x 144x     144x     144x       144x                                                         144x     493x       493x   493x   493x                   7x 7x   7x     7x       7x                                                                          
import { ToastPosition, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import { store } from "../../../redux/store";
import React from "react";
import _ from "lodash";
import { SnackbarIconsType } from "@shared/Snackbar/GiveSnackbarTypes";
import GiveSnackbar from "@shared/Snackbar/GiveSnackbar";
import { TToastOptions } from "./ToastMessage";
 
type Message =
  | "Error"
  | "Info"
  | "Success"
  | "Warning"
  | "Default"
  | "Switch"
  | "Invitation"
  | "SendingInvitation"
  | "Download";
export type TPosition = {
  position?: ToastPosition;
  style?: React.CSSProperties;
};
 
type TSnackbarOptions = {
  closeButton?: React.ReactNode;
  closeOnClick?: boolean;
  progress?: number;
  rightContent?: React.ReactNode;
  lineClamp?: number;
  showTitle?: boolean;
};
 
const iconTypeMap: Record<Message, SnackbarIconsType> = {
  Error: "error",
  Info: "info", // we only use info in old snackbar, and didnt call it in new one so no need to map it to warning
  Success: "success",
  Warning: "warning",
  Default: "warning",
  Invitation: "invitation",
  Switch: "switch",
  SendingInvitation: "sending-invitation",
  Download: "download",
};
 
const showMessage = (
  message: Message,
  description: string | React.ReactNode,
  isDesktop = true,
  title?: string,
  autoClose?: number | false,
  ToastPosition?: TPosition,
  options?: TSnackbarOptions & Partial<TToastOptions>,
) => {
  const _modals = store.getState().modals as Record<string, any>;
  const isModalOpen = Object.values(_modals).some((modal) => modal.visible);
 
  // for backward compatibility, as new one uses is Mobile
  const isMobile = !isDesktop;
 
  const toastAutoclose =
    (isModalOpen && message === "Error") || message === "Default"
      ? 5000
      : autoClose;
 
  const toastified = toast(
    <GiveSnackbar
      type={iconTypeMap[message]}
      title={title || message}
      description={description}
      progress={options?.progress}
      rightContent={options?.rightContent || options?.action}
      lineClamp={options?.lineClamp}
      showTitle={options?.showTitle}
      isMobile={isMobile}
    />,
    {
      autoClose: toastAutoclose,
      toastId: message,
      position: ToastPosition?.position,
      style: {
        background: "transparent",
        padding: 0,
        margin: 0,
        width: isMobile ? "90%" : "fit-content",
        ...(isMobile && {
          bottom: "20px",
          marginLeft: "auto",
        }),
        boxShadow: "none",
        ...ToastPosition?.style,
      },
    },
  );
  return toastified;
};
 
const showDefault = (description: string, isMobile = false) => {
  showMessage("Invitation", description, isMobile, "");
};
 
const clearToasts = () => _.delay(() => toast.dismiss(), 2000);
 
const dismissToast = (toastId: string) => toast.dismiss(toastId);
 
const updateToast = (
  toastId: string,
  message: Message,
  description: string | React.ReactNode,
  isDesktop = true,
  title?: string,
  autoClose?: number | false,
  ToastPosition?: TPosition,
  options?: TSnackbarOptions & Partial<TToastOptions>,
) => {
  const _modals = store.getState().modals as Record<string, any>;
  const isModalOpen = Object.values(_modals).some((modal) => modal.visible);
 
  const isMobile = !isDesktop;
 
  const toastAutoclose =
    (isModalOpen && message === "Error") || message === "Default"
      ? 5000
      : autoClose;
 
  toast.update(toastId, {
    autoClose: toastAutoclose,
    position: ToastPosition?.position,
    style: {
      background: "transparent",
      padding: 0,
      margin: 0,
      width: isMobile ? "90%" : "fit-content",
      ...(isMobile && {
        bottom: "20px",
        marginLeft: "auto",
      }),
      boxShadow: "none",
      ...ToastPosition?.style,
    },
    render: (
      <GiveSnackbar
        type={iconTypeMap[message]}
        title={title ?? message}
        description={description}
        progress={options?.progress}
        rightContent={options?.rightContent || options?.action}
        lineClamp={options?.lineClamp}
        showTitle={options?.showTitle}
        isMobile={isMobile}
      />
    ),
  });
};
 
export {
  showDefault,
  showMessage,
  clearToasts,
  dismissToast,
  toast,
  updateToast,
};