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

96 lines
2.8 KiB
React
Raw Normal View History

2018-05-21 22:30:00 +02:00
// @flow
2018-11-26 02:21:25 +01:00
import * as ICONS from 'constants/icons';
import * as MODALS from 'constants/modal_types';
import React from 'react';
2018-03-26 23:32:43 +02:00
import Button from 'component/button';
2018-06-20 05:55:25 +02:00
import ToolTip from 'component/common/tooltip';
import analytics from 'analytics';
2017-09-08 05:15:05 +02:00
2018-05-21 22:30:00 +02:00
type Props = {
2019-04-24 16:02:08 +02:00
claim: StreamClaim,
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,
status: string,
2018-05-21 22:30:00 +02:00
},
loading: boolean,
costInfo: ?{},
restartDownload: (string, number) => void,
openModal: (id: string, { path: string }) => void,
2018-05-21 22:30:00 +02:00
purchaseUri: string => void,
pause: () => void,
2018-05-21 22:30:00 +02:00
};
class FileDownloadLink extends React.PureComponent<Props> {
componentDidMount() {
const { fileInfo, uri, restartDownload } = this.props;
2017-09-08 05:15:05 +02:00
if (
fileInfo &&
!fileInfo.completed &&
fileInfo.status === 'running' &&
2017-09-08 05:15:05 +02:00
fileInfo.written_bytes !== false &&
fileInfo.written_bytes < fileInfo.total_bytes
) {
// This calls file list to show the percentage
2017-09-08 05:15:05 +02:00
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, openModal, purchaseUri, costInfo, loading, pause, claim } = this.props;
2017-12-18 18:02:55 +01:00
2017-09-08 05:15:05 +02:00
if (loading || downloading) {
2019-05-07 23:38:29 +02:00
const progress = fileInfo && fileInfo.written_bytes ? (fileInfo.written_bytes / fileInfo.total_bytes) * 100 : 0;
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>;
2019-02-18 18:33:02 +01:00
} else if ((fileInfo === null && !downloading) || (fileInfo && !fileInfo.download_path)) {
2017-09-08 05:15:05 +02:00
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 (
<ToolTip label={__('Add to your library')}>
2018-06-20 05:55:25 +02:00
<Button
button="link"
2018-11-26 02:21:25 +01:00
icon={ICONS.DOWNLOAD}
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 (
<ToolTip label={__('Open file')}>
<Button
button="link"
icon={ICONS.EXTERNAL}
2019-05-12 19:59:57 +02:00
onClick={() => {
pause();
openModal(MODALS.CONFIRM_EXTERNAL_RESOURCE, { path: fileInfo.download_path });
}}
/>
2018-06-20 05:55:25 +02:00
</ToolTip>
2017-09-08 05:15:05 +02:00
);
}
return null;
}
}
export default FileDownloadLink;