1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import classnames from 'classnames';
10 Table, TableBody, TableCell, TableHead, TableRow,
14 } from '@material-ui/core';
15 import { withStyles } from '@material-ui/core';
16 import Dropzone from 'react-dropzone';
17 import { CloudUploadIcon, RemoveIcon } from "../icon/icon";
18 import { formatFileSize, formatProgress, formatUploadSpeed } from "common/formatters";
19 import { UploadFile } from 'store/file-uploader/file-uploader-actions';
21 type CssRules = "root" | "dropzone" | "dropzoneWrapper" | "container" | "uploadIcon"
22 | "dropzoneBorder" | "dropzoneBorderLeft" | "dropzoneBorderRight" | "dropzoneBorderTop" | "dropzoneBorderBottom"
23 | "dropzoneBorderHorzActive" | "dropzoneBorderVertActive" | "deleteButton" | "deleteButtonDisabled" | "deleteIcon";
25 const styles: StyleRulesCallback<CssRules> = theme => ({
37 border: "1px solid rgba(0, 0, 0, 0.42)"
42 transition: "transform 200ms cubic-bezier(0.0, 0, 0.2, 1) 0ms",
43 pointerEvents: "none",
44 backgroundColor: "#6a1b9a"
51 transform: "scaleY(0)",
53 dropzoneBorderRight: {
58 transform: "scaleY(0)",
65 transform: "scaleX(0)",
67 dropzoneBorderBottom: {
72 transform: "scaleX(0)",
74 dropzoneBorderHorzActive: {
75 transform: "scaleY(1)"
77 dropzoneBorderVertActive: {
78 transform: "scaleX(1)"
84 verticalAlign: "middle"
89 deleteButtonDisabled: {
97 interface FileUploadPropsData {
100 onDrop: (files: File[]) => void;
101 onDelete: (file: UploadFile) => void;
104 interface FileUploadState {
108 export type FileUploadProps = FileUploadPropsData & WithStyles<CssRules>;
110 export const FileUpload = withStyles(styles)(
111 class extends React.Component<FileUploadProps, FileUploadState> {
112 constructor(props: FileUploadProps) {
118 onDelete = (event: React.MouseEvent<HTMLTableCellElement>, file: UploadFile): void => {
119 const { onDelete, disabled } = this.props;
121 event.stopPropagation();
127 let interval = setInterval(() => {
128 const key = Object.keys((window as any).cancelTokens).find(key => key.indexOf(file.file.name) > -1);
131 clearInterval(interval);
132 (window as any).cancelTokens[key]();
133 delete (window as any).cancelTokens[key];
139 const { classes, onDrop, disabled, files } = this.props;
140 return <div className={"file-upload-dropzone " + classes.dropzoneWrapper}>
141 <div className={classnames(classes.dropzoneBorder, classes.dropzoneBorderLeft, { [classes.dropzoneBorderHorzActive]: this.state.focused })} />
142 <div className={classnames(classes.dropzoneBorder, classes.dropzoneBorderRight, { [classes.dropzoneBorderHorzActive]: this.state.focused })} />
143 <div className={classnames(classes.dropzoneBorder, classes.dropzoneBorderTop, { [classes.dropzoneBorderVertActive]: this.state.focused })} />
144 <div className={classnames(classes.dropzoneBorder, classes.dropzoneBorderBottom, { [classes.dropzoneBorderVertActive]: this.state.focused })} />
145 <Dropzone className={classes.dropzone}
146 onDrop={files => onDrop(files)}
148 const el = document.getElementsByClassName("file-upload-dropzone")[0];
149 const inputs = el.getElementsByTagName("input");
150 if (inputs.length > 0) {
154 data-cy="drag-and-drop"
168 {files.length === 0 &&
169 <Grid container justify="center" alignItems="center" className={classes.container}>
170 <Grid item component={"span"}>
171 <Typography variant='subtitle1'>
172 <CloudUploadIcon className={classes.uploadIcon} /> Drag and drop data or click to browse
177 <Table style={{ width: "100%" }}>
180 <TableCell>File name</TableCell>
181 <TableCell>File size</TableCell>
182 <TableCell>Upload speed</TableCell>
183 <TableCell>Upload progress</TableCell>
184 <TableCell>Delete</TableCell>
189 <TableRow key={f.id}>
190 <TableCell>{f.file.name}</TableCell>
191 <TableCell>{formatFileSize(f.file.size)}</TableCell>
192 <TableCell>{formatUploadSpeed(f.prevLoaded, f.loaded, f.prevTime, f.currentTime)}</TableCell>
193 <TableCell>{formatProgress(f.loaded, f.total)}</TableCell>
197 onClick={(event: React.MouseEvent<HTMLTableCellElement>) => this.onDelete(event, f)}
198 className={disabled ? classnames(classes.deleteButtonDisabled, classes.deleteIcon) : classnames(classes.deleteButton, classes.deleteIcon)}