All files / src/components/common/DraggableModal Header.tsx

84.61% Statements 11/13
81.81% Branches 9/11
75% Functions 6/8
84.61% Lines 11/13

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                                                                            21x                                         34x   34x                   1x           8x         34x                                                   2x 2x                                                                   102x   101x                                             21x      
import {
  ArrowVertical,
  CloseConversationIcon,
  SearchConversationIcon,
} from "@assets/icons";
import Placeholder from "@assets/images/Placeholder.png";
import { Image } from "@common/StyledImage/Image";
import { TruncateText } from "@common/Text";
import { addSizeToImage } from "@components/UploadAvatar/UploadAvatar";
import { Box, Stack, SvgIconProps, SxProps } from "@mui/material";
import React, { RefObject } from "react";
 
type HeaderIconType = {
  Component: React.ElementType<SvgIconProps>;
  action: () => unknown;
  name: string;
  hidden?: boolean;
};
 
export type HeaderProps = {
  SearchElement?: React.ReactElement;
  isMobileView?: boolean;
  handleExpandModal: () => void;
  searchIconAction?: () => void;
  handleOpenModal: (open?: boolean) => void;
  ref?: RefObject<HTMLDivElement>;
  backButtonEnabled?: boolean;
  handleBackClick?: () => void;
  imageSx?: SxProps;
  image?: string;
  showImage?: boolean;
  headerTestId?: string;
  title: string;
  Breadcrumbs?: React.ReactElement;
  testIdBase?: string;
  hideTitle?: boolean;
};
 
const Header = React.forwardRef<HTMLDivElement, HeaderProps>(
  (
    {
      SearchElement,
      isMobileView,
      handleExpandModal,
      searchIconAction,
      handleOpenModal,
      backButtonEnabled,
      handleBackClick,
      imageSx,
      image,
      showImage,
      headerTestId,
      title,
      Breadcrumbs,
      testIdBase,
      hideTitle,
    },
    ref,
  ) => {
    const sizedImage = image ? addSizeToImage(image, "small") : Placeholder;
 
    const headerIconMapper: Array<HeaderIconType> = [
      {
        Component: SearchConversationIcon,
        action: searchIconAction ? searchIconAction : () => null,
        name: "search",
        hidden: !searchIconAction,
      },
 
      {
        Component: ArrowVertical,
        action: () => handleExpandModal(),
        name: "expand",
        hidden: isMobileView,
      },
      {
        Component: CloseConversationIcon,
        action: () => handleOpenModal(false),
        name: "close",
      },
    ];
 
    return (
      <Box>
        <Stack
          ref={ref}
          direction="row"
          sx={{
            padding: "12px",
            cursor: "grab",
            transition: "box-shadow 150ms ease-in",
            "&.conversationsBarActive": {
              borderBottom: "1px solid var(--Neutral-White, #FFF)",
              background: "rgba(251, 251, 251, 0.90)",
              boxShadow: "0px -2px 10px 0px rgba(76, 76, 76, 0.10)",
              backdropFilter: "blur(10px)",
            },
          }}
          justifyContent="space-between"
          alignItems={"center"}
          id="handle"
        >
          <Stack
            direction={"row"}
            alignItems="center"
            gap={1}
            id="imageSection"
            onClick={(e) => {
              e.stopPropagation();
              backButtonEnabled && handleBackClick && handleBackClick();
            }}
            sx={imageSx}
          >
            {!hideTitle ? (
              <>
                {showImage && (
                  <Image
                    draggable={false}
                    src={sizedImage}
                    width={24}
                    height={24}
                  />
                )}
                <TruncateText
                  fontSize="14px"
                  lineClamp={1}
                  sx={{ fontWeight: 400 }}
                  data-testid={headerTestId}
                >
                  {title}
                </TruncateText>
              </>
            ) : (
              <>{Breadcrumbs}</>
            )}
          </Stack>
          <Stack
            direction={"row"}
            alignItems={"center"}
            gap={1}
            id="actionSection"
          >
            {headerIconMapper
              .filter((item) => !item.hidden)
              .map(({ Component, action, name }: HeaderIconType, index) => {
                return (
                  <Box
                    key={index}
                    onClick={action}
                    sx={{
                      cursor: "pointer",
                      ":hover svg path": {
                        fill: "#6D9CF8",
                      },
                    }}
                    data-testid={`${testIdBase}-header-${name}-action`}
                  >
                    <Component onMouseEnter={(e) => undefined} />
                  </Box>
                );
              })}
          </Stack>
        </Stack>
        {SearchElement}
      </Box>
    );
  },
);
Header.displayName = "Header";
 
export default Header;