All files / src/components/Settings/Shared SettingsLayout.tsx

72.97% Statements 27/37
51.35% Branches 19/37
75% Functions 3/4
78.78% Lines 26/33

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                                          2x               23x 23x 23x 23x   23x 23x 23x   23x   23x 23x   1x   22x                                 23x   23x   23x   23x                   23x 1x               1x       23x 23x   23x             23x     23x                       23x                                                                                                                  
import { Box, Stack } from "@mui/material";
import { Outlet, useNavigate, useLocation } from "react-router-dom";
import { useEffect, Suspense } from "react";
import { useTranslation } from "react-i18next";
import { namespaces } from "@localization/resources/i18n.constants";
import { StyledTitle } from "@shared/Banner/styles";
import GiveBreadcrumb from "@shared/Breadcrumbs/GiveBreadcrumb";
import Menu from "@components/Layout/Menu/Menu";
import LoadingSpinner from "@components/Snipper/LoadingSpinner";
import useThemeSettings from "@components/EnterpriseSettings/Branding/hooks/useThemeSettings";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { useAppTheme } from "@theme/v2/Provider";
import { useSettingsBreadcrumbs } from "@hooks/useSettingsBreadcrumbs";
import { useMasqueradeRedirect } from "@hooks/common";
import merchantNestedRoutes from "@pages/Settings/nestedRoutes";
import enterpriseNestedRoutes from "@pages/EnterprisePortal/Settings/nestedRoutes";
import { useNestedRoutes as useAcquirerNestedRoutes } from "@pages/AcquirerPortal/AcquirerSettings/nestedRoutes";
import { SettingsLayoutProps } from "./SettingsLayout.types";
import GiveMerchantInfoTabSkeleton from "@components/CustomSkeletons/GiveMerchantInfoTabSkeleton";
import EnterpriseConfigurationLoadingSkeleton from "@components/EnterpriseSettings/BusinessDetails/Configuration/EnterpriseConfigurationLoadingSkeleton";
 
export const SettingsLayout = ({
  base,
  specialRender,
  redirectLogic,
  skipLayoutCondition,
  bannerOffset,
  excludeSegments = [],
}: SettingsLayoutProps) => {
  const theme = useAppTheme();
  const navigate = useNavigate();
  const { state } = useLocation();
  const { t } = useTranslation(namespaces.pages.settings);
 
  const { textColor } = useThemeSettings();
  const { isTabletView, isMobileView } = useCustomThemeV2();
  const isTabletOrMobile = isTabletView || isMobileView;
 
  const acquirerRoutes = useAcquirerNestedRoutes();
 
  const nestedRoutes = (() => {
    switch (base) {
      case "merchant":
        return merchantNestedRoutes;
      case "provider":
        return enterpriseNestedRoutes;
      case "acquirer":
        return acquirerRoutes;
      default:
        return merchantNestedRoutes;
    }
  })();
 
  const {
    isSettingsBasePage,
    breadcrumbItems,
    breadcrumbTitle,
    lastSegment,
    isPermissions,
    isLegalDocumentDetail,
    isBusinessDetailsTab,
    isRevanueTab,
  } = useSettingsBreadcrumbs({ base, nestedRoutes });
 
  useMasqueradeRedirect();
 
  const isProvider = base === "provider";
 
  const LoadingSkeleton = () => {
    if (isBusinessDetailsTab)
      return (
        <GiveMerchantInfoTabSkeleton isProvider={isProvider} showWithTabs />
      );
    if (isRevanueTab) return <EnterpriseConfigurationLoadingSkeleton />;
    return <LoadingSpinner />;
  };
 
  // --- special render (like WelcomePage) ---
  if (specialRender) {
    const content = specialRender({
      state,
      isSettingsBasePage,
      isPermissions,
      isLegalDocumentDetail,
      lastSegment,
      isTabletOrMobile,
    });
    Iif (content) return content;
  }
 
  // --- redirect logic ---
  useEffect(() => {
    Iif (!redirectLogic) return;
 
    const redirect = redirectLogic({
      state,
      isSettingsBasePage,
      isPermissions,
      isLegalDocumentDetail,
      isTabletOrMobile,
    });
    if (redirect?.path) navigate(redirect.path, { replace: true });
  }, [navigate, isTabletOrMobile]);
  // --- skip layout pages ---
  Iif (skipLayoutCondition?.({ isPermissions, isLegalDocumentDetail })) {
    return (
      <Box width="100%">
        {/* The loading state is handled in the components, not here */}
        <Suspense fallback={<LoadingSpinner />}>
          <Outlet />
        </Suspense>
      </Box>
    );
  }
 
  // --- main layout ---
  return (
    <Box
      display="flex"
      flexDirection="column"
      gap={isTabletOrMobile ? "10px" : 2}
      sx={{
        paddingTop:
          isTabletOrMobile || !bannerOffset ? "24px" : `${bannerOffset}px`,
      }}
    >
      <Stack gap="40px">
        {isTabletOrMobile ? (
          <>
            {!isSettingsBasePage && (
              <GiveBreadcrumb
                items={breadcrumbItems}
                customStyle={{
                  activeColor: "primary",
                  defaultColor: theme.palette.text.secondary,
                }}
              />
            )}
            {!excludeSegments.includes(lastSegment) && (
              <StyledTitle theme={theme} textColor={textColor} variant="h2">
                {breadcrumbTitle}
              </StyledTitle>
            )}
          </>
        ) : (
          <StyledTitle
            sx={{ position: "fixed" }}
            variant="h2"
            theme={theme}
            textColor={textColor}
          >
            {t("settings")}
          </StyledTitle>
        )}
      </Stack>
 
      <Stack
        direction={!isTabletOrMobile ? "row" : "column"}
        gap={3}
        alignItems="flex-start"
        marginTop={isTabletOrMobile ? 0 : "52px"}
      >
        <Menu menu={nestedRoutes} />
        <Box width="100%">
          {/* The loading state is handled in the components, not here */}
          <Suspense fallback={<LoadingSkeleton />}>
            <Outlet />
          </Suspense>
        </Box>
      </Stack>
    </Box>
  );
};