All files / src/componentsV2/SideMenu SideMenu.tsx

91.3% Statements 42/46
75% Branches 24/32
90.9% Functions 10/11
93.18% Lines 41/44

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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252                                                                  12x           10x 10x 10x   10x   10x     10x 10x   10x 4x     10x 4x 4x   4x 5x 5x 5x       4x 4x   4x 4x   4x 4x 4x 4x       10x 1x   1x     1x     10x                                                                                                               59x 59x                                               20x 20x                                                         12x         12x                   12x             12x       12x 10x                                         12x        
import React, { useEffect, useState, useRef } from "react";
import { styled } from "@theme/v2/Provider";
import DrawerAccountSelect from "features/Drawer/DrawerAccountSelect";
import { Divider as DividerMui, Stack, Skeleton } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import { useMenu } from "./hooks/useMenu";
import SideMenuItem from "./components/SideMenuItem";
import UserSection from "./components/UserSection";
import MerchantAccountSection from "./components/MerchantAccountSection";
import { useNavigate } from "react-router-dom";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import NiceModal from "@ebay/nice-modal-react";
import { CREATE_PRE_BUILD_FORM_MODAL } from "modals/modal_names";
import { useQueryObserver } from "@sections/PayBuilder/LaunchStep/hooks/useQueryObserver";
import { QFORM_QUERY_KEY } from "@pages/AcquirerPortal/Enterprises/Modal/constants";
import { TQueryDataLaunchStepPaymentFormKey } from "@sections/PayBuilder/LaunchStep/types";
import UserSectionNew from "./components/UserSectionNew";
import ProfileMenu from "./components/UserSection/ProfileMenu";
import { FeatureFlagKeys } from "FeatureFlags/featureFlagKeys";
import FlaggedWrapper from "FeatureFlags/FlaggedWrapper";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { CALCULATION_IN_PROGRESS } from "@constants/permissions";
import { BALANCE_MAINTENANCE_TOOLTIP_MESSAGE } from "@shared/constants";
import { parseAmount, parseToNumber } from "@utils/index";
import { useGetCurrentMerchantId } from "@hooks/common";
 
type TParams = {
  open: boolean;
  availableBalance: number | string;
  handleDrawerClose: () => void;
  handleDrawerOpen: () => void;
};
 
