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

46.3% Statements 25/54
20% Branches 8/40
36.84% Functions 7/19
46.15% Lines 24/52

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                                    2x             2x             2x             2x   1x     1x     1x 1x 1x   1x   1x   1x   1x   1x 1x   2x   2x   1x               1x 1x 1x     1x                                                                                           1x 1x                                                                                                                          
import React, { ReactElement, useEffect, useState } from "react";
import { getFolderContents } from "../../../background/api/filesystem";
import { FsEntity } from "../../../background/api/filesystemTypes";
import { Col, Container, Form, Row } from "react-bootstrap";
import { useLocation } from "react-router-dom";
import { FilesBreadcrumb } from "./FilesBreadcrumb";
import { filesBaseUrl } from "./Filesystem";
import FileListItem from "./FileListItem";
import { SystemState } from "../../../background/redux/actions/sytemState";
import {
  addToSelected,
  clearSelected,
  removeFromSelected,
  replaceSelected
} from "../../../background/redux/actions/filesystem";
import { connect, ConnectedProps } from "react-redux";
import { FFLoading } from "../../basicElements/Loading";
 
const mapState = (state: SystemState) => ({
  filesystem: {
    selectedFsEnties: state.filesystem.selectedFsEnties
  }
});
 
// this takes the redux actions and maps them to the props
const mapDispatch = {
  addToSelected,
  removeFromSelected,
  replaceSelected,
  clearSelected
};
 
const connector = connect(mapState, mapDispatch);
 
type PropsFromRedux = ConnectedProps<typeof connector>;
 
type Props = PropsFromRedux & {};
 
function FileList(props: Props): ReactElement {
  let location = useLocation();
 
  const [path, setPath] = useState<string>(
    location.pathname.slice(filesBaseUrl.length) || "/"
  );
  const [filesAndFolders, setFilesAndFolders] = useState<FsEntity[] | null>(
    null
  );
  const [error, setError] = useState<string>("");
  const [sortedBy, setSortedBy] = useState<keyof FsEntity | null>(null);
  const [sortIncreasing, setSortIncreasing] = useState<boolean>(false);
  const allAreSelected =
    filesAndFolders?.length === props.filesystem.selectedFsEnties.length;
 
  const clearSelected = props.clearSelected;
 
  useEffect(() => {
    function updateStates(): void {
      getFolderContents(path)
        .then((response: FsEntity[]) => {
          console.log("got folder content");
          setFilesAndFolders([
            ...response.filter(
              (fsEntiy: FsEntity) => fsEntiy.type === "FOLDER"
            ),
            ...response.filter((fsEntiy: FsEntity) => fsEntiy.type !== "FOLDER")
          ]);
          setError("");
        })
        .catch((err) => {
          setError(err.response?.data?.message);
          setFilesAndFolders(null);
        });
    }
 
    setPath(location.pathname.slice(filesBaseUrl.length) || "/");
    clearSelected();
    updateStates();
  }, [clearSelected, path, location]);
 
  const handleSelectAllChanged = () => {
    if (allAreSelected) {
      props.clearSelected();
    } else {
      if (filesAndFolders) {
        props.replaceSelected([...filesAndFolders]);
      }
    }
  };
 
  function handleSortClick(property: keyof FsEntity) {
    if (!filesAndFolders || filesAndFolders.length < 2) return;
    if (sortedBy === property) {
      setSortIncreasing(!sortIncreasing);
    } else {
      setSortedBy(property);
      setSortIncreasing(true);
    }
    let toSort = [...(filesAndFolders ?? [])];
 
    if (property === "lastUpdated" || property === "size") {
      toSort.sort((a, b) =>
        a[property] - b[property] === 0
          ? a.fileSystemId - b.fileSystemId
          : a[property] - b[property]
      );
    } else if (property === "name" || property === "type") {
      toSort.sort((a, b) =>
        a[property].toLowerCase().localeCompare(b[property].toLowerCase()) === 0
          ? a.fileSystemId - b.fileSystemId
          : a[property].toLowerCase().localeCompare(b[property].toLowerCase())
      );
    } else if (property === "lastUpdatedBy") {
      toSort.sort((a, b) =>
        a.lastUpdatedBy.username
          .toLowerCase()
          .localeCompare(b.lastUpdatedBy.username.toLowerCase()) === 0
          ? a.fileSystemId - b.fileSystemId
          : a.lastUpdatedBy.username
              .toLowerCase()
              .localeCompare(b.lastUpdatedBy.username.toLowerCase())
      );
    }
    setFilesAndFolders(sortIncreasing ? toSort.reverse() : toSort);
  }
 
  console.log("[FileList path]" + path);
  return (
    <Container fluid>
      <FilesBreadcrumb path={path} setPath={setPath} />
      <Row>
        <Col xs={2} md={1}>
          <Form.Group controlId="formBasicCheckbox">
            <Form.Check
              checked={allAreSelected}
              type="checkbox"
              onChange={handleSelectAllChanged}
            />
          </Form.Group>
        </Col>
        <Col
          xs={2}
          md={1}
          className="text-center"
          onClick={() => handleSortClick("type")}
        >
          {"Type"}
        </Col>
        <Col xs={2} md={1}>
          {"Share"}
        </Col>
        <Col xs={6} md={4} onClick={() => handleSortClick("name")}>
          {"Name"}
        </Col>
        <Col xs={6} md={3} onClick={() => handleSortClick("lastUpdatedBy")}>
          {"Owner"}
        </Col>
        <Col xs={3} md={1} onClick={() => handleSortClick("lastUpdated")}>
          {"Last changes"}
        </Col>
        <Col xs={3} md={1} onClick={() => handleSortClick("size")}>
          {"Size"}
        </Col>
      </Row>
      <hr />
      <Row>
        {error ? (
          <Col className={"text-center"}> {error}</Col>
        ) : filesAndFolders?.length === 0 ? (
          <Col className={"text-center"}> Nothing to see here.</Col>
        ) : (
          !filesAndFolders && <FFLoading />
        )}
 
        {filesAndFolders?.map((folder: FsEntity) => {
          return (
            <React.Fragment key={folder.fileSystemId}>
              <FileListItem setPath={setPath} fileListItem={folder} />
              <Col xs={12} className="border my-2" />
            </React.Fragment>
          );
        })}
      </Row>
    </Container>
  );
}
 
export default connector(FileList);