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';
|
|
|
|
import FormField from './form.js';
|
|
|
|
import {DropDownMenu, DropDownMenuItem} from './menu.js';
|
|
|
|
|
|
|
|
let WatchLink = React.createClass({
|
|
|
|
propTypes: {
|
|
|
|
streamName: React.PropTypes.string,
|
|
|
|
},
|
|
|
|
handleClick: function() {
|
|
|
|
this.setState({
|
|
|
|
loading: true,
|
|
|
|
})
|
|
|
|
lbry.getCostInfoForName(this.props.streamName, ({cost}) => {
|
|
|
|
lbry.getBalance((balance) => {
|
|
|
|
if (cost > balance) {
|
|
|
|
this.setState({
|
|
|
|
modal: 'notEnoughCredits',
|
|
|
|
loading: false,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
window.location = '?watch=' + this.props.streamName;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
modal: null,
|
|
|
|
loading: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
closeModal: function() {
|
|
|
|
this.setState({
|
|
|
|
modal: null,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
return (
|
|
|
|
<div className="button-container">
|
|
|
|
<Link button="primary" disabled={this.state.loading} label="Watch" icon="icon-play" onClick={this.handleClick} />
|
|
|
|
<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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export let FileActions = React.createClass({
|
|
|
|
_isMounted: false,
|
|
|
|
_fileInfoSubscribeId: null,
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
streamName: React.PropTypes.string,
|
2017-01-13 23:18:37 +01:00
|
|
|
sdHash: React.PropTypes.string.isRequired,
|
2017-01-13 03:03:34 +01:00
|
|
|
metadata: React.PropTypes.object,
|
|
|
|
path: React.PropTypes.string,
|
|
|
|
hidden: React.PropTypes.bool,
|
|
|
|
deleteChecked: React.PropTypes.bool,
|
2017-01-18 04:36:32 +01:00
|
|
|
onRemove: React.PropTypes.func,
|
2017-01-13 03:03:34 +01:00
|
|
|
},
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
fileInfo: null,
|
|
|
|
modal: null,
|
|
|
|
menuOpen: false,
|
|
|
|
deleteChecked: false,
|
|
|
|
attemptingDownload: false,
|
|
|
|
attemptingRemove: false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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
|
|
|
|
});
|
|
|
|
lbry.getCostInfoForName(this.props.streamName, ({cost}) => {
|
|
|
|
lbry.getBalance((balance) => {
|
|
|
|
if (cost > balance) {
|
|
|
|
this.setState({
|
|
|
|
modal: 'notEnoughCredits',
|
|
|
|
attemptingDownload: false,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
lbry.getStream(this.props.streamName, (streamInfo) => {
|
|
|
|
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() {
|
|
|
|
if (this.state.fileInfo && this.state.fileInfo.completed) {
|
2017-01-17 11:31:58 +01:00
|
|
|
lbry.openFile(this.props.sdHash);
|
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-01-17 11:31:58 +01:00
|
|
|
lbry.revealFile(this.props.sdHash);
|
2017-01-13 03:03:34 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
handleRemoveClicked: function() {
|
|
|
|
this.setState({
|
|
|
|
modal: 'confirmRemove',
|
|
|
|
});
|
|
|
|
},
|
|
|
|
handleRemoveConfirmed: function() {
|
2017-01-13 23:18:37 +01:00
|
|
|
if (this.props.streamName) {
|
2017-01-17 10:04:29 +01:00
|
|
|
lbry.removeFile(this.props.sdHash, this.props.streamName, this.state.deleteChecked);
|
2017-01-13 23:18:37 +01:00
|
|
|
} else {
|
|
|
|
alert('this file cannot be deleted because lbry is a retarded piece of shit');
|
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-01-13 23:18:37 +01:00
|
|
|
this._fileInfoSubscribeId = lbry.fileInfoSubscribe(this.props.sdHash, this.onFileInfoUpdate);
|
2017-01-13 03:03:34 +01:00
|
|
|
},
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
this._isMounted = false;
|
|
|
|
if (this._fileInfoSubscribeId) {
|
2017-01-13 23:18:37 +01:00
|
|
|
lbry.fileInfoUnsubscribe(this.props.sdHash, this._fileInfoSubscribeId);
|
2017-01-13 03:03:34 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
render: function() {
|
|
|
|
if (this.state.fileInfo === null)
|
|
|
|
{
|
|
|
|
return <section className="file-actions--stub"></section>;
|
|
|
|
}
|
|
|
|
const openInFolderMessage = window.navigator.platform.startsWith('Mac') ? 'Open in Finder' : 'Open in Folder',
|
2017-01-17 10:04:29 +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} />;
|
|
|
|
} else if (this.state.attemptingDownload || !this.state.fileInfo.completed) {
|
|
|
|
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-16 06:29:28 +01:00
|
|
|
labelWithIcon = <span className="button__content"><Icon icon="icon-download" />{label}</span>;
|
2017-01-13 03:03:34 +01:00
|
|
|
|
|
|
|
linkBlock =
|
|
|
|
<div className="faux-button-block file-actions__download-status-bar">
|
|
|
|
<div className="faux-button-block file-actions__download-status-bar-overlay" style={{ width: progress + '%' }}>{labelWithIcon}</div>
|
|
|
|
{labelWithIcon}
|
|
|
|
</div>;
|
|
|
|
} else {
|
|
|
|
linkBlock = <Link button="text" label="Open" icon="icon-folder-open" onClick={this.onOpenClick} />;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<section className="file-actions">
|
2017-01-18 04:57:16 +01:00
|
|
|
{(this.props.metadata.content_type && this.props.metadata.content_type.startsWith('video/')) ? <WatchLink streamName={this.props.streamName} /> : null}
|
2017-01-13 03:03:34 +01:00
|
|
|
{this.state.fileInfo !== null || this.state.fileInfo.isMine ?
|
|
|
|
<div className="button-container">{linkBlock}</div>
|
|
|
|
: 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}>
|
|
|
|
LBRY was unable to download the stream <strong>lbry://{this.props.streamName}</strong>.
|
|
|
|
</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>
|
|
|
|
</section>
|
|
|
|
);
|
|
|
|
}
|
2017-01-18 04:36:32 +01:00
|
|
|
});
|