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

112 lines
2.5 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,
},
loading: boolean,
costInfo: ?{},
restartDownload: (string, number) => void,
checkAvailability: string => void,
openInShell: string => void,
purchaseUri: string => void,
doPause: () => void,
};
class FileDownloadLink extends React.PureComponent<Props> {
2017-09-08 05:15:05 +02:00
componentWillMount() {
this.checkAvailability(this.props.uri);
}
2018-05-21 22:30:00 +02:00
componentWillReceiveProps(nextProps: Props) {
2017-09-08 05:15:05 +02:00
this.checkAvailability(nextProps.uri);
this.restartDownload(nextProps);
}
2018-05-21 22:30:00 +02:00
uri: ?string;
restartDownload = (props: Props) => {
2017-09-08 05:15:05 +02:00
const { downloading, fileInfo, uri, restartDownload } = props;
if (
!downloading &&
fileInfo &&
!fileInfo.completed &&
fileInfo.written_bytes !== false &&
fileInfo.written_bytes < fileInfo.total_bytes
) {
restartDownload(uri, fileInfo.outpoint);
}
2018-05-21 22:30:00 +02:00
};
2017-09-08 05:15:05 +02:00
2018-05-21 22:30:00 +02:00
checkAvailability(uri: string) {
if (!this.uri || uri !== this.uri) {
this.uri = uri;
2017-09-08 05:15:05 +02:00
this.props.checkAvailability(uri);
}
}
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}
onClick={() => {
purchaseUri(uri);
}}
/>
);
2017-09-08 05:15:05 +02:00
} else if (fileInfo && fileInfo.download_path) {
return (
2018-05-21 22:30:00 +02:00
<Button button="alt" label={__('Open File')} icon={icons.OPEN} onClick={() => openFile()} />
2017-09-08 05:15:05 +02:00
);
}
return null;
}
}
export default FileDownloadLink;