import React from 'react'; import Page from './page'; import TorrentLine from './torrent' import {List, ListItem} from 'material-ui/List'; import Divider from 'material-ui/Divider'; import Subheader from 'material-ui/Subheader'; import RaisedButton from 'material-ui/RaisedButton'; import LinearProgress from 'material-ui/LinearProgress'; import {Tabs, Tab} from 'material-ui/Tabs'; import _ from 'lodash' export default class TopPage extends Page { constructor(props) { super(props) this.setTitle('Rats On The Boat - Torrents top'); this.topTorrents = {}; this.types = ['main', 'video', 'audio', 'books', 'pictures', 'application', 'archive'] this.descriptions = { main: __('All'), video: __('Video'), audio: __('Audio/Music'), books: __('Books'), pictures: __('Pictures/Images'), application: __('Apps/Games'), archive: __('Archives') } this.times = { overall: __('Overall'), hours: __('Last hour'), week: __('Last week'), month: __('Last month') } this.state = {type: 'main', time: 'overall'} } loadMoreTorrents(type, time) { time = time ? time : this.state.time window.torrentSocket.emit('topTorrents', type == 'main' ? null : type, {index: (this.topTorrents[type] && this.topTorrents[type][time] && this.topTorrents[type][time].length) || 0, time}, window.customLoader((data) => { if(!this.topTorrents[type]) this.topTorrents[type] = {} if(!this.topTorrents[type][time]) this.topTorrents[type][time] = [] if(data && data.length > 0) { this.topTorrents[type][time] = this.topTorrents[type][time].concat(data); this._update() } }) ) } _update() { if(this.timeForce) return this.timeForce = setTimeout(() => { delete this.timeForce this.forceUpdate() }, 550) } componentDidMount() { super.componentDidMount(); for(const type of this.types) { this.loadMoreTorrents(type) } this.remoteTopTorrents = ({torrents, type, time}) => { if(!torrents) return time = time ? time : 'overall' type = type ? type : 'main' this.topTorrents[type][time] = _.orderBy(_.unionBy(this.topTorrents[type][time], torrents, 'hash'), ['seeders'], ['desc']) this._update(); } window.torrentSocket.on('remoteTopTorrents', this.remoteTopTorrents); } componentWillUnmount() { if(this.remoteTopTorrents) window.torrentSocket.off('remoteTopTorrents', this.remoteTopTorrents); } render() { return (