All files / src/features/TransactionPanel/components/Summary TransactionSummaryActions.tsx

86.36% Statements 19/22
66.66% Branches 20/30
57.14% Functions 4/7
89.47% Lines 17/19

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                                                          52x 80x 80x 80x 80x   80x           80x       80x   76x   76x   76x                 532x 211x                                                                                                 52x               52x 356x               76x                          
import { ACTION_DENY_MESSAGE } from "@constants/permissions";
import { Stack } from "@mui/material";
import { DotsThreeIcon } from "@phosphor-icons/react";
import GiveButton from "@shared/Button/GiveButton";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
import GiveLink from "@shared/Link/GiveLink";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { CustomTheme } from "@theme/v2/palette.interface";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { useTransactionActions } from "features/TransactionPanel/hooks/useTransactionActions";
import { useState } from "react";
 
type Props = {
  data: any; // TODO: add the type
  setSelectedRow?: (newIdx: string | number) => void;
  isFirst?: boolean;
  isLast?: boolean;
  setIsCustomerPanelOpen: (isOpen: boolean) => void;
  chooseTransaction?: (
    loading: boolean,
  ) => ((prevWasLoading: boolean, data: any) => void) | undefined;
  isMarkedAsFraud?: boolean;
  isFalsePositive?: boolean;
  customActions?: any[];
  isTopCard?: boolean;
  noOptions?: boolean;
};
 
const TransactionSummaryActions = (props: Props) => {
  const theme = useAppTheme();
  const { isMobileView } = useCustomThemeV2();
  const { actions, isAllActionsHidden } = useTransactionActions(props);
  const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
 
  const handleOpenMenu = (
    e: React.MouseEvent<HTMLButtonElement, MouseEvent>,
  ) => {
    setAnchorEl(e.currentTarget);
  };
 
  const handleCloseMenu = () => {
    setAnchorEl(null);
  };
 
  if (isAllActionsHidden && !props?.customActions) return null;
 
  const usedActions = props?.customActions ?? actions;
 
  const hideOptions = props?.noOptions && usedActions?.length === 1;
 
  return (
    <ActionsContainer
      theme={theme}
      noBorderAndPadding={isMobileView || !!props?.isTopCard}
      sx={{ ...(!isMobileView && { gap: "16px" }) }}
    >
      {!isMobileView &&
        usedActions.map(
          ({ text, IconRight, hidden, onClick, disabled, tooltip }: any) => {
            if (hidden) return null;
            return (
              <GiveTooltip
                key={text}
                title={tooltip || ACTION_DENY_MESSAGE}
                color="default"
                width="compact"
                disableHoverListener={!disabled}
              >
                <GiveLink
                  data-testid={`action-${text}-button`}
                  component="button"
                  Icon={IconRight}
                  color={theme?.palette?.primitive?.blue?.[100]}
                  onClick={onClick}
                  disabled={disabled}
                >
                  {text}
                </GiveLink>
              </GiveTooltip>
            );
          },
        )}
 
      {isMobileView && (
        <>
          <OptionsButton
            label={!hideOptions ? "Options" : usedActions?.[0]?.text}
            variant="filled"
            size="large"
            startIcon={!hideOptions && <DotsThreeIcon />}
            onClick={!hideOptions ? handleOpenMenu : usedActions?.[0]?.onClick}
          />
          <ContextualMenu
            anchorEl={anchorEl}
            color="primary"
            texture="solid"
            horizontalOrigin="left"
            options={usedActions}
            sx={{
              zIndex: 10000,
            }}
            handleClose={handleCloseMenu}
          />
        </>
      )}
    </ActionsContainer>
  );
};
 
const OptionsButton = styled(GiveButton)(() => ({
  position: "fixed",
  bottom: 20,
  left: 20,
  width: "calc(100% - 40px)",
  zIndex: 10,
}));
 
const ActionsContainer = styled(Stack, {
  shouldForwardProp: (prop) => prop !== "noBorderAndPadding",
})(
  ({
    theme,
    noBorderAndPadding,
  }: {
    theme: CustomTheme;
    noBorderAndPadding: boolean;
  }) => ({
    flexDirection: "row",
    spacing: "20px",
    marginBottom: 0,
    "& div": { width: "fit-content" },
    borderBottom: noBorderAndPadding
      ? "none"
      : `1px solid ${theme?.palette?.border?.primary}`,
    padding: noBorderAndPadding ? "0" : "20px 0",
  }),
);
 
export default TransactionSummaryActions;