All files / src/components/AvatarUpload AvatarUpload.tsx

73.07% Statements 19/26
80.48% Branches 33/41
88.88% Functions 8/9
73.91% Lines 17/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 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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198                                                                            274x           274x   274x       274x   274x                                     274x 4x 4x                 274x                                                                                                 270x               7x   1651x                   274x                                     274x                 7x 1103x 274x                                
import { EditIcon } from "@assets/icons/RebrandedIcons";
import NiceModal from "@ebay/nice-modal-react";
import { Box, CircularProgress, Stack, styled } from "@mui/material";
import { palette } from "@palette";
import { EDIT_AVATAR_MODAL } from "modals/modal_names";
import ImagePlaceholder from "@assets/images/avatar-placeholder.png";
import { TEditAvatarModalActions } from "./modal/EditAvatarModal";
import { IMAGE_UPLOAD_SIZE_LIMIT } from "@constants/constants";
import { showMessage } from "@common/Toast";
import { useFileUploadContext } from "@components/UploadFile/FileUploadContext";
import { FileUploadStatus } from "@components/UploadFile/types";
import { WithTooltipWrapper } from "@common/Menu/NewDropdownMenu";
import useFetchImage from "@hooks/useFetchImage";
import React from "react";
 
interface Props extends Partial<TEditAvatarModalActions> {
  placeholder?: string;
  defaultImageURL?: string;
  size?: number;
  rounded?: boolean;
  subTitle?: string;
  disabled?: boolean;
  tooltipMessage?: string;
}
 
function AvatarUpload({
  placeholder = ImagePlaceholder,
  defaultImageURL,
  size = 48,
  rounded,
  subTitle,
  onUpload,
  onDelete,
  canUpload,
  canDelete = true,
  disabled = false,
  tooltipMessage = "",
}: Props) {
  const actions = {
    onUpload,
    onDelete,
    canUpload,
    canDelete: canDelete && !!defaultImageURL,
  };
  const { setSnackbarFiles } = useFileUploadContext();
 
  const { data: imageURL, isFetching: isImageLoading } = useFetchImage(
    defaultImageURL ?? "",
  );
 
  const currentImage = imageURL || placeholder;
 
  const checkMaxSize = (file: File) => {
    const exceedsSize = file.size > IMAGE_UPLOAD_SIZE_LIMIT; //for upload Avatar we accept files <5mb
    const exceedsTitleLength = file.name.length > 254;
    if (exceedsSize) {
      setSnackbarFiles([
        {
          id: `${Date.now()}-${file.name}`,
          name: file.name,
          size: file.size,
          status: FileUploadStatus.FILE_TOO_LARGE,
          uploadProgress: 0,
          canBeDeleted: false,
        },
      ]);
    }
    if (exceedsTitleLength) showMessage("Error", "File name is too long");
    return exceedsSize || exceedsTitleLength;
  };
 
  const handleClick = (event: React.MouseEvent<HTMLDivElement>) => {
    event.stopPropagation();
    NiceModal.show(EDIT_AVATAR_MODAL, {
      defaultImageURL: currentImage,
      rounded,
      subTitle,
      actions,
      onLocalUpload: checkMaxSize,
    });
  };
 
  return (
    <WithTooltipWrapper
      hasTooltip={!!tooltipMessage}
      tooltipProps={{
        show: disabled,
        message: tooltipMessage,
        placement: "left",
      }}
    >
      <Main
        size={size}
        rounded={rounded}
        data-testid="form-avatar-upload-box"
        disabled={disabled || isImageLoading}
        isImageLoading={isImageLoading}
      >
        {isImageLoading ? (
          <CircularProgress
            sx={{
              color: palette.neutral.black,
              position: "absolute",
              top: "35%",
              left: "35%",
            }}
            size={24}
          />
        ) : (
          <AvatarImage
            src={isImageLoading ? placeholder : currentImage}
            alt="avatar_image"
          />
        )}
 
        <Overlay
          onClick={handleClick}
          rounded={rounded}
          disabled={disabled || isImageLoading}
        >
          <EditIconButton role="button">
            <EditIcon width={18} height={18} fill={palette.neutral.white} />
          </EditIconButton>
        </Overlay>
      </Main>
    </WithTooltipWrapper>
  );
}
 
export default AvatarUpload;
 
const AvatarImage = styled("img")(() => ({
  height: "100%",
  width: "100%",
  display: "block",
  borderRadius: "inherit",
  objectFit: "cover",
}));
 
const Main = styled(Box, {
  shouldForwardProp: (prop) =>
    prop !== "size" &&
    prop !== "rounded" &&
    prop !== "disabled" &&
    prop !== "isImageLoading",
})(
  (props: {
    size: number;
    rounded: boolean | undefined;
    disabled: boolean;
    isImageLoading?: boolean;
  }) => ({
    overflow: "hidden",
    position: "relative",
    transition: "border-radius .2s ease",
    height: `${props.size}px`,
    width: `${props.size}px`,
    borderRadius: props.rounded ? "50%" : "8px",
    ...(props?.isImageLoading && { backgroundColor: palette.neutral.white3 }),
    ...(!props.disabled && {
      "&:hover": {
        "& > div": {
          opacity: 1,
          borderColor: palette.gray[100],
        },
      },
    }),
  }),
);
 
const EditIconButton = styled(Stack)(() => ({
  borderRadius: "50%",
  backgroundColor: " #050505cc",
  height: "32px",
  width: "32px",
  alignItems: "center",
  justifyContent: "center",
}));
 
const Overlay = styled(Stack, {
  shouldForwardProp: (prop) => prop !== "rounded" && prop !== "disabled",
})((props: { rounded: boolean | undefined; disabled: boolean }) => ({
  position: "absolute",
  top: 0,
  right: 0,
  bottom: 0,
  left: 0,
  alignItems: "center",
  justifyContent: "center",
  opacity: 0,
  transition: "opacity 0.2s ease",
  border: "4px solid transparent",
  cursor: "pointer",
  overflow: "hidden",
  borderRadius: props.rounded ? "50%" : "8px",
  pointerEvents: props.disabled ? "none" : "auto",
}));