All files / src/features/Merchants/MerchantSidePanel/components/Collapse1stLevel CollapseNavigation.tsx

98.33% Statements 59/60
92.68% Branches 38/41
100% Functions 9/9
98.3% Lines 58/59

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202                      53x 53x                 53x             23x   23x                             23x     23x   23x 3x 3x     23x   2x 1x     2x 2x 2x           23x     3x       2x 2x       1x 1x 1x   1x 1x 1x             23x 5x   2x 1x 1x   1x   2x       3x 1x 1x   1x 1x 1x   1x       2x 1x 1x       1x     23x 2x     23x 12x   10x 3x     7x 5x     2x 2x       23x                                                                       53x 98x 20x                              
import { Stack } from "@mui/material";
import { styled, useAppTheme } from "@theme/v2/Provider";
import { ArrowLineLeftIcon } from "@phosphor-icons/react";
import { NEW_BASE_PANEL_WIDTH } from "@shared/SidePanel/GiveSidePanel";
import GiveAvatar from "@shared/Avatar/GiveAvatar";
import GiveMotionWrapper from "@shared/Motion/GiveMotionWrapper";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import { useMerchantSidePanelContext } from "../../Provider/MerchantSidePanelProvider";
import { getIconType, ThumbnailType } from "@shared/Thumbnail/getIconType";
import { ReactNode } from "react";
 
const NAVIGATION_ELEMENT_WIDTH = 64;
const NAVIGATION_ELEMENT_HEIGHT = 110;
 
type Props = {
  show: boolean;
  image?: string;
  navigationOffSet?: number;
  onBack?: () => void;
  placeholderType?: ThumbnailType;
};
const CollapseNavigation = ({
  image,
  show,
  navigationOffSet = NEW_BASE_PANEL_WIDTH,
  onBack,
  placeholderType,
}: Props) => {
  const theme = useAppTheme();
 
  const Icon = getIconType({
    size: "small",
    type: placeholderType ?? "merchant",
  }) as ReactNode;
  const {
    contentHistory,
    firstPanelContent,
    setContentHistory,
    setFirstPanelContent,
    secondPanelContent,
    secondPanelContentHistory,
    isSinglePanelSize,
    setSecondPanelContent,
    setSecondPanelContentHistory,
    handleSetProviderID,
  } = useMerchantSidePanelContext();
 
  const mainContentHistory =
    contentHistory === "default" ? "" : contentHistory || "";
 
  const restoreFirstPanelFromHistory = () => {
    setFirstPanelContent(mainContentHistory);
    setContentHistory("");
  };
 
  const handleTransactionWithProviderMerchant = () => {
    // Clear providerID if we're leaving provider view
    if (secondPanelContent === "provider") {
      handleSetProviderID(null);
    }
    // Move transaction to second panel
    setSecondPanelContent("transaction");
    setFirstPanelContent("");
    setContentHistory("");
    // Keep secondPanelContentHistory (e.g., "transactionRiskProfile") so we can restore it
    // when pressing back from transaction
    // Don't clear it here
  };
 
  const handleBackFromBothPanels = () => {
    // Special case: if first panel is "transaction" and second is "provider"/"merchant",
    // move transaction to second panel and clear first panel
    if (
      firstPanelContent === "transaction" &&
      ["provider", "merchant"].includes(secondPanelContent)
    ) {
      handleTransactionWithProviderMerchant();
      return;
    }
 
    // If we have history for second panel, restore it
    Eif (secondPanelContentHistory) {
      setSecondPanelContent(secondPanelContentHistory);
      setSecondPanelContentHistory("");
      // Restore first panel from contentHistory
      setFirstPanelContent(mainContentHistory);
      setContentHistory("");
      return;
    }
 
    // Otherwise, just clear second panel and keep first panel
    setSecondPanelContent("");
  };
 
  const handleBackFromSecondPanel = () => {
    if (isSinglePanelSize) {
      // In single panel mode, restore history or clear
      if (secondPanelContentHistory) {
        setSecondPanelContent(secondPanelContentHistory);
        setSecondPanelContentHistory("");
      } else {
        setSecondPanelContent("");
      }
      return;
    }
 
    // If we have history, restore it
    if (secondPanelContentHistory) {
      setSecondPanelContent(secondPanelContentHistory);
      setSecondPanelContentHistory("");
      // Restore first panel if we have contentHistory
      Eif (contentHistory) {
        setFirstPanelContent(mainContentHistory);
        setContentHistory("");
      }
      return;
    }
 
    // If no first panel content, restore it from history
    if (!firstPanelContent && contentHistory) {
      restoreFirstPanelFromHistory();
      return;
    }
 
    // Otherwise, just clear second panel
    setSecondPanelContent("");
  };
 
  const handleBackFromFirstPanel = () => {
    restoreFirstPanelFromHistory();
  };
 
  const handleBack = () => {
    if (onBack) return onBack();
 
    if (secondPanelContent && firstPanelContent) {
      return handleBackFromBothPanels();
    }
 
    if (secondPanelContent) {
      return handleBackFromSecondPanel();
    }
 
    Eif (firstPanelContent) {
      return handleBackFromFirstPanel();
    }
  };
 
  return (
    <GiveMotionWrapper
      type="slide"
      inView={show}
      direction={"down"}
      delay={350}
      duration={350}
    >
      <StyledContainer navigationOffSet={navigationOffSet}>
        <GiveIconButton
          data-testid="panel1-minimized-image"
          Icon={ArrowLineLeftIcon}
          onClick={handleBack}
          sx={{
            backgroundColor: theme.palette.surface?.secondary,
            "&:hover": {
              backgroundColor: theme.palette.primitive?.transparent["darken-5"],
            },
          }}
        />
        {image ? (
          <GiveAvatar size="32px" shape="square" imageUrl={image} />
        ) : placeholderType ? (
          Icon
        ) : (
          <></>
        )}
      </StyledContainer>
    </GiveMotionWrapper>
  );
};
 
interface StyledContainerProps {
  navigationOffSet: number;
}
 
const StyledContainer = styled(Stack, {
  shouldForwardProp: (prop) => prop !== "navigationOffSet",
})<StyledContainerProps>(({ theme, navigationOffSet }) => ({
  width: `${NAVIGATION_ELEMENT_WIDTH}`,
  height: `${NAVIGATION_ELEMENT_HEIGHT}`,
  borderBottomLeftRadius: theme.customs?.radius.medium,
  padding: "16px",
  position: "fixed",
  left: `calc(100% - ${navigationOffSet + NAVIGATION_ELEMENT_WIDTH}px)`,
  boxShadow: "initial",
  backgroundColor: theme.palette.surface?.secondary,
  gap: "16px",
  alignItems: "center",
  zIndex: -1,
}));
 
export default CollapseNavigation;