All files / src/shared/SidePanel/components PanelWrapper.tsx

93.93% Statements 31/33
86.36% Branches 19/22
80% Functions 8/10
93.54% Lines 29/31

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                            143x                                         143x                   1997x 1997x           1997x   1997x 1997x           1997x       1997x   1997x         48x 48x 43x       1997x 48x 17x   31x       1997x 718x     1997x 343x 50x 50x   293x 144x     293x     1997x 62x                                                           1935x                                                                        
import { Drawer, SwipeableDrawer } from "@mui/material";
import { IGiveSidePanelProps } from "@shared/SidePanel/GiveSidePanel";
import {
  Dispatch,
  ReactNode,
  SetStateAction,
  useEffect,
  useState,
} from "react";
import { useAppTheme } from "@theme/v2/Provider";
import { getDrawerV2DefaultProps } from "@theme/v2/customStyles/customDrawerStyle";
import { useBannerOffset } from "@hooks/common/useBannerOffset";
 
/** `GiveMotionWrapper`'s slide duration for the second panel, in ms. */
const SECOND_PANEL_SLIDE_DURATION = 195;
 
interface PanelWrapperProps {
  isMobileView: boolean;
  isDoublePanelMode: boolean;
  isSinglePanelOnly: boolean;
  /** True whenever the second panel holds the paper — beside the first panel or over it. */
  isSecondPanelVisible: boolean;
  width: number;
  secondPanelWidth: number;
  children?: ReactNode;
  drawerProps: Omit<
    IGiveSidePanelProps,
    | "width"
    | "isSecondPanelOpen"
    | "containerProps"
    | "children"
    | "secondPanel"
  >;
}
 
const PanelWrapper = ({
  children,
  isMobileView,
  isDoublePanelMode,
  isSecondPanelVisible,
  width,
  secondPanelWidth,
  drawerProps,
  isSinglePanelOnly,
}: PanelWrapperProps) => {
  const [isMainTransitionComplete, setMainTransitionStatus] = useState(false);
  const [isDesktopOpen, setDesktopOpen] = useState(false);
  // The paper is only ever as wide as what it actually shows. A panel whose
  // second panel is wider than its first would otherwise keep that difference
  // as empty paper down the right of the first panel. Held through the second panel's
  // slide-out so the outgoing panel is not clipped mid-animation.
  const [holdsSecondPanelWidth, setHoldsSecondPanelWidth] =
    useState(isSecondPanelVisible);
 
  const theme = useAppTheme();
  const bannerOffset = useBannerOffset(true);
  const {
    componentsProps = {},
    PaperProps = {},
    mobileStyle,
    ...rest
  } = drawerProps || {};
  const {
    componentsProps: defaultComponentsProps,
    PaperProps: defaultPaperProps,
  } = getDrawerV2DefaultProps(theme.palette.mode);
 
  const delaySecondDispatch = (
    firstDispatch: Dispatch<SetStateAction<boolean>>,
    secondDispatch: Dispatch<SetStateAction<boolean>>,
    value: boolean,
  ) => {
    firstDispatch(value);
    setTimeout(() => {
      secondDispatch(value);
    }, 300);
  };
 
  const toggleDesktopDrawer = (open?: boolean) => {
    if (open) {
      delaySecondDispatch(setDesktopOpen, setMainTransitionStatus, true);
    } else {
      delaySecondDispatch(setMainTransitionStatus, setDesktopOpen, false);
    }
  };
 
  useEffect(() => {
    if (!isSinglePanelOnly) toggleDesktopDrawer(drawerProps.open);
  }, [drawerProps.open, isMobileView]);
 
  useEffect(() => {
    if (isSecondPanelVisible) {
      setHoldsSecondPanelWidth(true);
      return;
    }
    const timeoutId = setTimeout(
      () => setHoldsSecondPanelWidth(false),
      SECOND_PANEL_SLIDE_DURATION,
    );
    return () => clearTimeout(timeoutId);
  }, [isSecondPanelVisible]);
 
  if (isMobileView) {
    return (
      <SwipeableDrawer
        disableDiscovery
        open={drawerProps.open}
        onClose={(e: any) =>
          drawerProps.onClose && drawerProps.onClose(e, "backdropClick")
        }
        onOpen={() => null}
        anchor="bottom"
        transitionDuration={300}
        sx={{
          "& .MuiDrawer-paper": {
            width: "100%",
            height: "96%",
            "&::-webkit-scrollbar": {
              width: "6px",
            },
            "&::-webkit-scrollbar-thumb": {
              backgroundColor: theme.palette.primitive?.neutral[110],
              borderRadius: "10px",
            },
          },
          ...mobileStyle,
        }}
      >
        {children}
      </SwipeableDrawer>
    );
  }
 
  return (
    <Drawer
      open={isSinglePanelOnly ? drawerProps.open : isDesktopOpen}
      PaperProps={{
        sx: {
          flexDirection: "row",
          width: isDoublePanelMode
            ? width + secondPanelWidth
            : holdsSecondPanelWidth
            ? secondPanelWidth
            : width,
          ...(isMainTransitionComplete
            ? { transitionProperty: "transform, width !important" }
            : {}),
          ...defaultPaperProps.sx,
        },
        ...PaperProps,
      }}
      type="sidepanel"
      bannerOffset={bannerOffset}
      anchor="right"
      componentsProps={{
        ...defaultComponentsProps,
        ...componentsProps,
      }}
      SlideProps={{
        easing: "ease-in",
        timeout: 195,
      }}
      {...rest}
    >
      {children}
    </Drawer>
  );
};
export default PanelWrapper;