src/components/MediaShowcase.tsx   A
last analyzed

Complexity

Total Complexity 8
Complexity/F 8

Size

Lines of Code 55
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 43
mnd 7
bc 7
fnc 1
dl 0
loc 55
rs 10
bpm 7
cpm 8
noi 0
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B MediaShowcase.tsx ➔ MediaShowcase 0 47 8
1
import React, {useEffect, useState} from "react";
2
import "./MediaShowcase.css";
3
4
const imageSizeLimit = 10 * 1000 * 1000; // 10MB
5
const videoSizeLimit = 100 * 1000 * 1000; // 100MB
6
7
function MediaShowcase(props: { file: File }): JSX.Element {
8
  const [fileContent, setFileContent] = useState<string>("");
9
10
  useEffect(() => {
11
    getFileContent();
12
  }, [props.file]);
13
14
  const getFileContent = () => {
15
    if ((props.file.type.includes("image") && props.file.size <= imageSizeLimit) ||
16
      (props.file.type.includes("video") && props.file.size <= videoSizeLimit)) {
17
      const reader = new FileReader();
18
      reader.onload = function (event) {
19
        if (event.target?.result) {
20
          setFileContent(event.target.result.toString());
21
        }
22
      };
23
      reader.readAsDataURL(props.file);
24
    }
25
  };
26
27
  if (props.file.type.includes("image")) {
28
    if (props.file.size < imageSizeLimit) {
29
      return (
30
        <React.Fragment>
31
          {fileContent != "" && <img className="selected-media" src={fileContent} alt="selected media"/>}
32
          {!fileContent && <h1>Loading...</h1>}
33
        </React.Fragment>
34
      );
35
    } else {
36
      return <h1>File too large</h1>;
37
    }
38
  } else if (props.file.type.includes("video")) {
39
    if (props.file.size < videoSizeLimit) {
40
      return (
41
        <React.Fragment>
42
          {props.file.type.includes("video") && fileContent &&
43
            <video className="selected-media" src={fileContent} autoPlay muted loop/>}
44
          {!fileContent && <h1>Loading...</h1>}
45
        </React.Fragment>
46
      );
47
    } else {
48
      return <h1>File too large</h1>;
49
    }
50
  }
51
  return <h1>Loading...</h1>;
52
}
53
54
export default MediaShowcase;
55