import React, { Component } from 'react'; import formatBytes from './format-bytes' import {ListItem} from 'material-ui/List'; import Divider from 'material-ui/Divider'; import ToolTip from './tooltip'; 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, downloaded: false, startingDownloading: false, downloadProgress: {}, downloadRemoveOnDone: false, downloadPaused: 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 } } componentDidMount() { this.downloading = (hash) => { if(this.props.torrent.hash != hash) return; this.setState({ downloading: true, startingDownloading: false }) } window.torrentSocket.on('downloading', this.downloading); this.downloadDone = (hash, canceled) => { if(this.props.torrent.hash != hash) return; this.setState({ downloading: false, downloaded: !canceled, startingDownloading: false }) } window.torrentSocket.on('downloadDone', this.downloadDone); this.downloadProgress = (hash, progress) => { if(this.props.torrent.hash != hash) return; this.setState({ downloading: true, startingDownloading: false, downloadProgress: progress }) } window.torrentSocket.on('downloadProgress', this.downloadProgress); this.downloadUpdate = (hash, options) => { if(this.props.torrent.hash != hash) return; this.setState({ downloadRemoveOnDone: options.removeOnDone, downloadPaused: options.paused }) } window.torrentSocket.on('downloadUpdate', this.downloadUpdate); } 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); if(this.downloadUpdate) window.torrentSocket.off('downloadUpdate', this.downloadUpdate); } 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); const canDeleteDownloadAfterFinish = (this.state.downloading || this.state.startingDownloading) && !this.state.downloaded const canPause = (this.state.downloading || this.state.startingDownloading) return (