All files / src/components/UploadFile/Rebranded Preview.tsx

0% Statements 0/18
0% Branches 0/38
0% Functions 0/2
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 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                                                                                                                                                                                                                                                                                                                                                                                                   
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 { SxProps, styled } from "@mui/material/styles";
import LinearProgress, {
  linearProgressClasses,
} from "@mui/material/LinearProgress";
// components
import { Text, TruncateText } from "@common/Text";
import { IconButton } from "@common/IconButton";
// assets
import { RefundTopIcon } from "@assets/icons";
// utils
import { truncateStringLength } from "utils";
import CloseIcon from "../../../assets/rebrandIcons/CloseIcon";
import { palette } from "@palette";
 
const PreviewLinearProgress = styled(LinearProgress)(({ theme }) => ({
  height: "6px",
  borderRadius: "8px",
  [`&.${linearProgressClasses.colorPrimary}`]: {
    backgroundColor: palette.liftedWhite[200],
  },
  [`& .${linearProgressClasses.bar}`]: {
    borderRadius: "8px",
    backgroundColor: palette.black[300],
  },
}));
 
const fileContainer = {
  margin: "8px",
  borderRadius: "8px",
  p: "8px 16px",
  height: 52,
  width: "100%",
  cursor: "pointer",
  background: palette.neutral[5],
  display: "inline-block",
  "&:hover": {
    background: palette.neutral[10],
  },
  //EVIL.Does not get override no matter what
  // "@media (min-width: 600px)": {
  //   width: "30%",
  // },
};
 
const Preview: React.FC<
  IPreviewProps & {
    customProps?: Partial<{
      file_width: string | number;
      wrapperBoxSx: SxProps;
    }>;
  }
> = ({
  fileWithMeta: { cancel, remove, restart },
  meta: {
    name = "",
    percent = 0,
    size = 0,
    // previewUrl,
    status,
    duration,
    validationError,
  },
  canCancel,
  canRemove,
  canRestart,
  extra: { minSizeBytes },
  customProps: { wrapperBoxSx } = { wrapperBoxSx: fileContainer },
}) => {
  let title = `${truncateStringLength(name, 15) || "?"}`;
  const fileSize = `${formatBytes(size)}`;
  if (duration) title = `${title}, ${formatDuration(duration)}`;
  
  if (status === "error_file_size" || status === "error_validation") {
    return (
      <Box sx={wrapperBoxSx}>
        <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}>
            <CloseIcon width={24} height={24} />
          </IconButton>
        )}
      </Box>
    );
  }
 
  if (
    status === "error_upload_params" ||
    status === "exception_upload" ||
    status === "error_upload"
  ) {
    title = `${truncateStringLength(name, 10)} (upload failed)`;
  }
  if (status === "aborted")
    title = `${truncateStringLength(name, 10)} (cancelled)`;
 
  if (status !== "uploading")
    return (
      <Box sx={fileContainer}>
        <Stack
          direction="row"
          alignItems="center"
          justifyContent="space-between"
        >
          <Stack
            direction={"column"}
            display={"flex"}
            justifyContent={"space-between"}
          >
            <TruncateText lineClamp={1}>{title}</TruncateText>
            <TruncateText
              lineClamp={1}
              color={palette.neutral[60]}
              textAlign="left"
            >
              {fileSize}
            </TruncateText>
          </Stack>
          {/** ----------- Button ----------- */}
          <Box display={"flex"}>
            {status !== "preparing" &&
              status !== "getting_upload_params" &&
              canRemove && (
                <IconButton variant="text" size="small" onClick={remove}>
                  <CloseIcon width={24} height={24} />
                </IconButton>
              )}
            {[
              "error_upload_params",
              "exception_upload",
              "error_upload",
              "aborted",
              "ready",
            ].includes(status) &&
              canRestart && (
                <IconButton variant="text" size="small" onClick={restart}>
                  <RefundTopIcon size="20px" />
                </IconButton>
              )}
          </Box>
        </Stack>
      </Box>
    );
 
  return (
    <Box sx={fileContainer}>
      <Stack display={"flex"}>
        <Stack
          gap={1}
          sx={{ height: 18 }}
          display="flex"
          alignItems="center"
          flexDirection="row"
        >
          <Box width="100%">
            <PreviewLinearProgress variant="determinate" value={percent} />
          </Box>
          {canCancel && (
            <IconButton variant="text" size="small" onClick={cancel}>
              <CloseIcon />
            </IconButton>
          )}
        </Stack>
        <StyledTitle textAlign={"center"}>{title}</StyledTitle>
      </Stack>
    </Box>
  );
};
 
const StyledTitle = styled(Text)({
  fontSize: 14,
  fontWeight: 350,
  lineHeight: "16.8px",
  color: palette.neutral[80],
});
 
export default Preview;