All files / src/sections/PayBuilder/components/products/Hero HeroImage.tsx

95.65% Statements 22/23
81.39% Branches 35/43
100% Functions 3/3
95.45% Lines 21/22

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                            45x                                   180x 180x 180x 180x 180x 180x   180x   180x   152x 276x         152x 152x   152x 134x                                                                                                                                   18x                                     45x 180x 177x   3x 3x        
import { Box } from "@mui/material";
import { useSignals } from "@preact/signals-react/runtime";
import { usePayBuilderContext } from "@sections/PayBuilder/provider/PayBuilderContext";
import {
  ImageCanvasTypes,
  ResizeType,
} from "@sections/PayBuilder/provider/provider.type";
import useCheckFormType from "../../hooks/useCheckFormType";
import {
  extractYoutubeVideoID,
  useGetRedesignedFormTypes,
} from "@sections/PayBuilder/utils";
import VideoPreview from "@sections/PayBuilder/Forms/About/VideoPreview";
 
export const HeroImage = ({
  src,
  isPreview,
  canvasData,
  borderRadius = 4,
  height = "302px",
  width = "700px",
  selectedVideoURL,
}: {
  src?: string;
  canvasData?: ImageCanvasTypes;
  isPreview?: boolean;
  borderRadius?: number;
  height?: string;
  width?: string;
  isLoading?: boolean;
  selectedVideoURL?: string | null;
}) => {
  useSignals();
  const hideImage = !src && !isPreview;
  const { resizeType = "fill" } = canvasData || {};
  const objectFit = getObjectFit(resizeType);
  const { screenStates: { isMobileView } = {} } = usePayBuilderContext() || {};
  const redesignedFormTypes = useGetRedesignedFormTypes();
 
  const { formType } = useCheckFormType();
 
  if (hideImage) return null;
 
  const getSrc = () => {
    return src
      ? src
      : `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 ${width} ${height}'%3E%3Crect width='100%25' height='100%25' fill='%23f0f0f0'/%3E%3C/svg%3E`;
  };
 
  const isRedesigned = redesignedFormTypes.includes(formType);
  const isContain = objectFit === "contain";
 
  if (isRedesigned) {
    return (
      <Box
        sx={{
          position: "relative",
          width: "100%",
          maxWidth: "100%",
          height: height,
          display: "flex",
          justifyContent: "center",
          alignItems: "center",
        }}
      >
        {!isMobileView && (
          <Box
            component="img"
            src={getSrc()}
            alt="Hero-Background"
            sx={{
              position: "absolute",
              height: height,
              maxHeight: "100%",
              objectFit: "cover",
              opacity: 0.2,
              top: 0,
              left: 0,
              width: "100%",
              borderRadius: "16px",
              objectPosition: "50% 50%",
              filter: "blur(10px)",
              zIndex: 1,
            }}
          />
        )}
 
        {selectedVideoURL ? (
          <VideoPreview
            videoID={extractYoutubeVideoID(selectedVideoURL) || ""}
            style={{
              zIndex: 2,
              borderRadius: "0px",
              maxWidth: "536px",
            }}
          />
        ) : (
          <Box
            component="img"
            src={getSrc()}
            alt="Hero-Foreground"
            sx={{
              height: height,
              zIndex: 1,
              maxWidth: "100%",
              ...(resizeType === "fill" && {
                width: "100%",
                borderRadius: isMobileView ? "0" : "16px",
              }),
              ...(isMobileView && {
                width: "100%",
              }),
              objectFit,
            }}
          />
        )}
      </Box>
    );
  } else {
    return (
      <Box
        component="img"
        src={getSrc()}
        alt="Hero"
        sx={{
          width: src && isContain ? undefined : `min(${width}, 100%)`,
          maxHeight: height,
          maxWidth: isContain ? "100%" : undefined,
          objectFit: objectFit,
          borderRadius: borderRadius,
          fontSize: 0,
          lineHeight: 0,
          objectPosition: "50% 50%",
        }}
      />
    );
  }
};
const getObjectFit = (resizeType?: ResizeType) => {
  if (resizeType === "fill" || resizeType === "crop") {
    return "cover";
  }
  Eif (resizeType === "fit" || resizeType === "original") {
    return "contain";
  }
  return "cover";
};