fix(download): fix download status in recent torrents

This commit is contained in:
Alexey Kasyanchuk
2021-02-17 03:50:50 +03:00
parent ac7e3cfe9f
commit 8b1fc513ad
3 changed files with 67 additions and 22 deletions

View File

@ -6,6 +6,8 @@ import Search from './search'
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
import _ from 'lodash'
export default class SearchPage extends Page {
constructor(props) {
super(props)
@ -14,10 +16,32 @@ export default class SearchPage extends Page {
componentDidMount()
{
Search.instance().onSearchUpdate = () => this.forceUpdate()
window.torrentSocket.emit('downloads', (downloads) => {
for(const torrent of downloads) {
const hash = torrent.torrentObject.hash;
delete torrent.torrentObject;
let needUpdate = false;
const updateValues = (target) => {
let index = _.findIndex(target, {hash});
if(index >= 0) {
target[index].download = torrent
needUpdate = true;
}
}
updateValues(Search.instance().searchTorrents);
updateValues(Search.instance().searchFiles);
if (needUpdate)
this.forceUpdate()
}
})
}
componentWillUnmount()
{
Search.instance().onSearchUpdate = () => {}
for(const torrent of Search.instance().searchTorrents)
delete torrent.download
for(const torrent of Search.instance().searchFiles)
delete torrent.download
}
render() {
const orderText = (text, field) => {

View File

@ -169,26 +169,43 @@ export default class Torrent extends Component {
downloadRemoveOnDone: false,
downloadPaused: false,
}
downloadInited = false;
constructor(props)
{
super(props)
const download = props.download || props.torrent.download
if(download)
{
const { progress, downloaded, downloadSpeed, removeOnDone, paused } = download
this.state.downloadProgress = {
progress, downloaded, downloadSpeed
}
this.state.downloading = progress < 1
this.state.downloaded = progress === 1
this.state.downloadRemoveOnDone = removeOnDone
this.state.downloadPaused = paused
}
super(props)
const download = props.download || props.torrent.download;
this.updateDownload(download);
this.downloadPathInput = React.createRef();
}
updateDownload(download)
{
if(download)
{
const { progress, downloaded, downloadSpeed, removeOnDone, paused } = download
this.state.downloadProgress = {
progress, downloaded, downloadSpeed
}
this.state.downloading = progress < 1
this.state.downloaded = progress === 1
this.state.downloadRemoveOnDone = removeOnDone
this.state.downloadPaused = paused
this.downloadInited = true;
}
}
componentDidUpdate(prevProps)
{
const download = this.props.download || this.props.torrent.download;
if(prevProps && !prevProps.download && download && !this.downloadInited) {
this.updateDownload(download);
this.forceUpdate();
}
}
componentDidMount()
{
scrollBack()