All files / src/components/UploadFile Preview.tsx

0% Statements 0/21
0% Branches 0/47
0% Functions 0/6
0% Lines 0/16

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                                                                                                                                                                                                                                                                                                                                                 
import * as React from "react";
import {
  IPreviewProps,
  formatBytes,
  formatDuration,
} from "react-dropzone-uploader";
// @mui
import Box from "@mui/material/Box";
import Stack from "@mui/material/Stack";
import { styled } from "@mui/material/styles";
import LinearProgress, {
  linearProgressClasses,
} from "@mui/material/LinearProgress";
import { useTheme } from "@mui/material/styles";
// components
import { Text } from "@common/Text";
import { IconButton } from "@common/IconButton";
// assets
import { CloseIcon, TrashIcon, RefundTopIcon } from "@assets/icons";
// utils
import { truncateStringLength } from "utils";
 
const PreviewLinearProgress = styled(LinearProgress)(({ theme }) => ({
  height: "6px",
  borderRadius: "8px",
  [`&.${linearProgressClasses.colorPrimary}`]: {
    backgroundColor: theme.palette.neutral[100],
  },
  [`& .${linearProgressClasses.bar}`]: {
    borderRadius: "8px",
    backgroundColor: theme.palette.success.main,
  }, 
  // [`& .${linearProgressClasses.root}.${linearProgressClasses.indeterminate}`]: {
  //   backgroundColor: theme.palette.neutral[700],
  // },
}));
 
const fileContainer = {
  borderRadius: "8px",
  p: "8px 16px",
  height: 53,
  width: "100%",
  cursor: "pointer",
  background: "#FFFFFF",
  boxShadow:
    "0px 12px 16px -4px rgba(16, 24, 40, 0.08), 0px 4px 6px -2px rgba(16, 24, 40, 0.03)",
 
  "&:hover": {
    background: "#F4F3F7",
  },
 
  "@media (min-width: 600px)": {
    width: 228,
  },
};
 
const Preview: React.FC<IPreviewProps> = ({
  fileWithMeta: { cancel, remove, restart },
  meta: {
    name = "",
    percent = 0,
    size = 0,
    // previewUrl,
    status,
    duration,
    validationError,
  },
  isUpload,
  canCancel,
  canRemove,
  canRestart,
  extra: { minSizeBytes },
}) => {
  const theme = useTheme();
  let title = `${truncateStringLength(name, 15) || "?"}`;
  const fileSize = `${formatBytes(size)}`;
  const [isHover, setIsHover] = React.useState(false);
  if (duration) title = `${title}, ${formatDuration(duration)}`;
 
  if (status === "error_file_size" || status === "error_validation") {
    return (
      <Box sx={fileContainer} onMouseEnter={() => setIsHover(true)} onMouseLeave={() => setIsHover(false)}>
        <Text color="error">{title}</Text>
        {status === "error_file_size" && (
          <Text color="error">
            {size < minSizeBytes ? "File too small" : "File too big"}
          </Text>
        )}
        {status === "error_validation" && (
          <Text color="error">{String(validationError)}</Text>
        )}
        {canRemove && (
          <IconButton size="small" onClick={remove}>
            <TrashIcon stroke={isHover ? "#D92D20" : "#273B4A"} />
          </IconButton>
        )}
      </Box>
    );
  }
 
  if (
    status === "error_upload_params" ||
    status === "exception_upload" ||
    status === "error_upload"
  ) {
    title = `${title} (upload failed)`;
  }
  if (status === "aborted") title = `${title} (cancelled)`;
 
  return (
    <Box sx={fileContainer} onMouseEnter={() => setIsHover(true)} onMouseLeave={() => setIsHover(false)}>
      <Stack direction="row" alignItems="center" justifyContent="space-between">
        <Box mb={0.5}>
          <Text
            lineHeight="16px"
            fontWeight="semibold"
            color={theme.palette.neutral[800]}
          >
            {title}
          </Text>
          <Text variant="caption" color={theme.palette.neutral[400]}>
            {fileSize}
          </Text>
        </Box>
 
        {/** ----------- Button ----------- */}
        {status === "uploading" && canCancel && (
          <IconButton variant="text" size="small" onClick={cancel}>
            <CloseIcon width={24} />
          </IconButton>
        )}
        {status !== "preparing" &&
          status !== "getting_upload_params" &&
          status !== "uploading" &&
          canRemove && (
            <IconButton variant="text" size="small" onClick={remove} >
              <TrashIcon stroke={isHover ? "#D92D20" : "#273B4A"} />
            </IconButton>
          )}
        {[
          "error_upload_params",
          "exception_upload",
          "error_upload",
          "aborted",
          "ready",
        ].includes(status) &&
          canRestart && (
            <IconButton variant="text" size="small" onClick={restart}>
              <RefundTopIcon size="20px" />
            </IconButton>
          )}
      </Stack>
 
      {isUpload && (
        <Box width="100%">
          <PreviewLinearProgress
            variant="determinate"
            value={
              status === "done" || status === "headers_received" ? 100 : percent
            }
          />
        </Box>
      )}
    </Box>
  );
};
 
export default Preview;