1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from 'react';
6 import * as 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();
128 const { classes, onDrop, disabled, files } = this.props;
129 return <div className={"file-upload-dropzone " + classes.dropzoneWrapper}>
130 <div className={classnames(classes.dropzoneBorder, classes.dropzoneBorderLeft, { [classes.dropzoneBorderHorzActive]: this.state.focused })} />
131 <div className={classnames(classes.dropzoneBorder, classes.dropzoneBorderRight, { [classes.dropzoneBorderHorzActive]: this.state.focused })} />
132 <div className={classnames(classes.dropzoneBorder, classes.dropzoneBorderTop, { [classes.dropzoneBorderVertActive]: this.state.focused })} />
133 <div className={classnames(classes.dropzoneBorder, classes.dropzoneBorderBottom, { [classes.dropzoneBorderVertActive]: this.state.focused })} />
134 <Dropzone className={classes.dropzone}
135 onDrop={files => onDrop(files)}
137 const el = document.getElementsByClassName("file-upload-dropzone")[0];
138 const inputs = el.getElementsByTagName("input");
139 if (inputs.length > 0) {
156 {files.length === 0 &&
157 <Grid container justify="center" alignItems="center" className={classes.container}>
158 <Grid item component={"span"}>
159 <Typography variant='subtitle1'>
160 <CloudUploadIcon className={classes.uploadIcon} /> Drag and drop data or click to browse
165 <Table style={{ width: "100%" }}>
168 <TableCell>File name</TableCell>
169 <TableCell>File size</TableCell>
170 <TableCell>Upload speed</TableCell>
171 <TableCell>Upload progress</TableCell>
172 <TableCell>Delete</TableCell>
177 <TableRow key={f.id}>
178 <TableCell>{f.file.name}</TableCell>
179 <TableCell>{formatFileSize(f.file.size)}</TableCell>
180 <TableCell>{formatUploadSpeed(f.prevLoaded, f.loaded, f.prevTime, f.currentTime)}</TableCell>
181 <TableCell>{formatProgress(f.loaded, f.total)}</TableCell>
185 onClick={(event: React.MouseEvent<HTMLTableCellElement>) => this.onDelete(event, f)}
186 className={disabled ? classnames(classes.deleteButtonDisabled, classes.deleteIcon) : classnames(classes.deleteButton, classes.deleteIcon)}