All files / src/features/GiveConversation/components FilesListComponent.tsx

72.41% Statements 21/29
80.76% Branches 21/26
55.55% Functions 5/9
74.07% Lines 20/27

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                      7x 11x 5x   6x 3x   3x     7x                     4x 4x 4x 4x   4x 4x     4x   4x       4x       4x             4x                                                                               11x 11x                                                                                                                                                            
import React, { useEffect, useRef, useState } from "react";
import { Box, Stack } from "@mui/material";
import { UploadedFile } from "../types";
import { useAppTheme } from "@theme/v2/Provider";
import GiveText from "@shared/Text/GiveText";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import { ArrowLeftIcon, ArrowRightIcon, XIcon } from "@phosphor-icons/react";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { bytesToSize } from "@utils/index";
import PdfPreview, { InputFileImage } from "./PdfPreview";
 
const getFilePreview = (file: File) => {
  if (file.type.startsWith("image/")) {
    return { url: URL.createObjectURL(file), type: "image" };
  }
  if (file.type === "application/pdf") {
    return { url: URL.createObjectURL(file), type: "pdf" };
  }
  return { url: null, type: "unknown" };
};
 
const FallBack = () => <Box width="36px" height="36px" />;
 
function FilesListComponent({
  files,
  handleRemoveFile,
  isSubmitting,
}: {
  files: UploadedFile[];
  handleRemoveFile?: (id: string) => void;
  isSubmitting?: boolean;
}) {
  const theme = useAppTheme();
  const scrollRef = useRef<HTMLDivElement | null>(null);
  const [hasScrolled, setHasScrolled] = useState(false);
  const [hasMore, setHasMore] = useState(files.length > 2);
 
  useEffect(() => {
    setHasMore(files.length > 2);
  }, [files.length]);
 
  const { isMobileView } = useCustomThemeV2();
 
  const scrollNext = () => {
    scrollRef.current?.scrollBy({ left: 202, behavior: "smooth" });
  };
 
  const scrollBack = () => {
    scrollRef.current?.scrollBy({ left: -202, behavior: "smooth" });
  };
 
  const handleScroll = () => {
    if (!scrollRef.current) return;
    const { scrollLeft, scrollWidth, clientWidth } = scrollRef.current;
    setHasScrolled(scrollLeft > 0);
    setHasMore(scrollLeft + clientWidth < scrollWidth - 1);
  };
 
  return (
    <Box
      position="relative"
      display="flex"
      justifyContent="space-between"
      alignItems="center"
      pb="8px"
    >
      {!isMobileView && (
        <>
          {hasScrolled ? (
            <GiveIconButton
              data-testid="icon-button-ArrowLeft"
              Icon={ArrowLeftIcon}
              onClick={scrollBack}
            />
          ) : (
            <FallBack />
          )}
        </>
      )}
 
      <Box sx={{ width: "408px", overflow: "hidden" }}>
        <Box
          ref={scrollRef}
          onScroll={handleScroll}
          sx={{
            display: "flex",
            gap: 1,
            overflowX: "auto",
            scrollBehavior: "smooth",
            scrollbarWidth: "none",
            msOverflowStyle: "none",
            "&::-webkit-scrollbar": {
              display: "none",
            },
            scrollSnapType: "x mandatory",
          }}
        >
          {files.map(({ id, file }) => {
            const { url: previewUrl, type: previewType } = getFilePreview(file);
            return (
              <Stack
                key={id}
                width={isMobileView ? "232px" : "194px"}
                height="60px"
                flexDirection="row"
                alignItems="center"
                borderRadius="8px"
                bgcolor={theme.palette.surface?.secondary}
                gap="8px"
                p="8px"
                flexShrink={0}
                sx={{ scrollSnapAlign: "start" }}
              >
                {previewType === "image" && (
                  <InputFileImage previewUrl={previewUrl || ""} file={file} />
                )}
 
                {previewType === "pdf" && <PdfPreview file={file} />}
                {previewType === "unknown" && (
                  <Box
                    sx={{
                      fontSize: "10px",
                      color: "gray",
                      textAlign: "center",
                    }}
                  >
                    No Preview
                  </Box>
                )}
 
                <Box sx={{ flex: 1, minWidth: 0 }}>
                  <GiveText
                    sx={{
                      whiteSpace: "nowrap",
                      overflow: "hidden",
                      textOverflow: "ellipsis",
                      display: "block",
                    }}
                    fontSize="14px"
                  >
                    {file?.name}
                  </GiveText>
                  <GiveText color="secondary" fontSize="12px">
                    {bytesToSize(Number(file?.size), 1).sizeString}
                  </GiveText>
                </Box>
 
                <GiveIconButton
                  disabled={isSubmitting}
                  data-testid={`icon-button`}
                  onClick={() => handleRemoveFile?.(id || "")}
                  variant="ghost"
                  Icon={XIcon}
                />
              </Stack>
            );
          })}
        </Box>
      </Box>
      {!isMobileView && (
        <>
          {hasMore ? (
            <GiveIconButton
              data-testid="icon-button-ArrowRight"
              Icon={ArrowRightIcon}
              onClick={scrollNext}
            />
          ) : (
            <FallBack />
          )}
        </>
      )}
    </Box>
  );
}
 
export default FilesListComponent;