All files / src/features/Settlements/components SettlementsActions.tsx

68.18% Statements 15/22
47.05% Branches 8/17
33.33% Functions 3/9
68.18% Lines 15/22

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                                                                      2x 12x         12x 12x             12x 12x 7x     12x 2x     2x                       2x           2x                                                                             10x                                                   10x         20x                                
import { Stack } from "@mui/material";
import { useGetSettlementActions } from "../hooks/useGetSettlementActions";
import { TPage } from "../types";
import {
  cloneElement,
  Fragment,
  ReactElement,
  useEffect,
  useState,
} from "react";
import { CaretRightIcon, DotsThreeVerticalIcon } from "@phosphor-icons/react";
import { useAppSelector } from "@redux/hooks";
import { selectedQueries } from "@redux/slices/tableFilters";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import GiveDivider from "@shared/GiveDivider/GiveDivider";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import ContextualMenu from "@shared/ContextualMenu/ContextualMenu";
import {
  DesktopContainer,
  FullWidthWrapper,
  MobileContainer,
  MobileRightActionsContainer,
  ScrollableRow,
} from "../styles";
 
type Props = {
  page: TPage;
  keepState?: boolean;
  disabled?: boolean;
  /**
   * Renders the filter controls in a single tight row for the compact
   * (on-scroll) banner. See TRA013 AC004.
   */
  compact?: boolean;
};
const SettlementsActions = ({ page, keepState, disabled, compact }: Props) => {
  const { leftActions, rightActions } = useGetSettlementActions({
    page,
    keepState,
    disabled,
  });
  const { isWideView, isMobileView } = useCustomThemeV2();
  const [compactAnchorEl, setCompactAnchorEl] = useState<null | HTMLElement>(
    null,
  );
 
  // Close the compact kebab when a filter changes: selecting a filter can
  // reset the list/scroll and hide the mini banner, otherwise the menu would
  // linger (portaled) after the bar slides away. See TRA013 AC004.
  const contextQueries = useAppSelector(selectedQueries);
  useEffect(() => {
    setCompactAnchorEl(null);
  }, [contextQueries]);
 
  if (compact) {
    const actions = [...leftActions, ...rightActions];
    // Render each filter as a full-width menu row: label left, ">" right, no
    // pill background. The filter's own submenu opens from the row.
    const rowButtonProps = {
      variant: "ghost" as const,
      endIcon: <CaretRightIcon size={16} />,
      sx: {
        width: "100%",
        height: "44px",
        px: "12px",
        justifyContent: "space-between",
        borderRadius: "8px",
      },
    };
    const kebab = (
      <GiveIconButton
        Icon={DotsThreeVerticalIcon}
        variant="ghost"
        onClick={(e) => setCompactAnchorEl(e.currentTarget)}
      />
    );
    return (
      <>
        {/* On the mobile top bar the compact head has no search, so the compact
            banner stretches this actions node full-width; right-align the kebab
            so it stays on the right instead of stretching into the middle. */}
        {isMobileView ? (
          <Stack
            direction="row"
            alignItems="center"
            justifyContent="flex-end"
            sx={{ width: "100%" }}
          >
            {kebab}
          </Stack>
        ) : (
          kebab
        )}
        <ContextualMenu
          anchorEl={compactAnchorEl}
          handleClose={() => setCompactAnchorEl(null)}
          color="primary"
          texture="solid"
          horizontalOrigin="right"
          options={[]}
          customContent={
            <Stack gap="2px" sx={{ p: "8px", minWidth: 240 }}>
              {actions.map((action) =>
                cloneElement(action.component as ReactElement<any>, {
                  key: action.id,
                  buttonProps: rowButtonProps,
                }),
              )}
            </Stack>
          }
        />
      </>
    );
  }
 
  Iif (!isWideView) {
    return (
      <MobileContainer>
        {leftActions.length > 0 && (
          <ScrollableRow>
            {leftActions.map((action) => (
              <Fragment key={action.id}>{action.component}</Fragment>
            ))}
          </ScrollableRow>
        )}
        {leftActions.length > 0 && rightActions.length > 0 && (
          <GiveDivider sx={{ margin: "20px 0" }} />
        )}
        {rightActions.length > 0 && (
          <MobileRightActionsContainer>
            {rightActions.map((action) => (
              <FullWidthWrapper key={action.id}>
                {action.component}
              </FullWidthWrapper>
            ))}
          </MobileRightActionsContainer>
        )}
      </MobileContainer>
    );
  }
 
  return (
    <DesktopContainer>
      {leftActions.length > 0 && (
        <Stack direction="row" gap="20px">
          {leftActions.map((action) => (
            <Fragment key={action.id}>{action.component}</Fragment>
          ))}
        </Stack>
      )}
      {rightActions.length > 0 && (
        <Stack direction="row" gap="20px">
          {rightActions.map((action) => (
            <Fragment key={action.id}>{action.component}</Fragment>
          ))}
        </Stack>
      )}
    </DesktopContainer>
  );
};
 
export default SettlementsActions;