All files / src/features/Drawer DrawerAccountSelect.tsx

53.12% Statements 17/32
67.24% Branches 39/58
14.28% Functions 1/7
56.66% Lines 17/30

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                                                          21x                   101x   101x 101x   101x   101x   101x 101x 101x 101x   101x         101x                             101x   101x           30x     71x                             71x                                                                                                                                    
import {
  TParsedAccount,
  useAccountSelection,
} from "@pages/Login/modals/hooks/useAccountSelectionModal";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useGetMerchantById } from "@hooks/enterprise-api/account/useGetMerchants";
import { Box } from "@mui/material";
import { CaretDownIcon, CaretUpIcon } from "@phosphor-icons/react";
import { useAppSelector } from "@redux/hooks";
import { selectMasqueradeMode } from "@redux/slices/app";
import ContextualMenuOption from "@shared/ContextualMenu/ContextualMenuOption";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import React, { useState } from "react";
import SwitchAccountsMenu from "./SwitchAccountsMenu";
import { addSizeToImage } from "@utils/image.helpers";
import GiveThumbnail from "@shared/Thumbnail/GiveThumbnail";
import { getUserRole } from "@hooks/auth-api/useLogin";
 
type DrawerProps = {
  drawerOpen: boolean;
  handleDrawerOpen: () => void;
  onChooseAccount?: (cb: () => void) => void;
  hideAvatar?: boolean;
  baseRender?: () => React.ReactNode;
  accountSelectorRef?: React.RefObject<HTMLDivElement>;
  onSuccessFecth?: (res: TParsedAccount[]) => void;
  CustomButton?: (props: { onClick: any }) => JSX.Element;
};
 
const DrawerAccountSelect: React.FC<DrawerProps> = ({
  drawerOpen,
  handleDrawerOpen,
  onChooseAccount,
  hideAvatar,
  baseRender,
  accountSelectorRef,
  onSuccessFecth,
  CustomButton,
}) => {
  const { name: masqName } = useAppSelector(selectMasqueradeMode);
 
  const { accountList, chooseAccount } = useAccountSelection(onSuccessFecth);
  const { data: merchantData } = useGetMerchantById();
 
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
 
  const { merchantId, name, img, isControlMode } = useGetCurrentMerchantId();
 
  const account = accountList.find((el) => el.id === merchantId);
  const avatarImage = img || addSizeToImage(account?.img || "", "thumb") || "";
  const merchantName = name || account?.name || "";
  const accountType = account?.merchType || getUserRole(merchantData?.type);
 
  const handleOpenMenu = (event: React.MouseEvent<HTMLElement>) => {
    if (accountList.length === 0 || isControlMode) return;
    setAnchorEl(anchorEl ? null : event.currentTarget);
  };
 
  const onPress = (idx: number) => {
    handleDrawerOpen();
    if (onChooseAccount) {
      return onChooseAccount(() => {
        chooseAccount(idx);
        setAnchorEl(null);
      });
    } else {
      chooseAccount(idx);
      setAnchorEl(null);
 
      return true;
    }
  };
 
  const isControlModeVariation = isControlMode || accountList.length <= 1;
 
  if (baseRender && accountList.length <= 1) {
    //if baseRender is provided and there is only one account, we render the baseRender
    /*
    This change ensures the component always returns a React element by wrapping the baseRender() result in a React Fragment. 
    This should resolve the type error since we're now guaranteeing a valid React element return type.
    */
    return <>{baseRender()}</>;
  }
 
  Iif (CustomButton) {
    return (
      <>
        <CustomButton onClick={handleOpenMenu} />
        <SwitchAccountsMenu
          accountList={accountList}
          chooseAccount={onPress}
          drawerOpen={drawerOpen}
          anchorEl={anchorEl}
          onCloseMenu={() => setAnchorEl(null)}
        />
      </>
    );
  }
 
  return (
    <Box
      ref={accountSelectorRef}
      onClick={isControlModeVariation ? undefined : handleOpenMenu}
      flexShrink={0}
      sx={{
        width: drawerOpen ? "auto" : "fit-content",
        alignSelf: drawerOpen ? "auto" : "center",
        overflow: "hidden",
        ...((accountList.length <= 1 || isControlMode) && {
          cursor: "default",
        }),
        ...(accountList.length > 1 && {
          cursor: "pointer",
        }),
      }}
    >
      {drawerOpen ? (
        <ContextualMenuOption
          onClick={isControlModeVariation ? undefined : handleOpenMenu}
          text={masqName || merchantName}
          {...(!isControlMode &&
            accountList.length > 1 && {
              IconRight: anchorEl ? CaretUpIcon : CaretDownIcon,
            })}
          {...(isControlModeVariation && {
            shouldDefaultCursor: true,
          })}
          Image={
            !hideAvatar && (
              <GiveThumbnail
                imageUrl={avatarImage}
                size={"small"}
                type={accountType || "merchant"}
              />
            )
          }
          {...(hideAvatar && { sx: { pl: 0 } })}
        />
      ) : (
        <GiveTooltip
          title={name}
          color="default"
          disableHoverListener={drawerOpen}
          placement="right"
        >
          <GiveThumbnail
            imageUrl={avatarImage}
            size={"small"}
            type={accountType || "merchant"}
          />
        </GiveTooltip>
      )}
 
      <SwitchAccountsMenu
        accountList={accountList}
        chooseAccount={onPress}
        drawerOpen={drawerOpen}
        anchorEl={anchorEl}
        onCloseMenu={() => setAnchorEl(null)}
      />
    </Box>
  );
};
 
export default React.memo(DrawerAccountSelect);