All files / src/features/Campaigns/CampaignDetails/hooks useCampaignSidePanel.tsx

69.56% Statements 16/23
22.22% Branches 2/9
60% Functions 3/5
69.56% Lines 16/23

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                2x 88x 88x 88x       88x   88x   88x 2x 2x 2x 2x     88x                     88x               88x 15x       88x                        
import { useMemo, useState } from "react";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import { MIN_ALLOWED_SCREEN_SIZE_FOR_DOUBLE_PANELS } from "@shared/SidePanel/GiveSidePanel";
import { useMediaQuery } from "@mui/material";
import { useHideAndRemoveModal } from "@shared/SidePanel/hooks/useHideAndRemoveModal";
 
type TCampaignDoublePanelNew = "transaction" | "edit" | "customer" | "";
 
const useCampaignSidePanel = () => {
  const { open, onClose, modal } = useNiceModal();
  const { hideAndRemove } = useHideAndRemoveModal(modal);
  const isSinglePanelSize = useMediaQuery(
    `(max-width:${MIN_ALLOWED_SCREEN_SIZE_FOR_DOUBLE_PANELS}px)`,
  );
 
  const [isFirstPannelCollapsed, setIsFirstPannelCollapsed] = useState(false);
  const [secondPanelContent, setSecondPanelContent] =
    useState<TCampaignDoublePanelNew>("");
 
  const handleCloseAll = () => {
    onClose();
    setIsFirstPannelCollapsed(false);
    setSecondPanelContent("");
    hideAndRemove();
  };
 
  const handleCloseFirst = (
    event?: any,
    reason?: "backdropClick" | "escapeKeyDown",
  ) => {
    if (secondPanelContent && !reason) {
      setIsFirstPannelCollapsed(true);
      return;
    }
    handleCloseAll();
  };
 
  const handleBack = () => {
    if (isSinglePanelSize) {
      setSecondPanelContent("");
    } else {
      setIsFirstPannelCollapsed(false);
    }
  };
 
  const isFirstPannelHidden = useMemo(
    () => !!secondPanelContent && (isSinglePanelSize || isFirstPannelCollapsed),
    [secondPanelContent, isSinglePanelSize, isFirstPannelCollapsed],
  );
 
  return {
    open,
    isFirstPannelHidden,
    secondPanelContent,
    setSecondPanelContent,
    handleBack,
    handleCloseFirst,
    handleCloseAll,
  };
};
 
export default useCampaignSidePanel;