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

110 lines
2.7 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';
2018-06-20 05:55:25 +02:00
import ToolTip from 'component/common/tooltip';
import analytics from 'analytics';
import type { Claim } from 'types/claim';
2017-09-08 05:15:05 +02:00
2018-05-21 22:30:00 +02:00
type Props = {
claim: Claim,
2018-05-21 22:30:00 +02:00
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,
pause: () => void,
2018-05-21 22:30:00 +02:00
};
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,
pause,
2018-09-19 17:55:14 +02:00
claim,
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);
pause();
2018-05-21 22:30:00 +02:00
}
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
2018-06-20 05:55:25 +02:00
? (fileInfo.written_bytes / fileInfo.total_bytes) * 100
2018-03-26 23:32:43 +02:00
: 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-06-20 05:55:25 +02:00
<ToolTip onComponent body={__('Download')}>
<Button
button="alt"
icon={icons.DOWNLOAD}
iconColor="green"
2018-06-20 05:55:25 +02:00
onClick={() => {
purchaseUri(uri);
const { name, claim_id: claimId, nout, txid } = claim;
// // ideally outpoint would exist inside of claim information
// // we can use it after https://github.com/lbryio/lbry/issues/1306 is addressed
const outpoint = `${txid}:${nout}`;
analytics.apiLogView(`${name}#${claimId}`, outpoint, claimId);
2018-06-20 05:55:25 +02:00
}}
/>
</ToolTip>
);
2017-09-08 05:15:05 +02:00
} else if (fileInfo && fileInfo.download_path) {
return (
2018-06-20 05:55:25 +02:00
<ToolTip onComponent body={__('Open file')}>
<Button button="alt" iconColor="green" icon={icons.LOCAL} onClick={() => openFile()} />
2018-06-20 05:55:25 +02:00
</ToolTip>
2017-09-08 05:15:05 +02:00
);
}
return null;
}
}
export default FileDownloadLink;