2018-05-21 22:30:00 +02:00
|
|
|
// @flow
|
2018-11-26 02:21:25 +01:00
|
|
|
import * as ICONS from 'constants/icons';
|
2019-05-12 16:46:14 +02:00
|
|
|
import * as MODALS from 'constants/modal_types';
|
2017-12-21 22:08:54 +01:00
|
|
|
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';
|
2018-08-22 19:17:11 +02:00
|
|
|
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,
|
2019-02-21 23:45:17 +01:00
|
|
|
status: string,
|
2018-05-21 22:30:00 +02:00
|
|
|
},
|
|
|
|
loading: boolean,
|
|
|
|
costInfo: ?{},
|
|
|
|
restartDownload: (string, number) => void,
|
2019-05-12 16:46:14 +02:00
|
|
|
openModal: (id: string, { path: string }) => void,
|
2018-05-21 22:30:00 +02:00
|
|
|
purchaseUri: string => void,
|
2018-07-31 15:29:28 +02:00
|
|
|
pause: () => void,
|
2018-05-21 22:30:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class FileDownloadLink extends React.PureComponent<Props> {
|
2019-02-21 23:45:17 +01:00
|
|
|
componentDidMount() {
|
|
|
|
const { fileInfo, uri, restartDownload } = this.props;
|
2017-09-08 05:15:05 +02:00
|
|
|
if (
|
|
|
|
fileInfo &&
|
|
|
|
!fileInfo.completed &&
|
2019-02-21 23:45:17 +01:00
|
|
|
fileInfo.status === 'running' &&
|
2017-09-08 05:15:05 +02:00
|
|
|
fileInfo.written_bytes !== false &&
|
|
|
|
fileInfo.written_bytes < fileInfo.total_bytes
|
|
|
|
) {
|
2019-02-21 23:45:17 +01:00
|
|
|
// 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() {
|
2019-05-12 16:46:14 +02:00
|
|
|
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
|
|
|
|
2017-12-21 22:08:54 +01:00
|
|
|
return (
|
2019-06-28 09:27:55 +02:00
|
|
|
<ToolTip label={__('Add to your library')}>
|
2018-06-20 05:55:25 +02:00
|
|
|
<Button
|
2019-06-28 09:27:55 +02:00
|
|
|
button="link"
|
2018-11-26 02:21:25 +01:00
|
|
|
icon={ICONS.DOWNLOAD}
|
2018-06-20 05:55:25 +02:00
|
|
|
onClick={() => {
|
|
|
|
purchaseUri(uri);
|
2018-08-22 19:17:11 +02:00
|
|
|
|
|
|
|
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-12-21 22:08:54 +01:00
|
|
|
);
|
2017-09-08 05:15:05 +02:00
|
|
|
} else if (fileInfo && fileInfo.download_path) {
|
|
|
|
return (
|
2019-06-28 09:27:55 +02:00
|
|
|
<ToolTip label={__('Open file')}>
|
2019-05-12 16:46:14 +02:00
|
|
|
<Button
|
2019-06-28 09:27:55 +02:00
|
|
|
button="link"
|
2019-05-12 16:46:14 +02:00
|
|
|
icon={ICONS.EXTERNAL}
|
2019-05-12 19:59:57 +02:00
|
|
|
onClick={() => {
|
|
|
|
pause();
|
|
|
|
openModal(MODALS.CONFIRM_EXTERNAL_RESOURCE, { path: fileInfo.download_path });
|
|
|
|
}}
|
2019-05-12 16:46:14 +02:00
|
|
|
/>
|
2018-06-20 05:55:25 +02:00
|
|
|
</ToolTip>
|
2017-09-08 05:15:05 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FileDownloadLink;
|