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

110 lines
2.6 KiB
React
Raw Normal View History

import React from 'react';
import { BusyMessage } from 'component/common';
import Icon from 'component/icon';
import Link from 'component/link';
2017-09-08 05:15:05 +02:00
class FileDownloadLink extends React.PureComponent {
componentWillMount() {
this.checkAvailability(this.props.uri);
}
componentWillReceiveProps(nextProps) {
this.checkAvailability(nextProps.uri);
this.restartDownload(nextProps);
}
restartDownload(props) {
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);
}
}
checkAvailability(uri) {
if (!this._uri || uri !== this._uri) {
this._uri = uri;
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 = () => {
openInShell(fileInfo.download_path);
2017-12-21 00:38:11 +01:00
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 =
fileInfo && fileInfo.written_bytes
? fileInfo.written_bytes / fileInfo.total_bytes * 100
: 0,
label = fileInfo ? progress.toFixed(0) + __('% complete') : __('Connecting...'),
2017-09-08 05:15:05 +02:00
labelWithIcon = (
<span className="button__content">
<Icon icon="icon-download" />
2017-11-24 15:31:05 +01:00
<span>{label}</span>
2017-09-08 05:15:05 +02:00
</span>
);
return (
<div className="faux-button-block file-download button-set-item">
2017-09-08 05:15:05 +02:00
<div
className="faux-button-block file-download__overlay"
style={{ width: `${progress}%` }}
2017-09-08 05:15:05 +02:00
>
{labelWithIcon}
</div>
{labelWithIcon}
</div>
);
} else if (fileInfo === null && !downloading) {
if (!costInfo) {
return <BusyMessage message={__('Fetching cost info')} />;
2017-09-08 05:15:05 +02:00
}
return (
<Link
button="text"
label={__('Download')}
icon="icon-download"
className="no-underline"
onClick={() => {
purchaseUri(uri);
}}
/>
);
2017-09-08 05:15:05 +02:00
} else if (fileInfo && fileInfo.download_path) {
return (
<Link
label={__('Open')}
2017-09-08 05:15:05 +02:00
button="text"
icon="icon-external-link-square"
2017-10-05 06:13:06 +02:00
className="no-underline"
2017-12-18 18:02:55 +01:00
onClick={() => openFile()}
2017-09-08 05:15:05 +02:00
/>
);
}
return null;
}
}
export default FileDownloadLink;