All files / src/components/pages/filesytem FileListItem.tsx

9.09% Statements 3/33
0% Branches 0/33
0% Functions 0/6
10% Lines 3/30

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                                                      2x             2x         2x                                                                                                                                                                                                                                                  
import { FsEntity } from "../../../background/api/filesystemTypes";
import React, { ReactElement } from "react";
import { Col, Form } from "react-bootstrap";
import { Link } from "react-router-dom";
import {
  FileEarmarkCodeIcon,
  FileEarmarkIcon,
  FileEarmarkImageIcon,
  FileEarmarkLock2Icon,
  FileEarmarkMusicIcon,
  FileEarmarkPDFIcon,
  FileEarmarkPlayIcon,
  FileEarmarkRichtextIcon,
  FileEarmarkTextIcon,
  FileEarmarkZipIcon,
  FolderIcon
} from "../../../elements/svg/SymbolFile";
import { reverseString } from "../../../background/methods/strings";
import { getDateAsStringFromTimestamp } from "../../../background/methods/time";
import { formatBytes } from "../../../background/methods/bytes";
import { connect, ConnectedProps } from "react-redux";
import { SystemState } from "../../../background/redux/actions/sytemState";
import {
  addToSelected,
  removeFromSelected
} from "../../../background/redux/actions/filesystem";
 
const mapState = (state: SystemState) => ({
  filesystem: {
    selectedFsEnties: state.filesystem.selectedFsEnties
  }
});
 
// this takes the redux actions and maps them to the props
const mapDispatch = {
  addToSelected,
  removeFromSelected
};
 
const connector = connect(mapState, mapDispatch);
 
type PropsFromRedux = ConnectedProps<typeof connector>;
 
type Props = PropsFromRedux & {
  fileListItem: FsEntity;
  setPath?: Function;
};
 
function FileListItem(props: Props): ReactElement {
  let isSelected = !!props.filesystem.selectedFsEnties.find(
    (e: FsEntity) => e.fileSystemId === props.fileListItem.fileSystemId
  );
 
  const ICON_PREFERENCES = {
    height: "40px",
    width: "100%",
    color: "secondary"
  };
 
  const FileIcon = (isFolder: boolean, name: string): ReactElement => {
    if (isFolder) return <FolderIcon {...ICON_PREFERENCES} />;
 
    let positionOfPoint = reverseString(name).indexOf(".");
    if (positionOfPoint < 0) return <FileEarmarkIcon {...ICON_PREFERENCES} />;
 
    const fileExtension = reverseString(
      reverseString(name).substr(0, positionOfPoint + 1)
    );
    switch (fileExtension) {
      case ".txt":
        return <FileEarmarkTextIcon {...ICON_PREFERENCES} />;
      case ".jpg":
        return <FileEarmarkImageIcon {...ICON_PREFERENCES} />;
      case ".mp4":
        return <FileEarmarkPlayIcon {...ICON_PREFERENCES} />;
      case ".mp3":
        return <FileEarmarkMusicIcon {...ICON_PREFERENCES} />;
      case ".crypt":
        return <FileEarmarkLock2Icon {...ICON_PREFERENCES} />;
      case ".pdf":
        return <FileEarmarkPDFIcon {...ICON_PREFERENCES} />;
      case ".zip":
      case ".7z":
        return <FileEarmarkZipIcon {...ICON_PREFERENCES} />;
      case ".ts":
      case ".tsx":
      case ".js":
      case ".jsx":
      case ".java":
        return <FileEarmarkCodeIcon {...ICON_PREFERENCES} />;
      case ".md":
      case ".html":
        return <FileEarmarkRichtextIcon {...ICON_PREFERENCES} />;
      default:
        return <FileEarmarkIcon {...ICON_PREFERENCES} />;
    }
  };
 
  const onClick = () => {
    if (
      props.fileListItem.type === "FOLDER" &&
      props.setPath &&
      props.fileListItem.path
    ) {
      props.setPath(props.fileListItem.path);
    }
  };
 
  const handleSelectedChanged = () => {
    if (isSelected) {
      props.removeFromSelected(props.fileListItem);
    } else {
      props.addToSelected(props.fileListItem);
    }
  };
 
  return (
    <>
      <Col xs={2} md={1}>
        <Form.Group controlId="formBasicCheckbox">
          <Form.Check
            checked={isSelected}
            type="checkbox"
            onChange={handleSelectedChanged}
          />
        </Form.Group>
      </Col>
      <Col xs={2} md={1}>
        {FileIcon(
          props.fileListItem.type === "FOLDER",
          props.fileListItem.name
        )}
      </Col>
      <Col xs={1}>...</Col>
      <Col xs={7} md={4}>
        <Link
          to={
            props.fileListItem.path && props.fileListItem.type === "FOLDER"
              ? `/file${props.fileListItem.path ?? ""}`
              : `#${props.fileListItem.name}`
          }
          onClick={onClick}
        >
          {props.fileListItem.name}
        </Link>
      </Col>
      <Col xs={6} md={3}>
        {props.fileListItem.lastUpdatedBy.username}
      </Col>
      <Col xs={3} md={1}>
        {getDateAsStringFromTimestamp(props.fileListItem.lastUpdated)}
      </Col>
      <Col xs={3} md={1}>
        {formatBytes(props.fileListItem.size)}
      </Col>
    </>
  );
}
 
export default connector(FileListItem);