All files / src/features/Merchants/MerchantSidePanel/hooks useGetContentDetails.tsx

27.02% Statements 10/37
16% Branches 4/25
22.22% Functions 2/9
27.77% Lines 10/36

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              21x                             566x   566x                     566x       566x                   566x 566x   3x                                                                                                         563x                       566x        
import { useCallback } from "react";
import { ChallengesSecondPanelWrapper } from "../WithRepository/Challenges/ChallengesSecondPanel";
import { useMerchantSidePanelContext } from "../Provider/MerchantSidePanelProvider";
import TransactionPanelContent from "@features/TransactionPanel/components/Summary/TransactionPanelContent";
import FirstPanelContent from "../FirstPanelContent";
 
// This hook can be used to get the content details for the first panel
const useGetContentDetails = () => {
  const {
    openChallenge,
    firstPanelContent,
    setFirstPanelContent,
    setContentHistory,
    handleCloseAll,
    isSinglePanelSize,
    secondPanelContent,
    openTransaction,
    setOpenCustomer,
    setSecondPanelContent,
    handleSetProviderID,
    setSecondPanelContentHistory,
    secondPanelContentHistory,
  } = useMerchantSidePanelContext();
 
  const handleClose = () => {
    // Here we are storing the current first panel content in the history
    // so that we can use it to navigate back to the same panel
 
    if (isSinglePanelSize || !secondPanelContent) {
      return handleCloseAll();
    }
    setContentHistory(firstPanelContent);
    setFirstPanelContent("");
  };
 
  const handleBack = () => {
    setFirstPanelContent("");
  };
 
  const handleRestore = () => {
    // If closing customer, restore from history
    if (secondPanelContentHistory) {
      setSecondPanelContent(secondPanelContentHistory);
      setSecondPanelContentHistory("");
    } else {
      setSecondPanelContent("");
    }
  };
 
  const getContentDetails = useCallback(() => {
    switch (firstPanelContent) {
      case "challenges":
        return (
          <ChallengesSecondPanelWrapper
            handleClose={handleClose}
            handleBack={handleBack}
            challengeIdentifier={openChallenge?.identifier || ""}
            challengeDisplayName={openChallenge?.displayName || ""}
          />
        );
      case "transaction":
        return (
          <TransactionPanelContent
            data={openTransaction!}
            handleCustomer={(open, customerData) => {
              if (open) {
                setOpenCustomer(customerData);
                // Save transactionRiskProfile as history
                if (secondPanelContentHistory !== "transactionRiskProfile") {
                  setSecondPanelContentHistory("transactionRiskProfile");
                }
                setSecondPanelContent("customer");
              } else {
                handleRestore();
              }
            }}
            handleSidePanelOpen={() => {
              // Save transaction as history before opening risk profile
              setSecondPanelContentHistory("transaction");
              setSecondPanelContent("transactionRiskProfile");
            }}
            onClose={() => {
              handleRestore();
              setFirstPanelContent("");
            }}
            handleOpenMerchant={(type, id) => {
              // When opening merchant/provider from transaction in first panel,
              // we need to preserve "transactionRiskProfile" as history if we came from there
              // Check secondPanelContentHistory (which was set when opening transaction from risk profile)
              // or secondPanelContent (if transaction is still in second panel)
              if (!secondPanelContentHistory) {
                if (secondPanelContent === "transactionRiskProfile") {
                  setSecondPanelContentHistory("transactionRiskProfile");
                }
              }
              // Otherwise, keep the existing secondPanelContentHistory (e.g., "transactionRiskProfile")
              if (type === "provider") handleSetProviderID(Number(id));
              setSecondPanelContent(type);
            }}
          />
        );
      case "provider":
      case "merchant":
        return <FirstPanelContent />;
      default:
        return <></>;
    }
  }, [
    firstPanelContent,
    secondPanelContent,
    openTransaction,
    secondPanelContent,
    secondPanelContentHistory,
    handleRestore,
    handleClose,
  ]);
 
  return { contentDetails: getContentDetails() };
};
 
export default useGetContentDetails;