All files / src/components/Disputes/DisputeTable/Banner DisputeActions.tsx

60% Statements 15/25
42.85% Branches 6/14
20% Functions 2/10
58.33% Lines 14/24

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                                                                        1x         1x         1x 15x 15x     15x       15x 15x   15x       15x   15x                                                   30x                 15x                                                               15x                                                              
import NiceModal from "@ebay/nice-modal-react";
import {
  DotsThreeVerticalIcon,
  ExportIcon,
  FunnelSimpleIcon,
  XIcon,
} from "@phosphor-icons/react";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import GiveMerchantTableActions from "@shared/MerchantsProviders/GiveMerchantTableActions";
import { GIVE_EXPORT_MODAL } from "modals/modal_names";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
import { useState } from "react";
import { useFilterButton } from "@shared/GiveFilter/hooks/useFilterButton";
import { FilterPagesEnum } from "@shared/GiveFilter/constants";
import { Container } from "@features/MerchantPortal/ManageMoney/ManageMoneyActions.atoms";
import GiveExpandingSearch from "@shared/SearchBar/GiveExpandingSearch";
import { Stack } from "@mui/material";
import { useAppSelector } from "@redux/hooks";
import { selectDisputeTab } from "@redux/slices/transactions";
import {
  tabNames,
  useDisputeTableNewTable,
} from "@components/Disputes/DisputesList/hooks/useDisputeTableNewTable";
 
type Props = {
  noTransactions: boolean;
  search: string;
  handleSearchChange: (value: string) => void;
  /**
   * Compact-on-scroll banner variant: collapses search + all actions
   * (filter, export, tab filters) into a search icon + kebab menu.
   */
  compact?: boolean;
};
 
export const ACTION_KEYS = {
  filter: "filter",
  export: "export",
} as const;
 
export const ACTIONS_ICONS = {
  [ACTION_KEYS.filter]: FunnelSimpleIcon,
  [ACTION_KEYS.export]: ExportIcon,
};
 
const DisputeActions = ({ handleSearchChange, search, compact }: Props) => {
  const { isNarrowView: showMobileActions } = useCustomThemeV2();
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
 
  const { handleOpenFilterModal, filtersAmount, handleResetFilters } =
    useFilterButton({
      filterPage: FilterPagesEnum.ACQUIRER_DISPUTES,
    });
 
  const { tableFilters } = useDisputeTableNewTable();
  const tab = useAppSelector(selectDisputeTab);
 
  const handleExport = () => {
    NiceModal.show(GIVE_EXPORT_MODAL);
  };
 
  const filterLabel = filtersAmount ? `Filters (${filtersAmount})` : "Filter";
 
  const actions = [
    {
      id: ACTION_KEYS.filter,
      label: filterLabel,
      icon: <ACTIONS_ICONS.filter />,
      handleAction: handleOpenFilterModal,
      disabled: false,
      ...((filtersAmount || 0) > 0 && {
        endIcon: (
          <GiveIconButton
            Icon={XIcon}
            size="extraSmall"
            onClick={handleResetFilters}
          />
        ),
      }),
    },
    {
      id: ACTION_KEYS.export,
      label: "Export",
      icon: <ACTIONS_ICONS.export />,
      handleAction: handleExport,
      disabled: false,
    },
  ];
 
  const mobileActions = actions.map((action) => ({
    text: action.label,
    IconRight: ACTIONS_ICONS[action.id],
    onClick: () => {
      action.handleAction();
    },
    disabled: !!action.disabled,
  }));
 
  Iif (compact) {
    const compactMenuOptions = [
      ...mobileActions,
      ...(tableFilters.length > 0
        ? [{ type: "divider" as const, text: "" }]
        : []),
      ...tableFilters.map((filter) => ({
        text: filter.label,
        isSelected: (tab || tabNames.all) === filter.value,
        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={actions}
        isAllActionsHidden={showMobileActions}
        search={search}
        onSearchChange={handleSearchChange}
        searchOnEnter
      />
 
      {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 DisputeActions;