2016-11-22 21:19:08 +01:00
|
|
|
import React from 'react';
|
|
|
|
import lbry from '../lbry.js';
|
2017-01-09 11:48:04 +01:00
|
|
|
import {Link} from '../component/link.js';
|
2016-12-28 05:08:27 +01:00
|
|
|
import FormField from '../component/form.js';
|
2016-12-26 06:59:15 +01:00
|
|
|
import FileTile from '../component/file-tile.js';
|
2016-11-22 21:19:08 +01:00
|
|
|
import Modal from '../component/modal.js';
|
|
|
|
import {BusyMessage, Thumbnail} from '../component/common.js';
|
|
|
|
|
2016-05-10 12:36:54 +02:00
|
|
|
var MyFilesPage = React.createClass({
|
2016-09-16 18:46:15 +02:00
|
|
|
_fileTimeout: null,
|
2016-12-13 19:11:01 +01:00
|
|
|
_fileInfoCheckRate: 300,
|
2016-09-16 18:46:15 +02:00
|
|
|
_fileInfoCheckNum: 0,
|
2016-11-18 11:54:57 +01:00
|
|
|
_sortFunctions: {
|
|
|
|
date: function(filesInfo) {
|
|
|
|
return filesInfo.reverse();
|
|
|
|
},
|
|
|
|
title: function(filesInfo) {
|
|
|
|
return filesInfo.sort(function(a, b) {
|
|
|
|
return ((a.metadata ? a.metadata.title.toLowerCase() : a.name) >
|
|
|
|
(b.metadata ? b.metadata.title.toLowerCase() : b.name));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
filename: function(filesInfo) {
|
|
|
|
return filesInfo.sort(function(a, b) {
|
|
|
|
return (a.file_name.toLowerCase() >
|
|
|
|
b.file_name.toLowerCase());
|
|
|
|
});
|
|
|
|
},
|
|
|
|
},
|
2016-09-16 18:46:15 +02:00
|
|
|
|
2016-05-10 12:36:54 +02:00
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
filesInfo: null,
|
2016-11-09 13:00:25 +01:00
|
|
|
publishedFilesSdHashes: null,
|
2017-01-06 12:41:31 +01:00
|
|
|
filesAvailable: null,
|
2016-11-18 11:56:55 +01:00
|
|
|
sortBy: 'date',
|
2016-05-10 12:36:54 +02:00
|
|
|
};
|
|
|
|
},
|
2016-09-23 11:56:01 +02:00
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
show: null,
|
|
|
|
};
|
|
|
|
},
|
2016-08-08 00:13:17 +02:00
|
|
|
componentDidMount: function() {
|
|
|
|
document.title = "My Files";
|
|
|
|
},
|
2016-05-10 12:36:54 +02:00
|
|
|
componentWillMount: function() {
|
2016-11-09 13:00:25 +01:00
|
|
|
if (this.props.show == 'downloaded') {
|
|
|
|
this.getPublishedFilesSdHashes(() => {
|
|
|
|
this.updateFilesInfo();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.updateFilesInfo();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getPublishedFilesSdHashes: function(callback) {
|
|
|
|
// Determines which files were published by the user and saves their SD hashes in
|
|
|
|
// this.state.publishedFilesSdHashes. Used on the Downloads page to filter out claims published
|
|
|
|
// by the user.
|
|
|
|
var publishedFilesSdHashes = [];
|
|
|
|
lbry.getMyClaims((claimsInfo) => {
|
|
|
|
for (let claimInfo of claimsInfo) {
|
|
|
|
let metadata = JSON.parse(claimInfo.value);
|
|
|
|
publishedFilesSdHashes.push(metadata.sources.lbry_sd_hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
publishedFilesSdHashes: publishedFilesSdHashes,
|
|
|
|
});
|
|
|
|
callback();
|
|
|
|
});
|
2016-05-14 14:26:54 +02:00
|
|
|
},
|
2016-08-08 02:57:12 +02:00
|
|
|
componentWillUnmount: function() {
|
2016-09-16 18:46:15 +02:00
|
|
|
if (this._fileTimeout)
|
2016-08-08 02:57:12 +02:00
|
|
|
{
|
2016-09-16 18:46:15 +02:00
|
|
|
clearTimeout(this._fileTimeout);
|
2016-08-08 02:57:12 +02:00
|
|
|
}
|
|
|
|
},
|
2016-11-18 11:56:55 +01:00
|
|
|
handleSortChanged: function(event) {
|
|
|
|
this.setState({
|
|
|
|
sortBy: event.target.value,
|
|
|
|
});
|
|
|
|
},
|
2016-05-14 14:26:54 +02:00
|
|
|
updateFilesInfo: function() {
|
2016-11-09 13:00:25 +01:00
|
|
|
this._fileInfoCheckNum += 1;
|
2016-09-16 18:46:15 +02:00
|
|
|
|
2016-11-09 13:00:25 +01:00
|
|
|
if (this.props.show == 'published') {
|
|
|
|
// We're in the Published tab, so populate this.state.filesInfo with data from the user's claims
|
|
|
|
lbry.getMyClaims((claimsInfo) => {
|
2017-01-04 01:41:32 +01:00
|
|
|
/**
|
|
|
|
* Build newFilesInfo as a sparse array and drop elements in at the same position they
|
|
|
|
* occur in claimsInfo, so the order is preserved even if the API calls inside this loop
|
|
|
|
* return out of order.
|
|
|
|
*/
|
|
|
|
|
|
|
|
let newFilesInfo = Array(claimsInfo.length);
|
2016-11-09 13:00:25 +01:00
|
|
|
let claimInfoProcessedCount = 0;
|
2017-01-04 01:41:32 +01:00
|
|
|
for (let [i, claimInfo] of claimsInfo.entries()) {
|
2016-11-09 13:00:25 +01:00
|
|
|
let metadata = JSON.parse(claimInfo.value);
|
|
|
|
lbry.getFileInfoBySdHash(metadata.sources.lbry_sd_hash, (fileInfo) => {
|
|
|
|
claimInfoProcessedCount++;
|
|
|
|
if (fileInfo !== false) {
|
2017-01-04 01:41:32 +01:00
|
|
|
newFilesInfo[i] = fileInfo;
|
2016-11-09 13:00:25 +01:00
|
|
|
}
|
|
|
|
if (claimInfoProcessedCount >= claimsInfo.length) {
|
2017-01-04 01:41:32 +01:00
|
|
|
/**
|
|
|
|
* newFilesInfo may have gaps from claims that don't have associated files in
|
|
|
|
* lbrynet, so filter out any missing elements
|
|
|
|
*/
|
2017-01-04 01:09:46 +01:00
|
|
|
this.setState({
|
2017-01-04 01:41:32 +01:00
|
|
|
filesInfo: newFilesInfo.filter(function() { return true }),
|
2017-01-04 01:09:46 +01:00
|
|
|
});
|
2016-11-09 13:00:25 +01:00
|
|
|
|
|
|
|
this._fileTimeout = setTimeout(() => { this.updateFilesInfo() }, 1000);
|
2016-09-16 18:46:15 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-11-09 13:00:25 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// We're in the Downloaded tab, so populate this.state.filesInfo with files the user has in
|
|
|
|
// lbrynet, with published files filtered out.
|
|
|
|
lbry.getFilesInfo((filesInfo) => {
|
2017-01-04 01:09:46 +01:00
|
|
|
this.setState({
|
|
|
|
filesInfo: filesInfo.filter(({sd_hash}) => {
|
|
|
|
return this.state.publishedFilesSdHashes.indexOf(sd_hash) == -1;
|
|
|
|
}),
|
|
|
|
});
|
2016-09-16 18:46:15 +02:00
|
|
|
|
2016-11-09 13:00:25 +01:00
|
|
|
let newFilesAvailable;
|
|
|
|
if (!(this._fileInfoCheckNum % this._fileInfoCheckRate)) {
|
|
|
|
// Time to update file availability status
|
2016-09-16 18:46:15 +02:00
|
|
|
|
2016-11-09 13:00:25 +01:00
|
|
|
newFilesAvailable = {};
|
|
|
|
let filePeersCheckCount = 0;
|
|
|
|
for (let {sd_hash} of filesInfo) {
|
|
|
|
lbry.getPeersForBlobHash(sd_hash, (peers) => {
|
|
|
|
filePeersCheckCount++;
|
|
|
|
newFilesAvailable[sd_hash] = peers.length >= 0;
|
|
|
|
if (filePeersCheckCount >= filesInfo.length) {
|
|
|
|
this.setState({
|
|
|
|
filesAvailable: newFilesAvailable,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-09-16 18:46:15 +02:00
|
|
|
|
2016-11-09 13:00:25 +01:00
|
|
|
this._fileTimeout = setTimeout(() => { this.updateFilesInfo() }, 1000);
|
|
|
|
})
|
|
|
|
}
|
2016-05-10 12:36:54 +02:00
|
|
|
},
|
|
|
|
render: function() {
|
2016-11-09 13:00:25 +01:00
|
|
|
if (this.state.filesInfo === null || (this.props.show == 'downloaded' && this.state.publishedFileSdHashes === null)) {
|
2016-08-20 03:48:08 +02:00
|
|
|
return (
|
|
|
|
<main className="page">
|
2016-08-21 16:55:32 +02:00
|
|
|
<BusyMessage message="Loading" />
|
2016-08-20 03:48:08 +02:00
|
|
|
</main>
|
|
|
|
);
|
2016-11-09 12:58:58 +01:00
|
|
|
} else if (!this.state.filesInfo.length) {
|
|
|
|
return (
|
|
|
|
<main className="page">
|
|
|
|
{this.props.show == 'downloaded'
|
|
|
|
? <span>You haven't downloaded anything from LBRY yet. Go <Link href="/" label="search for your first download" />!</span>
|
|
|
|
: <span>You haven't published anything to LBRY yet.</span>}
|
|
|
|
</main>
|
|
|
|
);
|
2016-05-10 12:36:54 +02:00
|
|
|
} else {
|
2016-08-07 17:27:00 +02:00
|
|
|
var content = [],
|
2016-08-07 17:29:08 +02:00
|
|
|
seenUris = {};
|
|
|
|
|
2017-01-04 01:09:46 +01:00
|
|
|
const filesInfoSorted = this._sortFunctions[this.state.sortBy](this.state.filesInfo);
|
|
|
|
for (let fileInfo of filesInfoSorted) {
|
2017-01-06 12:41:31 +01:00
|
|
|
let {completed, lbry_uri, sd_hash, metadata, download_path, stopped, pending} = fileInfo;
|
2016-07-26 15:02:28 +02:00
|
|
|
|
2016-11-09 13:00:25 +01:00
|
|
|
if (!metadata || seenUris[lbry_uri]) {
|
2016-07-26 15:02:28 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-08-07 17:29:08 +02:00
|
|
|
seenUris[lbry_uri] = true;
|
|
|
|
|
2017-01-06 12:41:31 +01:00
|
|
|
content.push(<FileTile name={lbry_uri} sdHash={sd_hash} isMine={this.props.show == 'published'} local={true}
|
|
|
|
metadata={metadata} completed={completed} stopped={stopped} pending={pending} path={download_path}
|
|
|
|
{... this.state.filesAvailable !== null ? {available: this.state.filesAvailable[sd_hash]} : {}} />);
|
2016-05-10 12:36:54 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 10:19:41 +02:00
|
|
|
return (
|
2016-05-23 14:16:14 +02:00
|
|
|
<main className="page">
|
2016-11-18 11:56:55 +01:00
|
|
|
<span className='sort-section'>
|
|
|
|
Sort by { ' ' }
|
|
|
|
<FormField type="select" onChange={this.handleSortChanged}>
|
|
|
|
<option value="date">Date</option>
|
|
|
|
<option value="title">Title</option>
|
|
|
|
<option value="filename">File name</option>
|
|
|
|
</FormField>
|
|
|
|
</span>
|
2016-08-08 05:31:21 +02:00
|
|
|
{content}
|
2016-05-16 10:19:41 +02:00
|
|
|
</main>
|
|
|
|
);
|
2016-05-10 12:36:54 +02:00
|
|
|
}
|
2016-06-27 22:38:08 +02:00
|
|
|
});
|
2016-11-22 21:19:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
export default MyFilesPage;
|