import React, { Component } from 'react'; import { connect } from 'react-redux'; import selectTorrent, { UNION, SUBTRACT, EXCLUSIVE } from '../actions/selection'; import { formatBitrate } from '../bitrate'; import Ratio from './ratio'; import TorrentProgress from './torrent_progress'; const name_style = { maxWidth: `${window.innerWidth * 0.25}px`, textOverflow: 'ellipsis', overflowX: 'hidden', whiteSpace: 'nowrap' }; class _Torrent extends Component { shouldComponentUpdate(nextProps, _) { const { selection, torrent } = this.props; const nt = nextProps.torrent; const active = selection.indexOf(torrent.id); const nActive = nextProps.selection.indexOf(torrent.id); return active !== nActive || torrent.id !== nt.id || torrent.status !== nt.status || torrent.rate_down !== nt.rate_down || torrent.rate_up !== nt.rate_up; } render() { const { dispatch, selection, torrent } = this.props; const t = torrent; return ( dispatch(selectTorrent([t.id], e.target.checked ? UNION : SUBTRACT)) } /> { e.preventDefault(); dispatch(selectTorrent([t.id], EXCLUSIVE)); }} >{t.name} {formatBitrate(t.rate_up)} {formatBitrate(t.rate_down)} ); } } const Torrent = connect((state, props) => { return { torrent: state.torrents[props.id], selection: state.selection, }; })(_Torrent); class TorrentTable extends Component { render() { const { selection, torrents, dispatch } = this.props; return ( {Object.values(torrents).slice().sort((a, b) => a.name.localeCompare(b.name)).map(t => )}
{ if (selection.length > 0) { dispatch(selectTorrent([], EXCLUSIVE)); } else { dispatch(selectTorrent(Object.keys(torrents), EXCLUSIVE)); } }} /> name up down ratio progress
); } } export default connect(state => ({ torrents: state.torrents, selection: state.selection, router: state.router }))(TorrentTable);