All files / src/sections/PayBuilder/components/products/ProductPageContainer ProductPageLogoComponent.tsx

71.11% Statements 32/45
43.13% Branches 22/51
80% Functions 8/10
72.09% Lines 31/43

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 207 208 209 210                                              12x           154x 154x 154x 154x 154x   154x 22x 22x   22x 22x   22x 22x       22x 22x                     154x 22x                                                                                               154x     22x               154x 22x       22x                                       22x     154x       154x       154x                                                         12x   12x                   12x 628x 154x                      
import useMasqueradeReducer from "@hooks/Reducers/useMasqueradeReducer";
import { useUser } from "@hooks/common/useUser";
import { Box } from "@mui/material";
import { useImageSource } from "@sections/PayBuilder/Forms/About/utils";
import { isUrl } from "@sections/PayBuilder/helpers";
import { LogoImage } from "@sections/PayBuilder/components/products/Hero/LogoImage";
import { MediaItem } from "@sections/PayBuilder/provider/provider.type";
import GiveText from "@shared/Text/GiveText";
import { styled } from "@theme/v2/Provider";
import DOMPurify from "dompurify";
import draftToHtml from "draftjs-to-html";
import parse from "html-react-parser";
import { useMemo } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { safeParse } from "@utils/index";
 
type Props = {
  logoUrl?: string | MediaItem;
  showBackToPortalButton?: boolean;
  isPreview?: boolean;
  boxSx?: any;
};
 
const ProductPageLogoComponent = ({
  logoUrl,
  showBackToPortalButton,
  isPreview,
  boxSx = {},
}: Props) => {
  const { data } = useImageSource(logoUrl);
  const { isMasqueradeMode } = useMasqueradeReducer();
  const { isMerchant } = useUser();
  const params = useParams<{ id: string }>();
  const navigate = useNavigate();
 
  const logoState = useMemo(() => {
    let urlString = "";
    Iif (typeof logoUrl === "object" && logoUrl !== null) {
      urlString = logoUrl.URL ?? "";
    } else Eif (typeof logoUrl === "string") {
      urlString = logoUrl;
    }
    Eif (typeof urlString === "string") {
      Iif (isUrl(urlString)) {
        return "";
      }
 
      Eif (urlString === "") {
        return "";
      }
 
      const parsed = safeParse<any>(urlString);
      if (parsed?.blocks) {
        return draftToHtml(parsed);
      }
    }
    return "";
  }, [logoUrl]);
 
  const sanitizedLogoState = useMemo(() => {
    Eif (!logoState) return "";
    return DOMPurify.sanitize(logoState, {
      ALLOWED_TAGS: [
        "b",
        "i",
        "em",
        "strong",
        "a",
        "ul",
        "ol",
        "li",
        "p",
        "br",
        "span",
        "div",
        "img",
        "h1",
        "h2",
        "h3",
        "h4",
        "h5",
        "h6",
        "blockquote",
        "pre",
        "code",
        "u",
        "ins",
        "del",
        "s",
        "sub",
        "sup",
      ],
      ALLOWED_ATTR: [
        "href",
        "target",
        "rel",
        "src",
        "alt",
        "title",
        "style",
        "class",
        "width",
        "height",
      ],
      FORBID_TAGS: ["iframe", "script", "object", "embed", "form"],
    });
  }, [logoState]);
 
  const isLogoMediaItem = (
    logo: string | MediaItem | undefined,
  ): logo is MediaItem => {
    return (
      typeof logo === "object" &&
      logo !== null &&
      "URL" in logo &&
      typeof logo.URL === "string"
    );
  };
 
  const { url, canvasData } = useMemo(() => {
    Iif (typeof logoUrl === "string" && isUrl(logoUrl)) {
      return { url: logoUrl, canvasData: undefined };
    }
 
    Iif (
      isLogoMediaItem(logoUrl) &&
      typeof logoUrl.URL === "string" &&
      isUrl(logoUrl.URL.trim())
    ) {
      return {
        url: logoUrl.URL,
        canvasData: {
          x: logoUrl.x,
          y: logoUrl.y,
          width: logoUrl.width,
          height: logoUrl.height,
          rotate: logoUrl.rotate,
          scaleX: logoUrl.scaleX,
          scaleY: logoUrl.scaleY,
          resizeType: logoUrl.resizeType,
        },
      };
    }
 
    return { url: null, canvasData: undefined };
  }, [logoUrl]);
  const conditionToShowBackButton =
    (isMasqueradeMode ||
      (showBackToPortalButton && params.id !== "" && isMerchant)) &&
    !isPreview;
 
  const goBack = () => {
    if (isPreview) return;
    navigate(`/${params.id}`);
  };
  return (
    <>
      {url ? (
        <StyledImageBox
          sx={boxSx}
          conditionToShowBackButton={conditionToShowBackButton}
        >
          {typeof logoUrl === "object" && (
            <LogoImage src={data} height={`${logoUrl.logoSize ?? "94"}px`} />
          )}
        </StyledImageBox>
      ) : (
        <Box
          sx={{
            width: "100%",
            display: "flex",
            justifyContent: "center",
            ...boxSx,
          }}
        >
          <TextLogo isPreview={isPreview} variant="bodyL" onClick={goBack}>
            {parse(DOMPurify.sanitize(sanitizedLogoState))}
          </TextLogo>
        </Box>
      )}
    </>
  );
};
 
const StyledImageBox = styled(Box, {
  shouldForwardProp: (prop) =>
    prop !== "conditionToShowBackButton" && prop !== "isInvoice",
})<{ conditionToShowBackButton?: boolean }>(
  ({ conditionToShowBackButton }) => ({
    padding: "0",
    paddingTop: `${conditionToShowBackButton ? 68 : 32}px`,
    paddingBottom: "32px",
    alignSelf: "center",
  }),
);
 
const TextLogo = styled(GiveText, {
  shouldForwardProp: (prop) => prop !== "isPreview",
})<{ isPreview?: boolean }>(({ isPreview }) => ({
  wordWrap: "break-word",
  cursor: isPreview ? "default" : "pointer",
  width: "fit-content",
  marginTop: "30px",
  lineHeight: "1.5",
  wordBreak: "break-all",
  textAlign: "center",
}));
 
export default ProductPageLogoComponent;