All files / src/sections/PayBuilder/LaunchStep/PublicURLShare PublicURLShare.tsx

93.54% Statements 29/31
95% Branches 19/20
87.5% Functions 7/8
93.54% Lines 29/31

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                                                                    62x                           609x 609x   609x 68x   5x   1x   8x   54x       609x   609x       609x   609x 68x   5x   5x   54x     4x           609x 70x     16x                               16x                                                                                               609x 69x 69x   8x                         53x                 8x           609x        
import EmailIcon from "@assets/icons/Share/EmailIcon";
import EmbedIcon from "@assets/icons/Share/EmbedIcon";
import FacebookIcon from "@assets/icons/Share/FacebookIcon";
import LinkedinIcon from "@assets/icons/Share/LinkedinIcon";
import ThreadsIcon from "@assets/icons/Share/ThreadsIcon";
import TwitterXIcon from "@assets/icons/Share/TwitterXIcon";
import WhatsappIcon from "@assets/icons/Share/WhatsappIcon";
import useCheckEnvironment from "@hooks/common/useCheckEnvironment";
import { Box } from "@mui/material";
import { QFORM_QUERY_KEY } from "@pages/AcquirerPortal/Enterprises/Modal/constants";
import { useMemo } from "react";
import { useParams } from "react-router-dom";
import {
  EmailShareButton,
  FacebookShareButton,
  LinkedinShareButton,
  TwitterShareButton,
  WhatsappShareButton,
} from "react-share";
import { useQueryObserver } from "../hooks/useQueryObserver";
import { TQueryDataLaunchStepPaymentFormKey } from "../types";
import { EmbedShareButton, ThreadsShareButton } from "./AdditionalButtons";
import LargeVariant from "./LargeVariant";
import type { PublicUrlShareProps, TButtonsMap } from "./PublicShare.types";
import StrictVariant from "./StrictVariant";
import StylessVariant from "./StylessVariant";
import useCheckFormType from "@sections/PayBuilder/components/hooks/useCheckFormType";
 
enum VariantsEnum {
  LARGE = "large",
  STRICT = "strict",
  BODY_LESS = "styless", // without any background or whatsover
}
 
const PublicUrlShare = <T extends "large" | "strict" | "styless">({
  variant,
  anchorEl,
  setAnchorEl,
  handleClose,
  idFromProps,
  query,
  typeFromProps,
}: PublicUrlShareProps<T> & {
  idFromProps?: string;
  query?: string;
  typeFromProps?: string;
}) => {
  const { queryData } =
    useQueryObserver<TQueryDataLaunchStepPaymentFormKey>(QFORM_QUERY_KEY);
  const { id: idInUrl } = useParams<{ id: string }>();
 
  const id = useMemo(() => {
    switch (true) {
      case typeof queryData?.product.id === "number":
        return `${queryData.product.id}`;
      case !!idInUrl:
        return idInUrl;
      case typeof idFromProps === "string":
        return idFromProps;
      default:
        return "";
    }
  }, [queryData?.product.id, idFromProps, idInUrl]);
 
  const { platformBaseUrl } = useCheckEnvironment();
 
  const url = `${platformBaseUrl}/${id}${
    query ? `?sharedProductId=${query}` : ""
  }`;
 
  const { formType: hookFormType } = useCheckFormType();
 
  const formType = useMemo(() => {
    switch (true) {
      case !!queryData?.product.typeName:
        return `${queryData.product.typeName}`;
      case !!typeFromProps:
        return typeFromProps;
      case !!hookFormType:
        return hookFormType;
 
      default:
        return "";
    }
  }, [queryData?.product.typeName, typeFromProps, hookFormType]);
 
  // Channel order is part of the Share modal spec (PayBuilder 032-b AC003.3 /
  // frame 7397-80460): Embed, Email, Facebook, Whatsapp, LinkedIn, X, Threads.
  const ButtonsMap = useMemo(
    () => [
      {
        Component: () => (
          <EmbedShareButton
            url={url}
            productId={id + "" || ""}
            type={formType || "product"}
            handleClose={handleClose}
          >
            <EmbedIcon />
          </EmbedShareButton>
        ),
        Icon: () => <></>,
        id: "embed",
        label: "Embed",
        hidden: variant === "strict",
      },
      {
        Component: ({ url }: { url: string }) => (
          <a href={`mailto:?body=${encodeURIComponent(url)}`}>
            <EmailIcon />
          </a>
        ),
        Icon: EmailIcon,
        id: "email",
        label: "Email",
        hidden: false,
      },
      {
        Component: FacebookShareButton,
        Icon: FacebookIcon,
        id: "facebook",
        label: "Facebook",
        hidden: false,
      },
      {
        Component: WhatsappShareButton,
        Icon: WhatsappIcon,
        id: "whatsapp",
        label: "Whatsapp",
        hidden: false,
      },
      {
        Component: LinkedinShareButton,
        Icon: LinkedinIcon,
        id: "linkedin",
        label: "LinkedIn",
        hidden: false,
      },
      {
        Component: TwitterShareButton,
        Icon: TwitterXIcon,
        id: "x",
        label: "X",
        hidden: false,
      },
      {
        Component: ThreadsShareButton,
        Icon: ThreadsIcon,
        id: "threads",
        label: "Threads",
        hidden: false,
      },
    ],
    [id, url, formType, handleClose],
  );
 
  const component = useMemo(() => {
    const SocialButtons = ButtonsMap as TButtonsMap;
    switch (true) {
      case variant === VariantsEnum.LARGE:
        return (
          <Box
            sx={{
              ...(!id && {
                filter: "blur(3px)",
                pointerEvents: "none",
              }),
            }}
          >
            <LargeVariant ButtonsMap={ButtonsMap as TButtonsMap} url={url} />
          </Box>
        );
      case variant === VariantsEnum.STRICT:
        return (
          <StrictVariant
            ButtonsMap={SocialButtons}
            url={url}
            anchorEl={anchorEl}
            setAnchorEl={setAnchorEl}
          />
        );
      case variant === "styless":
        return <StylessVariant ButtonsMap={SocialButtons} url={url} />;
      default:
        return <LargeVariant ButtonsMap={SocialButtons} url={url} />;
    }
  }, [url, anchorEl]);
 
  return component;
};
 
export default PublicUrlShare;