All files / src/componentsV2/Table ProviderActionsHandler.tsx

75.86% Statements 22/29
71.42% Branches 15/21
46.15% Functions 6/13
74.07% Lines 20/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 153 154 155 156 157 158 159 160 161 162 163                                                10x                   163x 163x   163x   163x   163x 24x   2x   22x       163x                                 163x           22x   21x 21x       163x               163x 3x                             27x                 3x                                     160x                                                            
import { useEffect, useMemo, useState } from "react";
import { DotsThreeVerticalIcon, PlusIcon, XIcon } from "@phosphor-icons/react";
import { Stack } from "@mui/material";
import GiveMerchantTableActions from "@shared/MerchantsProviders/GiveMerchantTableActions";
import GiveExpandingSearch from "@shared/SearchBar/GiveExpandingSearch";
import { useAppSelector } from "@redux/hooks";
import { selectMerchantStatusName } from "@redux/slices/enterprise/merchants";
import { StatusNames } from "@components/Merchants/MerchantList/MerchantStatusFilters";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import { useGiveEnterpriseFilter } from "./hooks/useGiveEnterpriseFilter";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
import {
  Container,
  ACTIONS,
  MOBILE_ACTIONS_ICONS,
} from "@features/MerchantPortal/ManageMoney/ManageMoneyActions.atoms";
import { CompactTableActionsProps, CompactTableFilter } from "./Table.types";
 
type Props = {
  handleSearchChange?: (value: string) => void;
  search: string;
} & CompactTableActionsProps;
 
const ProviderActionsHandler = ({
  handleSearchChange,
  search,
  compact = false,
  filters,
  selectedTab,
  handleCreate,
  createLabel,
  disableCreate,
}: Props) => {
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
  const { isNarrowView: showMobileActions } = useCustomThemeV2();
  const { handleResetFilters, handleOpenModal, filterLabel, filtersAmount } =
    useGiveEnterpriseFilter();
 
  const statusName = useAppSelector(selectMerchantStatusName);
 
  const currentTab = useMemo(() => {
    switch (statusName) {
      case StatusNames.enterpriseUnderwriting:
        return "underwriting";
      default:
        return "all";
    }
  }, [statusName]);
 
  const providerActions = [
    {
      ...ACTIONS.filter,
      label: filterLabel,
      handleAction: handleOpenModal,
      ...(filtersAmount > 0 && {
        endIcon: (
          <GiveIconButton
            Icon={XIcon}
            size="extraSmall"
            onClick={handleResetFilters}
          />
        ),
      }),
    },
  ];
 
  useEffect(() => {
    // The compact (scroll-revealed) banner mounts/unmounts as the user scrolls.
    // Its unmount must NOT clear the search, otherwise triggering a search
    // (which scrolls back to top and hides the compact banner) instantly wipes
    // the query. The always-mounted full banner owns the tab-change/unmount
    // reset. See TRA013.
    if (compact) return;
 
    return () => {
      handleSearchChange?.("");
    };
  }, [currentTab, compact]);
 
  const mobileActions = providerActions.map((action) => ({
    text: action.label,
    IconRight: MOBILE_ACTIONS_ICONS[action.id],
    onClick: () => {
      action.handleAction();
    },
  }));
 
  if (compact) {
    const compactMenuOptions = [
      ...(handleCreate
        ? [
            {
              text: createLabel ?? "Add",
              IconRight: PlusIcon,
              disabled: disableCreate,
              onClick: () => handleCreate(),
            },
          ]
        : []),
      ...mobileActions,
      ...((filters?.length ?? 0) > 0
        ? [{ type: "divider" as const, text: "" }]
        : []),
      ...(filters ?? []).map((filter: CompactTableFilter) => ({
        text: filter.label,
        isSelected: filter.value === selectedTab,
        disabled: filter.disabled,
        hidden: filter.hidden,
        onClick: () => filter.onClickItem?.(),
      })),
    ];
 
    return (
      <Stack direction="row" alignItems="center" gap="8px">
        <GiveExpandingSearch value={search} onChange={handleSearchChange} />
        <GiveIconButton
          Icon={DotsThreeVerticalIcon}
          variant="ghost"
          onClick={(e) => setAnchorEl(e.currentTarget)}
        />
        <ContextualMenu
          anchorEl={anchorEl}
          options={compactMenuOptions}
          handleClose={() => setAnchorEl(null)}
          color="primary"
          texture="solid"
        />
      </Stack>
    );
  }
 
  return (
    <Container isMobileView={showMobileActions}>
      <GiveMerchantTableActions
        actions={providerActions}
        search={search}
        onSearchChange={handleSearchChange}
        isAllActionsHidden={showMobileActions}
        searchOnEnter={true}
      />
      {showMobileActions && (
        <>
          <GiveIconButton
            Icon={DotsThreeVerticalIcon}
            variant="ghost"
            onClick={(e) => setAnchorEl(e.currentTarget)}
          />
          <ContextualMenu
            anchorEl={anchorEl}
            options={mobileActions}
            handleClose={() => setAnchorEl(null)}
            color="primary"
            texture="solid"
          />
        </>
      )}
    </Container>
  );
};
 
export default ProviderActionsHandler;