2017-01-13 03:03:34 +01:00
|
|
|
import React from 'react';
|
|
|
|
import lbry from '../lbry.js';
|
|
|
|
import {Link} from '../component/link.js';
|
|
|
|
import {Icon} from '../component/common.js';
|
|
|
|
import Modal from './modal.js';
|
2017-04-10 14:32:40 +02:00
|
|
|
import {FormField} from './form.js';
|
2017-01-21 22:31:41 +01:00
|
|
|
import {ToolTip} from '../component/tooltip.js';
|
2017-01-13 03:03:34 +01:00
|
|
|
import {DropDownMenu, DropDownMenuItem} from './menu.js';
|
|
|
|
|
2017-03-08 07:50:38 +01:00
|
|
|
const {shell} = require('electron');
|
|
|
|
|
2017-01-13 03:03:34 +01:00
|
|
|
let WatchLink = React.createClass({
|
|
|
|
propTypes: {
|
2017-04-11 03:24:35 +02:00
|
|
|
uri: React.PropTypes.string,
|
2017-02-23 09:45:43 +01:00
|
|
|
downloadStarted: React.PropTypes.bool,
|
|
|
|
},
|
|
|
|
startVideo: function() {
|
2017-04-11 03:24:35 +02:00
|
|
|
window.location = '?watch=' + this.props.uri;
|
2017-01-13 03:03:34 +01:00
|
|
|
},
|
|
|
|
handleClick: function() {
|
|
|
|
this.setState({
|
|
|
|
loading: true,
|
|
|
|
});
|
2017-02-23 09:45:43 +01:00
|
|
|
|
|
|
|
if (this.props.downloadStarted) {
|
|
|
|
this.startVideo();
|
|
|
|
} else {
|
2017-04-11 03:24:35 +02:00
|
|
|
lbry.getCostInfo(this.props.uri, ({cost}) => {
|
2017-02-23 09:45:43 +01:00
|
|
|
lbry.getBalance((balance) => {
|
|
|
|
if (cost > balance) {
|
|
|
|
this.setState({
|
|
|
|
modal: 'notEnoughCredits',
|
|
|
|
loading: false,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.startVideo();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-01-13 03:03:34 +01:00
|
|
|
},
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
modal: null,
|
|
|
|
loading: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
closeModal: function() {
|
|
|
|
this.setState({
|
|
|
|
modal: null,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
return (
|
2017-02-21 07:26:59 +01:00
|
|
|
<div className="button-set-item">
|
2017-04-10 14:32:40 +02:00
|
|
|
<Link button={ this.props.button ? this.props.button : 'alt' } disabled={this.state.loading} label="Watch" icon="icon-play" onClick={this.handleClick} />
|
2017-01-13 03:03:34 +01:00
|
|
|
<Modal contentLabel="Not enough credits" isOpen={this.state.modal == 'notEnoughCredits'} onConfirmed={this.closeModal}>
|
|
|
|
You don't have enough LBRY credits to pay for this stream.
|
|
|
|
</Modal>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-21 22:31:41 +01:00
|
|
|
let FileActionsRow = React.createClass({
|
2017-01-13 03:03:34 +01:00
|
|
|
_isMounted: false,
|
|
|
|
_fileInfoSubscribeId: null,
|
|
|
|
|
|
|
|
propTypes: {
|
2017-04-11 03:24:35 +02:00
|
|
|
uri: React.PropTypes.string,
|
2017-03-08 08:17:16 +01:00
|
|
|
outpoint: React.PropTypes.string.isRequired,
|
2017-02-23 07:14:48 +01:00
|
|
|
metadata: React.PropTypes.oneOfType([React.PropTypes.object, React.PropTypes.string]),
|
2017-04-11 03:24:35 +02:00
|
|
|
contentType: React.PropTypes.string.isRequired,
|
2017-01-13 03:03:34 +01:00
|
|
|
},
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
fileInfo: null,
|
|
|
|
modal: null,
|
|
|
|
menuOpen: false,
|
|
|
|
deleteChecked: false,
|
|
|
|
attemptingDownload: false,
|
2017-01-21 22:31:41 +01:00
|
|
|
attemptingRemove: false
|
2017-01-13 03:03:34 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onFileInfoUpdate: function(fileInfo) {
|
|
|
|
if (this._isMounted) {
|
|
|
|
this.setState({
|
|
|
|
fileInfo: fileInfo ? fileInfo : false,
|
|
|
|
attemptingDownload: fileInfo ? false : this.state.attemptingDownload
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
tryDownload: function() {
|
|
|
|
this.setState({
|
|
|
|
attemptingDownload: true,
|
|
|
|
attemptingRemove: false
|
|
|
|
});
|
2017-04-11 03:24:35 +02:00
|
|
|
lbry.getCostInfo(this.props.uri, ({cost}) => {
|
2017-01-13 03:03:34 +01:00
|
|
|
lbry.getBalance((balance) => {
|
|
|
|
if (cost > balance) {
|
|
|
|
this.setState({
|
|
|
|
modal: 'notEnoughCredits',
|
|
|
|
attemptingDownload: false,
|
|
|
|
});
|
|
|
|
} else {
|
2017-04-11 03:24:35 +02:00
|
|
|
lbry.get({uri: this.props.uri}).then((streamInfo) => {
|
2017-01-13 03:03:34 +01:00
|
|
|
if (streamInfo === null || typeof streamInfo !== 'object') {
|
|
|
|
this.setState({
|
|
|
|
modal: 'timedOut',
|
|
|
|
attemptingDownload: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
closeModal: function() {
|
|
|
|
this.setState({
|
|
|
|
modal: null,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
onDownloadClick: function() {
|
|
|
|
if (!this.state.fileInfo && !this.state.attemptingDownload) {
|
|
|
|
this.tryDownload();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onOpenClick: function() {
|
2017-03-08 07:50:38 +01:00
|
|
|
if (this.state.fileInfo && this.state.fileInfo.download_path) {
|
|
|
|
shell.openItem(this.state.fileInfo.download_path);
|
2017-01-13 03:03:34 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
handleDeleteCheckboxClicked: function(event) {
|
|
|
|
this.setState({
|
|
|
|
deleteChecked: event.target.checked,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
handleRevealClicked: function() {
|
|
|
|
if (this.state.fileInfo && this.state.fileInfo.download_path) {
|
2017-03-08 07:50:38 +01:00
|
|
|
shell.showItemInFolder(this.state.fileInfo.download_path);
|
2017-01-13 03:03:34 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
handleRemoveClicked: function() {
|
|
|
|
this.setState({
|
|
|
|
modal: 'confirmRemove',
|
|
|
|
});
|
|
|
|
},
|
|
|
|
handleRemoveConfirmed: function() {
|
2017-03-08 08:17:16 +01:00
|
|
|
lbry.removeFile(this.props.outpoint, this.state.deleteChecked);
|
2017-01-13 03:03:34 +01:00
|
|
|
this.setState({
|
|
|
|
modal: null,
|
|
|
|
fileInfo: false,
|
|
|
|
attemptingDownload: false
|
|
|
|
});
|
|
|
|
},
|
|
|
|
openMenu: function() {
|
|
|
|
this.setState({
|
|
|
|
menuOpen: !this.state.menuOpen,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
componentDidMount: function() {
|
|
|
|
this._isMounted = true;
|
2017-03-08 08:17:16 +01:00
|
|
|
this._fileInfoSubscribeId = lbry.fileInfoSubscribe(this.props.outpoint, this.onFileInfoUpdate);
|
2017-01-13 03:03:34 +01:00
|
|
|
},
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
this._isMounted = false;
|
|
|
|
if (this._fileInfoSubscribeId) {
|
2017-03-08 08:17:16 +01:00
|
|
|
lbry.fileInfoUnsubscribe(this.props.outpoint, this._fileInfoSubscribeId);
|
2017-01-13 03:03:34 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
if (this.state.fileInfo === null)
|
|
|
|
{
|
2017-01-21 22:31:41 +01:00
|
|
|
return null;
|
2017-01-21 07:52:32 +01:00
|
|
|
}
|
|
|
|
|
2017-01-13 03:03:34 +01:00
|
|
|
const openInFolderMessage = window.navigator.platform.startsWith('Mac') ? 'Open in Finder' : 'Open in Folder',
|
2017-01-21 22:31:41 +01:00
|
|
|
showMenu = !!this.state.fileInfo;
|
2017-01-13 23:18:37 +01:00
|
|
|
|
2017-01-13 03:03:34 +01:00
|
|
|
let linkBlock;
|
2017-01-17 10:04:29 +01:00
|
|
|
if (this.state.fileInfo === false && !this.state.attemptingDownload) {
|
2017-01-13 03:03:34 +01:00
|
|
|
linkBlock = <Link button="text" label="Download" icon="icon-download" onClick={this.onDownloadClick} />;
|
2017-03-27 08:55:51 +02:00
|
|
|
} else if (this.state.attemptingDownload || (!this.state.fileInfo.completed && !this.state.fileInfo.isMine)) {
|
2017-01-13 03:03:34 +01:00
|
|
|
const
|
|
|
|
progress = this.state.fileInfo ? this.state.fileInfo.written_bytes / this.state.fileInfo.total_bytes * 100 : 0,
|
|
|
|
label = this.state.fileInfo ? progress.toFixed(0) + '% complete' : 'Connecting...',
|
2017-01-24 05:06:29 +01:00
|
|
|
labelWithIcon = <span className="button__content"><Icon icon="icon-download" /><span>{label}</span></span>;
|
2017-01-13 03:03:34 +01:00
|
|
|
|
2017-01-21 07:52:32 +01:00
|
|
|
linkBlock = (
|
2017-02-21 07:26:59 +01:00
|
|
|
<div className="faux-button-block file-actions__download-status-bar button-set-item">
|
2017-01-13 03:03:34 +01:00
|
|
|
<div className="faux-button-block file-actions__download-status-bar-overlay" style={{ width: progress + '%' }}>{labelWithIcon}</div>
|
|
|
|
{labelWithIcon}
|
2017-01-21 07:52:32 +01:00
|
|
|
</div>
|
|
|
|
);
|
2017-01-13 03:03:34 +01:00
|
|
|
} else {
|
2017-01-25 04:22:36 +01:00
|
|
|
linkBlock = <Link label="Open" button="text" icon="icon-folder-open" onClick={this.onOpenClick} />;
|
2017-01-13 03:03:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-01-21 22:31:41 +01:00
|
|
|
<div>
|
2017-04-08 08:34:51 +02:00
|
|
|
{this.props.contentType && this.props.contentType.startsWith('video/')
|
2017-04-11 03:24:35 +02:00
|
|
|
? <WatchLink uri={this.props.uri} downloadStarted={!!this.state.fileInfo} />
|
2017-02-23 09:45:43 +01:00
|
|
|
: null}
|
2017-02-21 07:26:59 +01:00
|
|
|
{this.state.fileInfo !== null || this.state.fileInfo.isMine
|
|
|
|
? linkBlock
|
2017-01-13 03:03:34 +01:00
|
|
|
: null}
|
|
|
|
{ showMenu ?
|
|
|
|
<DropDownMenu>
|
|
|
|
<DropDownMenuItem key={0} onClick={this.handleRevealClicked} label={openInFolderMessage} />
|
|
|
|
<DropDownMenuItem key={1} onClick={this.handleRemoveClicked} label="Remove..." />
|
|
|
|
</DropDownMenu> : '' }
|
|
|
|
<Modal isOpen={this.state.modal == 'notEnoughCredits'} contentLabel="Not enough credits"
|
|
|
|
onConfirmed={this.closeModal}>
|
|
|
|
You don't have enough LBRY credits to pay for this stream.
|
|
|
|
</Modal>
|
|
|
|
<Modal isOpen={this.state.modal == 'timedOut'} contentLabel="Download failed"
|
|
|
|
onConfirmed={this.closeModal}>
|
2017-04-11 03:24:35 +02:00
|
|
|
LBRY was unable to download the stream <strong>lbry://{this.props.uri}</strong>.
|
2017-01-13 03:03:34 +01:00
|
|
|
</Modal>
|
|
|
|
<Modal isOpen={this.state.modal == 'confirmRemove'} contentLabel="Not enough credits"
|
|
|
|
type="confirm" confirmButtonLabel="Remove" onConfirmed={this.handleRemoveConfirmed}
|
|
|
|
onAborted={this.closeModal}>
|
|
|
|
<p>Are you sure you'd like to remove <cite>{this.props.metadata.title}</cite> from LBRY?</p>
|
|
|
|
|
|
|
|
<label><FormField type="checkbox" checked={this.state.deleteChecked} onClick={this.handleDeleteCheckboxClicked} /> Delete this file from my computer</label>
|
|
|
|
</Modal>
|
2017-01-21 22:31:41 +01:00
|
|
|
</div>
|
2017-01-13 03:03:34 +01:00
|
|
|
);
|
|
|
|
}
|
2017-01-18 04:36:32 +01:00
|
|
|
});
|
2017-01-21 22:31:41 +01:00
|
|
|
|
|
|
|
export let FileActions = React.createClass({
|
|
|
|
_isMounted: false,
|
|
|
|
_fileInfoSubscribeId: null,
|
|
|
|
|
|
|
|
propTypes: {
|
2017-04-11 03:24:35 +02:00
|
|
|
uri: React.PropTypes.string,
|
2017-03-08 08:17:16 +01:00
|
|
|
outpoint: React.PropTypes.string.isRequired,
|
2017-02-23 07:14:48 +01:00
|
|
|
metadata: React.PropTypes.oneOfType([React.PropTypes.object, React.PropTypes.string]),
|
2017-04-08 08:34:51 +02:00
|
|
|
contentType: React.PropTypes.string,
|
2017-01-21 22:31:41 +01:00
|
|
|
},
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
available: true,
|
|
|
|
forceShowActions: false,
|
2017-01-30 09:21:43 +01:00
|
|
|
fileInfo: null,
|
2017-01-21 22:31:41 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onShowFileActionsRowClicked: function() {
|
|
|
|
this.setState({
|
|
|
|
forceShowActions: true,
|
|
|
|
});
|
|
|
|
},
|
2017-01-30 09:21:43 +01:00
|
|
|
onFileInfoUpdate: function(fileInfo) {
|
2017-02-24 08:30:27 +01:00
|
|
|
if (this.isMounted) {
|
|
|
|
this.setState({
|
|
|
|
fileInfo: fileInfo,
|
2017-03-26 20:30:18 +02:00
|
|
|
});
|
2017-02-24 08:30:27 +01:00
|
|
|
}
|
2017-01-30 09:21:43 +01:00
|
|
|
},
|
2017-01-21 22:31:41 +01:00
|
|
|
componentDidMount: function() {
|
|
|
|
this._isMounted = true;
|
2017-03-08 08:17:16 +01:00
|
|
|
this._fileInfoSubscribeId = lbry.fileInfoSubscribe(this.props.outpoint, this.onFileInfoUpdate);
|
2017-04-11 03:24:35 +02:00
|
|
|
lbry.get_availability({uri: this.props.uri}, (availability) => {
|
2017-02-20 00:19:41 +01:00
|
|
|
if (this._isMounted) {
|
|
|
|
this.setState({
|
|
|
|
available: availability > 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, () => {
|
|
|
|
// Take any error to mean the file is unavailable
|
|
|
|
if (this._isMounted) {
|
|
|
|
this.setState({
|
|
|
|
available: false,
|
|
|
|
});
|
2017-01-21 22:31:41 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
this._isMounted = false;
|
2017-03-26 20:30:18 +02:00
|
|
|
if (this._fileInfoSubscribeId) {
|
|
|
|
lbry.fileInfoUnsubscribe(this.props.outpoint, this._fileInfoSubscribeId);
|
|
|
|
}
|
2017-01-21 22:31:41 +01:00
|
|
|
},
|
|
|
|
render: function() {
|
2017-01-30 09:21:43 +01:00
|
|
|
const fileInfo = this.state.fileInfo;
|
|
|
|
if (fileInfo === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-21 22:31:41 +01:00
|
|
|
return (<section className="file-actions">
|
|
|
|
{
|
2017-01-30 09:21:43 +01:00
|
|
|
fileInfo || this.state.available || this.state.forceShowActions
|
2017-04-11 03:24:35 +02:00
|
|
|
? <FileActionsRow outpoint={this.props.outpoint} metadata={this.props.metadata} uri={this.props.uri}
|
2017-04-08 08:34:51 +02:00
|
|
|
contentType={this.props.contentType} />
|
2017-01-30 09:21:43 +01:00
|
|
|
: <div>
|
2017-04-10 20:12:07 +02:00
|
|
|
<div className="button-set-item empty">Content unavailable.</div>
|
2017-02-21 07:26:59 +01:00
|
|
|
<ToolTip label="Why?"
|
|
|
|
body="The content on LBRY is hosted by its users. It appears there are no users connected that have this file at the moment."
|
|
|
|
className="button-set-item" />
|
2017-02-23 07:28:58 +01:00
|
|
|
<Link label="Try Anyway" onClick={this.onShowFileActionsRowClicked} className="button-text button-set-item" />
|
2017-01-30 09:21:43 +01:00
|
|
|
</div>
|
2017-01-21 22:31:41 +01:00
|
|
|
}
|
|
|
|
</section>);
|
|
|
|
}
|
|
|
|
});
|