2015-12-02 18:16:16 +00:00
|
|
|
/*
|
2021-03-05 20:06:53 +00:00
|
|
|
Copyright 2015, 2016, 2019, 2021 The Matrix.org Foundation C.I.C.
|
2015-12-02 18:16:16 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-08-30 09:34:59 +00:00
|
|
|
import React from 'react';
|
2021-03-05 23:29:14 +00:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2019-04-08 16:53:39 +00:00
|
|
|
import ContentMessages from '../../ContentMessages';
|
2020-05-14 02:41:41 +00:00
|
|
|
import dis from "../../dispatcher/dispatcher";
|
2019-12-20 21:41:07 +00:00
|
|
|
import filesize from "filesize";
|
2017-06-07 10:40:46 +00:00
|
|
|
import { _t } from '../../languageHandler';
|
2021-03-05 23:29:14 +00:00
|
|
|
import { ActionPayload } from "../../dispatcher/payloads";
|
|
|
|
import { Action } from "../../dispatcher/actions";
|
2021-03-05 21:14:43 +00:00
|
|
|
import ProgressBar from "../views/elements/ProgressBar";
|
2021-03-05 21:24:02 +00:00
|
|
|
import AccessibleButton from "../views/elements/AccessibleButton";
|
2021-03-05 23:29:14 +00:00
|
|
|
import { IUpload } from "../../models/IUpload";
|
2021-03-09 02:35:10 +00:00
|
|
|
import {replaceableComponent} from "../../utils/replaceableComponent";
|
2015-12-02 18:16:16 +00:00
|
|
|
|
2021-03-05 20:06:53 +00:00
|
|
|
interface IProps {
|
|
|
|
room: Room;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
2021-03-05 21:24:02 +00:00
|
|
|
currentUpload?: IUpload;
|
|
|
|
uploadsHere: IUpload[];
|
2021-03-05 20:06:53 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 02:35:10 +00:00
|
|
|
@replaceableComponent("structures.UploadBar")
|
2021-03-05 20:06:53 +00:00
|
|
|
export default class UploadBar extends React.Component<IProps, IState> {
|
|
|
|
private dispatcherRef: string;
|
|
|
|
private mounted: boolean;
|
2015-12-02 18:16:16 +00:00
|
|
|
|
2021-03-05 21:24:02 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {uploadsHere: []};
|
|
|
|
}
|
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
componentDidMount() {
|
2017-03-16 14:19:17 +00:00
|
|
|
this.dispatcherRef = dis.register(this.onAction);
|
2015-12-02 18:16:16 +00:00
|
|
|
this.mounted = true;
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2015-12-02 18:16:16 +00:00
|
|
|
|
2020-08-29 11:14:16 +00:00
|
|
|
componentWillUnmount() {
|
2015-12-02 18:16:16 +00:00
|
|
|
this.mounted = false;
|
2017-03-16 14:19:17 +00:00
|
|
|
dis.unregister(this.dispatcherRef);
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2015-12-02 18:16:16 +00:00
|
|
|
|
2021-03-05 20:20:50 +00:00
|
|
|
private onAction = (payload: ActionPayload) => {
|
2015-12-02 18:16:16 +00:00
|
|
|
switch (payload.action) {
|
2021-03-05 21:24:02 +00:00
|
|
|
case Action.UploadStarted:
|
2021-03-05 20:20:50 +00:00
|
|
|
case Action.UploadProgress:
|
|
|
|
case Action.UploadFinished:
|
|
|
|
case Action.UploadCanceled:
|
2021-03-05 21:24:02 +00:00
|
|
|
case Action.UploadFailed: {
|
|
|
|
if (!this.mounted) return;
|
|
|
|
const uploads = ContentMessages.sharedInstance().getCurrentUploads();
|
|
|
|
const uploadsHere = uploads.filter(u => u.roomId === this.props.room.roomId);
|
|
|
|
this.setState({currentUpload: uploadsHere[0], uploadsHere});
|
2015-12-02 18:16:16 +00:00
|
|
|
break;
|
2021-03-05 21:24:02 +00:00
|
|
|
}
|
2015-12-02 18:16:16 +00:00
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2015-12-02 18:16:16 +00:00
|
|
|
|
2021-03-05 21:24:02 +00:00
|
|
|
private onCancelClick = (ev) => {
|
|
|
|
ev.preventDefault();
|
|
|
|
ContentMessages.sharedInstance().cancelUpload(this.state.currentUpload.promise);
|
|
|
|
};
|
2016-04-14 18:11:20 +00:00
|
|
|
|
2021-03-05 21:24:02 +00:00
|
|
|
render() {
|
|
|
|
if (!this.state.currentUpload) {
|
2021-03-05 21:14:43 +00:00
|
|
|
return null;
|
2015-12-02 18:16:16 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 10:40:46 +00:00
|
|
|
// MUST use var name 'count' for pluralization to kick in
|
2020-10-30 18:18:17 +00:00
|
|
|
const uploadText = _t(
|
2021-03-05 21:14:43 +00:00
|
|
|
"Uploading %(filename)s and %(count)s others", {
|
2021-03-05 21:24:02 +00:00
|
|
|
filename: this.state.currentUpload.fileName,
|
|
|
|
count: this.state.uploadsHere.length - 1,
|
2021-03-05 21:14:43 +00:00
|
|
|
},
|
2020-10-30 18:18:17 +00:00
|
|
|
);
|
2021-03-05 21:42:15 +00:00
|
|
|
|
2021-03-05 21:24:02 +00:00
|
|
|
const uploadSize = filesize(this.state.currentUpload.total);
|
2015-12-02 18:16:16 +00:00
|
|
|
return (
|
|
|
|
<div className="mx_UploadBar">
|
2021-03-05 21:42:15 +00:00
|
|
|
<div className="mx_UploadBar_filename">{uploadText} ({uploadSize})</div>
|
2021-03-05 21:24:02 +00:00
|
|
|
<AccessibleButton onClick={this.onCancelClick} className='mx_UploadBar_cancel' />
|
|
|
|
<ProgressBar value={this.state.currentUpload.loaded} max={this.state.currentUpload.total} />
|
2015-12-02 18:16:16 +00:00
|
|
|
</div>
|
|
|
|
);
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
|
|
|
}
|