All files / src/hooks/common useEnterpriseLogo.tsx

96.29% Statements 26/27
70.83% Branches 17/24
100% Functions 6/6
96.29% Lines 26/27

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            5x 5x   5x 5x   5x 5x   5x 224x   224x     224x     25x         25x                 224x               5x             5x 224x                                             5x                 224x 224x 224x   224x       224x                                       5x 26x                                 5x 1x                                    
import { customInstance } from "@services/api";
import { useQuery } from "react-query";
import ReactPlaceholder from "react-placeholder";
import { RoundShape } from "react-placeholder/lib/placeholders";
import { Box } from "@mui/material";
 
const WIDTH = 48;
const HEIGHT = 48;
 
const MAX_WIDTH = 150;
const MAX_HEIGHT = 75;
 
const LANDSCAPE_MAX_HEIGHT = 48;
const LANDSCAPE_MAX_WIDTH = 200;
 
export const useEnterpriseLogo = () => {
  const pathname = window.location.href;
  const isEnabledFetching =
    !pathname.startsWith("https://local.") &&
    !pathname.startsWith("https://portal.");
 
  const { data, isFetching, isError } = useQuery(
    "get_enterpise_public_info",
    async () => {
      const data = customInstance({
        url: "/merchants-public",
        method: "GET",
      });
 
      return data;
    },
    {
      retry: 1,
      refetchOnWindowFocus: false,
      enabled: isEnabledFetching,
    },
  );
 
  return {
    url: data?.imageURL,
    isFetching,
    isError,
    landscapeImageURL: data?.landscapeImageURL,
  };
};
 
const mapPos = {
  left: "flex-start",
  center: "center",
  right: "flex-end",
};
type posType = "left" | "center" | "right";
 
const awesomePlaceholder = (position: posType) => (
  <div
    className="my-awesome-placeholder"
    style={{
      width: "100%",
      display: "flex",
      alignItems: "center",
      justifyContent: mapPos[position],
    }}
  >
    <RoundShape
      style={{
        width: WIDTH,
        height: HEIGHT,
        borderRadius: 8,
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
      }}
      color="white"
    />
  </div>
);
 
export const MainLogo = ({
  defaultLogo,
  position = "left",
  isAuthPage = false,
}: {
  defaultLogo: React.ReactElement;
  position?: posType;
  isAuthPage?: boolean;
}) => {
  const { landscapeImageURL, url, isError, isFetching } = useEnterpriseLogo();
  const URL = landscapeImageURL || url;
  const ImageLogo = isAuthPage ? AuthPageImageLogo : GeneralPageImageLogo;
 
  Iif (isError) {
    return defaultLogo;
  }
 
  return (
    <ReactPlaceholder
      showLoadingAnimation
      ready={!isFetching}
      customPlaceholder={awesomePlaceholder(position)}
    >
      {URL ? (
        <ImageLogo URL={URL} isLandscape={!!landscapeImageURL} />
      ) : (
        defaultLogo
      )}
    </ReactPlaceholder>
  );
};
 
interface ImageLogoProps {
  URL: string;
  isLandscape?: boolean;
}
 
const AuthPageImageLogo = ({ URL, isLandscape }: ImageLogoProps) => {
  return (
    <Box>
      <img
        style={{
          width: isLandscape ? LANDSCAPE_MAX_WIDTH : "100%",
          height: isLandscape ? LANDSCAPE_MAX_HEIGHT : "100%",
          ...(!isLandscape && { maxWidth: MAX_WIDTH, maxHeight: MAX_HEIGHT }),
          borderRadius: 8,
          objectFit: isLandscape ? "cover" : "contain",
        }}
        src={`${URL}/medium`}
        data-testid="main-logo"
      />
    </Box>
  );
};
 
const GeneralPageImageLogo = ({ URL, isLandscape }: ImageLogoProps) => {
  return (
    <Box
      width={isLandscape ? LANDSCAPE_MAX_WIDTH : WIDTH}
      height={isLandscape ? LANDSCAPE_MAX_HEIGHT : HEIGHT}
    >
      <img
        style={{
          width: "100%",
          height: "100%",
          borderRadius: 8,
          objectFit: "contain",
        }}
        src={`${URL}/thumb`}
        data-testid="main-logo"
      />
    </Box>
  );
};