All files / src/features/Events/EventDetail/hooks useEventListActions.ts

100% Statements 9/9
100% Branches 6/6
100% Functions 2/2
100% Lines 8/8

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              9x                                     9x 34x       34x               34x   34x   34x                                   34x    
import { ExportIcon, FunnelSimpleIcon, Icon } from "@phosphor-icons/react";
import { ContextualMenuOptionProps } from "@shared/ContextualMenu/ContextualMenu.types";
import { FilterPagesEnum } from "@shared/GiveFilter/constants";
import { useFilterButton } from "@shared/GiveFilter/hooks/useFilterButton";
import { EVENT_DETAIL_TABS, EventDetailTab } from "../constants";
import { useTicketsExport } from "../tickets/useTicketsExport";
 
export const EXPORT_MENU_LABEL = "Export";
 
type Args = {
  eventId?: string;
  tab: EventDetailTab;
  /**
   * Where the glyphs sit. Shared so the compact banner's menu and the phone
   * toolbar's sheet cannot drift apart on which controls they offer, while each
   * keeps the placement its own frame draws.
   */
  withIcon: (Glyph: Icon) => { IconLeft: Icon } | { IconRight: Icon };
  /** Wraps each action, e.g. to close the sheet before it runs. */
  wrap?: (action: () => void) => () => void;
};
 
/**
 * The active tab's list controls — Filter, plus Export on Tickets — as menu
 * rows, for the collapsed affordances that stand in for the full toolbar.
 */
export const useEventListActions = ({ eventId, tab, withIcon, wrap }: Args) => {
  const isTicketsTab = tab === EVENT_DETAIL_TABS.TICKETS;
 
  // One call with the page id switched by tab: both panels are the shared
  // `GiveFilterModal`, and only the active tab's list can be filtered from here.
  const { handleOpenFilterModal, filtersAmount, filterLabel } = useFilterButton(
    {
      filterPage: isTicketsTab
        ? FilterPagesEnum.EVENT_TICKETS
        : FilterPagesEnum.EVENT_TRANSACTIONS,
      productId: eventId,
    },
  );
  const { exportTickets, isExporting } = useTicketsExport(eventId);
 
  const run = wrap ?? ((action: () => void) => action);
 
  const options: ContextualMenuOptionProps[] = [
    {
      text: filterLabel,
      ...withIcon(FunnelSimpleIcon),
      onClick: run(handleOpenFilterModal),
    },
    ...(isTicketsTab
      ? [
          {
            text: EXPORT_MENU_LABEL,
            ...withIcon(ExportIcon),
            disabled: isExporting,
            onClick: run(exportTickets),
          },
        ]
      : []),
  ];
 
  return { options, filtersAmount };
};