import React, { Component } from 'react'; import formatBytes from './format-bytes' import {ListItem} from 'material-ui/List'; import Divider from 'material-ui/Divider'; import PagesPie from './pages-pie.js'; import TorrentPage from './torrent-page' import LinearProgress from 'material-ui/LinearProgress'; let rating = require('./rating'); const contentIcon = (type, category, fill = 'grey') => { if(category == 'xxx') { return ( ) } switch(type) { case 'video': return ( ) case 'audio': return ( ) case 'pictures': return ( ) case 'application': return ( ) case 'books': return ( ) case 'archive': return ( ) case 'disc': return ( ) default: return ( ) } }; export {contentIcon} export default class Torrent extends Component { state = { downloading: false, askDownloading: false, downloadProgress: {} } constructor(props) { super(props) if(props.download) { const { progress, downloaded, downloadSpeed } = props.download this.state.downloadProgress = { progress, downloaded, downloadSpeed } } } componentDidMount() { this.downloading = (hash) => { if(this.props.torrent.hash != hash) return; this.setState({downloading: true}) } window.torrentSocket.on('downloading', this.downloading); this.downloadDone = (hash, canceled) => { if(this.props.torrent.hash != hash) return; this.setState({ downloading: false, askDownloading: !canceled }) } window.torrentSocket.on('downloadDone', this.downloadDone); this.downloadProgress = (hash, progress) => { if(this.props.torrent.hash != hash) return; this.setState({ downloading: true, askDownloading: true, downloadProgress: progress }) } window.torrentSocket.on('downloadProgress', this.downloadProgress); } componentWillUnmount() { if(this.downloading) window.torrentSocket.off('downloading', this.downloading); if(this.downloadDone) window.torrentSocket.off('downloadDone', this.downloadDone); if(this.downloadProgress) window.torrentSocket.off('downloadProgress', this.downloadProgress); } render() { const torrent = this.props.torrent; if(!torrent) return null // can try draw null torrent (for example on downloading not started) let torrentRating = -1 if(torrent.good > 0 || torrent.bad > 0) torrentRating = Math.round(rating(torrent.good, torrent.bad) * 100); return (
{ const link = '/torrent/' + torrent.hash; if(e.button === 1) return false; /* if(e.ctrlKey && e.button === 0) { let win = window.open(link, '_blank'); //win.focus(); return true; } */ window.routerFix() PagesPie.instance().open(TorrentPage, {replace: 'all', hash: torrent.hash, peer: torrent.peer}) }} primaryText={ { if(node) node.onclick = () => { return false } }}> {torrent.name} } secondaryText={ { if(node) node.onclick = () => { return false } }}>
{ formatBytes(torrent.size, 1) + ' (' + torrent.files + ' files)' }
{ torrent.path && torrent.path.length > 0 ? torrent.path.map((path, index) => { return
{path}
}) : null } { torrent.seeders || torrent.leechers || torrent.completed ?
0 ? '#00C853' : 'grey')}}>{torrent.seeders} {__('seeders')} 0 ? '#AA00FF' : 'grey'), marginLeft: '12px'}}>{torrent.leechers} {__('leechers')} 0 ? '#FF6D00' : 'grey'), marginLeft: '12px'}}>{torrent.completed} {__('completed')}
: null } { (torrent.good > 0 || torrent.bad > 0) &&
= 50 ? '#00E676' : '#FF3D00'} style={{ height: '5px', }} />
= 50 ? '#00E676' : '#FF3D00', width: '190px'}}>{__('Torrent rating')}: {torrentRating}%
} { this.state.downloading &&
{__('downloading')}:
{this.state.downloadProgress && (this.state.downloadProgress.progress * 100).toFixed(1)}%
{this.state.downloadProgress && formatBytes(this.state.downloadProgress.downloadSpeed || 0, 0)}/s
}
} leftIcon={contentIcon(torrent.contentType, torrent.contentCategory, torrent.contentCategory != 'xxx' ? (torrent.peer ? '#6f5ee0' : 'grey') : (torrent.peer ? '#9083e2' : '#d3d3d3'))} rightIcon={
{ !this.state.askDownloading && !this.state.downloading ? { e.preventDefault(); e.stopPropagation(); this.setState({askDownloading: true}) window.torrentSocket.emit('download', torrent) }} viewBox="0 0 56 56"> : this.state.askDownloading && !this.state.downloading ?
: this.state.askDownloading && this.state.downloading && { e.preventDefault(); e.stopPropagation(); window.torrentSocket.emit('downloadCancel', torrent.hash) }} viewBox="0 0 18 18"> } { e.preventDefault(); e.stopPropagation(); var win = window.open(`magnet:?xt=urn:btih:${torrent.hash}`, '_self'); }} viewBox="0 0 24 24">
} />
) } }