All files / src/components/Settings/MediaLibrary GiveMediaLibrary.tsx

90.9% Statements 30/33
87.5% Branches 28/32
100% Functions 5/5
100% Lines 28/28

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                                              27x           26x 26x 26x 26x 26x 26x 26x 26x 26x   26x 1x 1x 1x   26x 1x     26x 1x 1x 1x 1x 1x 1x   1x       1x 1x         1x     26x                                                                                                                          
import { Box, Stack } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import LoadingState from "./components/LoadingState";
import { useGetAllImages } from "@hooks/merchant-api/image-gallery/useGetAllImages";
import { ROWS_PER_PAGE } from "@hooks/common/usePagination";
import { isEmpty } from "lodash";
import EmptyState from "./components/EmptyState";
import { AddImageButton, GridContainer } from "./components/Media.atoms";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import useThemeSettings from "@components/EnterpriseSettings/Branding/hooks/useThemeSettings";
import LibraryImageComponent from "./components/LibraryImageComponent";
import { DROP_MEDIA_LIBRARY_FILE } from "modals/modal_names";
import NiceModal from "@ebay/nice-modal-react";
import { useRef } from "react";
import {
  AcceptAllowedImagesTypes,
  useDropzoneUpload,
} from "@hooks/upload-api/uploadHooks";
import { useIsValidFile } from "@hooks/upload-api/useIsValidFile";
import useHEICConversion from "@components/UploadFile/hooks/useHEICConversion";
import { UploadDocumentTypes } from "@redux/slices/uploadProgressSlice/types";
 
function GiveMediaLibrary() {
  const fileInputRef = useRef<HTMLInputElement>(null);
  const {
    images,
    isLoading = false,
    scrollRef,
    hasNextPage,
  } = useGetAllImages(ROWS_PER_PAGE, false, true);
  const { isMobileView } = useCustomThemeV2();
  const { textColor } = useThemeSettings();
  const { isValidFile } = useIsValidFile();
  const { isUploading, handleDirectUpload: handleUpload } = useDropzoneUpload();
  const showImages = !isEmpty(images);
  const showLoading = hasNextPage || isLoading;
  const showEmpty = !showImages && !showLoading;
  const { convertAndUploadHEICFiles } = useHEICConversion();
 
  const handleDragEnter = (event: any) => {
    Iif (isMobileView) return;
    event.preventDefault();
    NiceModal.show(DROP_MEDIA_LIBRARY_FILE);
  };
  const handleButtonClick = () => {
    Eif (fileInputRef.current) fileInputRef.current?.click?.();
  };
 
  const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    const file = event.target.files;
    Iif (!file || file.length === 0) return;
    for (let i = 0; i < file.length; i++) {
      const currentFile = file[i];
      const allowedTypes = Object.keys(AcceptAllowedImagesTypes);
      Iif (!isValidFile({ file: currentFile, allowedTypes })) continue;
 
      convertAndUploadHEICFiles({
        files: [currentFile],
        documentType: UploadDocumentTypes.mediaLibrary,
        onComplete: (files: File[]) => {
          const [currentFile] = files;
          handleUpload(currentFile);
        },
      });
    }
 
    event.target.value = "";
  };
 
  return (
    <Box data-testid="media-library-modal-trigger" onDragOver={handleDragEnter}>
      <input
        type="file"
        multiple={false}
        ref={fileInputRef}
        style={{ display: "none" }}
        onChange={handleFileChange}
        data-testid="media-library-file-input"
      />
      <Stack
        justifyContent="space-between"
        alignItems={{
          xs: "stretch",
          sm: "center",
        }}
        flexDirection={{
          xs: "column",
          sm: "row",
        }}
        gap="24px"
      >
        <GiveText
          sx={{
            color: isMobileView ? textColor : undefined,
          }}
          fontWeight={isMobileView ? 300 : 400}
          fontSize={isMobileView ? "32px" : "18px"}
          mt={isMobileView ? "40px" : undefined}
        >
          Media Library
        </GiveText>
        {(isLoading || !isEmpty(images)) && (
          <AddImageButton
            isUploading={isUploading}
            onClick={handleButtonClick}
          />
        )}
      </Stack>
      <Box
        py={{
          xs: "24px",
          sm: "36px",
        }}
      >
        <GridContainer dataTestId="media-library-image-list">
          {showImages && <LibraryImageComponent images={images} />}
          {showLoading && <LoadingState ref={scrollRef} />}
        </GridContainer>
        {showEmpty && (
          <EmptyState
            isUploading={isUploading}
            handleButtonClick={handleButtonClick}
          />
        )}
      </Box>
    </Box>
  );
}
 
export default GiveMediaLibrary;