lbry-desktop/src/renderer/component/fileDownloadLink/view.jsx

102 lines
2.2 KiB
React
Raw Normal View History

2018-05-21 22:30:00 +02:00
// @flow
import React from 'react';
2018-03-26 23:32:43 +02:00
import Button from 'component/button';
import * as icons from 'constants/icons';
2017-09-08 05:15:05 +02:00
2018-05-21 22:30:00 +02:00
type Props = {
uri: string,
downloading: boolean,
fileInfo: ?{
written_bytes: number,
total_bytes: number,
outpoint: number,
download_path: string,
2018-05-23 02:29:29 +02:00
completed: boolean,
2018-05-21 22:30:00 +02:00
},
loading: boolean,
costInfo: ?{},
restartDownload: (string, number) => void,
openInShell: string => void,
purchaseUri: string => void,
doPause: () => void,
};
class FileDownloadLink extends React.PureComponent<Props> {
2018-05-23 02:29:29 +02:00
componentWillUpdate() {
const { downloading, fileInfo, uri, restartDownload } = this.props;
2017-09-08 05:15:05 +02:00
if (
!downloading &&
fileInfo &&
!fileInfo.completed &&
fileInfo.written_bytes !== false &&
fileInfo.written_bytes < fileInfo.total_bytes
) {
restartDownload(uri, fileInfo.outpoint);
}
}
2018-05-23 02:29:29 +02:00
uri: ?string;
2017-09-08 05:15:05 +02:00
render() {
const {
fileInfo,
downloading,
uri,
openInShell,
purchaseUri,
2017-09-08 05:15:05 +02:00
costInfo,
loading,
2017-12-21 00:38:11 +01:00
doPause,
2017-09-08 05:15:05 +02:00
} = this.props;
2017-12-18 18:02:55 +01:00
const openFile = () => {
2018-05-21 22:30:00 +02:00
if (fileInfo) {
openInShell(fileInfo.download_path);
doPause();
}
2017-12-18 18:02:55 +01:00
};
2017-09-08 05:15:05 +02:00
if (loading || downloading) {
2017-11-24 15:31:05 +01:00
const progress =
2018-03-26 23:32:43 +02:00
fileInfo && fileInfo.written_bytes
? fileInfo.written_bytes / fileInfo.total_bytes * 100
: 0;
2018-05-21 22:30:00 +02:00
const label = fileInfo
? __('Downloading: ') + progress.toFixed(0) + __('% complete')
: __('Connecting...');
2017-09-08 05:15:05 +02:00
2018-05-21 22:30:00 +02:00
return <span className="file-download">{label}</span>;
2017-09-08 05:15:05 +02:00
} else if (fileInfo === null && !downloading) {
if (!costInfo) {
2018-03-26 23:32:43 +02:00
return null;
2017-09-08 05:15:05 +02:00
}
2018-03-26 23:32:43 +02:00
return (
2018-03-26 23:32:43 +02:00
<Button
2018-05-21 22:30:00 +02:00
button="alt"
label={__('Download')}
2018-03-26 23:32:43 +02:00
icon={icons.DOWNLOAD}
iconColor="purple"
onClick={() => {
purchaseUri(uri);
}}
/>
);
2017-09-08 05:15:05 +02:00
} else if (fileInfo && fileInfo.download_path) {
return (
<Button
button="alt"
iconColor="purple"
label={__('Open File')}
icon={icons.OPEN}
onClick={() => openFile()}
/>
2017-09-08 05:15:05 +02:00
);
}
return null;
}
}
export default FileDownloadLink;