const SideMenu = ({
  open,
  availableBalance,
  handleDrawerClose,
  handleDrawerOpen,
}: TParams) => {
  const { isDesktopView } = useCustomThemeV2();
  const navigate = useNavigate();
  const { topMenu, bottomMenu, curPath, isMerchant, isEnterprise } = useMenu();
  const { remove } =
    useQueryObserver<TQueryDataLaunchStepPaymentFormKey>(QFORM_QUERY_KEY);
 
  const [showMerchantAccountSection, setShowMerchantAccountSection] = useState(
    isMerchant || isEnterprise,
  );
  const [showTopShadow, setShowTopShadow] = useState(false);
  const scrollerRef = useRef<HTMLDivElement>(null);
 
  useEffect(() => {
    setShowMerchantAccountSection(isMerchant || isEnterprise);
  }, [isMerchant, isEnterprise]);
 
  useEffect(() => {
    const scroller = scrollerRef.current;
    Iif (!scroller) return;
 
    const checkScroll = () => {
      const isScrollable = scroller.scrollHeight > scroller.clientHeight;
      const isScrolled = scroller.scrollTop > 0;
      setShowTopShadow(isScrollable && isScrolled);
    };
 
    // Check immediately and after a short delay to account for content rendering
    checkScroll();
    const timeoutId = setTimeout(checkScroll, 100);
 
    scroller.addEventListener("scroll", checkScroll);
    window.addEventListener("resize", checkScroll);
 
    return () => {
      clearTimeout(timeoutId);
      scroller.removeEventListener("scroll", checkScroll);
      window.removeEventListener("resize", checkScroll);
    };
  }, [open, topMenu]);
 
  const handleClick = (value: string) => {
    !isDesktopView && handleDrawerClose();
 
    Iif (value === "new-form") {
      remove();
      NiceModal.show(CREATE_PRE_BUILD_FORM_MODAL);
    } else navigate(value);
  };
 
  return (
    <Container>
      <Scroller ref={scrollerRef}>
        <Top>
          <DrawerAccountSelect
            handleDrawerOpen={handleDrawerOpen}
            drawerOpen={open}
          />
          <Menu>
            {open && (
              <>
                <Divider />
                <Stack spacing="4px" marginBottom="20px">
                  {availableBalance === "-" ? (
                    <GiveTooltip
                      title={BALANCE_MAINTENANCE_TOOLTIP_MESSAGE}
                      placement="top"
                      fluidWidth
                    >
                      <Skeleton
                        variant="rounded"
                        width={100}
                        height={20}
                        sx={{
                          borderRadius: "40px",
                        }}
                      />
                    </GiveTooltip>
                  ) : (
                    <GiveTooltip
                      title={
                        availableBalance === "-"
                          ? CALCULATION_IN_PROGRESS
                          : availableBalance.toLocaleString()
                      }
                      placement="bottom"
                      disableHoverListener={
                        availableBalance !== "-" &&
                        Math.abs(parseToNumber(availableBalance) || 0) <
                          1_000_000
                      }
                      fluidWidth
                    >
                      <GiveText variant="bodyS" color="primary">
                        {parseAmount(availableBalance)}
                      </GiveText>
                    </GiveTooltip>
                  )}
                  <GiveText variant="bodyXS" color="secondary">
                    Available Balance (USD)
                  </GiveText>
                </Stack>
              </>
            )}
 
            {topMenu.map((i, ind) => {
              const { Icon, CustomIcon, ...rest } = i;
              return (
                <SideMenuItem
                  key={ind}
                  Icon={Icon ? <Icon size={20} /> : CustomIcon}
                  {...rest}
                  open={open}
                  onClick={handleClick}
                  isSelected={curPath.includes(i.value)}
                />
              );
            })}
          </Menu>
        </Top>
      </Scroller>
      <Bottom showTopShadow={showTopShadow}>
        {showMerchantAccountSection && (
          <MerchantAccountSection
            open={open}
            onClose={() => setShowMerchantAccountSection(false)}
            isEnterprise={isEnterprise}
          />
        )}
        <Menu>
          {bottomMenu.map((i, ind) => {
            const { Icon, CustomIcon, ...rest } = i;
            return (
              <SideMenuItem
                key={ind}
                Icon={Icon ? <Icon size={20} /> : CustomIcon}
                {...rest}
                open={open}
                onClick={i.customOnClick ? i.customOnClick : handleClick}
                isSelected={curPath.includes(i.value)}
              />
            );
          })}
        </Menu>
        <FlaggedWrapper
          ActiveComponent={
            <>
              <UserSectionNew open={open} />
              <ProfileMenu open={open} />
            </>
          }
          FallbackComponent={<UserSection open={open} />}
          name={FeatureFlagKeys.NEW_CONVERSATIONS}
        />
      </Bottom>
    </Container>
  );
};
 
export default React.memo(SideMenu);
 
const Container = styled(Stack)({
  flex: 1,
  justifyContent: "space-between",
});
 
const Scroller = styled(Stack)({
  height: "100%",
  overflowY: "auto",
  position: "relative",
  "&::-webkit-scrollbar": {
    display: "none",
  },
  scrollbarWidth: "none",
});
 
const Top = styled(Stack)({
  flex: 1,
  width: "100%",
  position: "absolute",
  gap: "16px",
});
 
const Menu = styled(Stack)({
  gap: "4px",
});
 
const Bottom = styled(Stack)<{ showTopShadow?: boolean }>(
  ({ showTopShadow }) => ({
    paddingTop: "12px",
    gap: "16px",
    width: "100%",
    position: "relative",
    ...(showTopShadow && {
      "&::before": {
        content: '""',
        position: "absolute",
        top: 0,
        left: "-20px",
        right: "-20px",
        height: "12px",
        boxShadow: "0px -4px 8px 0px #0000000A",
        zIndex: 1,
        pointerEvents: "none",
      },
    }),
  }),
);
 
const Divider = styled(DividerMui)(({ theme }) => ({
  marginBottom: "16px",
  color: theme.palette.border?.primary,
}));