module.exports = TorrentList var h = require('virtual-dom/h') var hyperx = require('hyperx') var hx = hyperx(h) var prettyBytes = require('pretty-bytes') function TorrentList (state, dispatch) { var torrents = state.view.client ? state.view.client.torrents : [] var list = torrents.map((torrent) => renderTorrent(state, dispatch, torrent)) return hx`${list}` } // Renders a torrent in the torrent list // Includes name, download status, play button, background image // May be expanded for additional info, including the list of files inside function renderTorrent (state, dispatch, torrent) { // Background image: show some nice visuals, like a frame from the movie, if possible var style = {} if (torrent.posterURL) { style['background-image'] = `linear-gradient(to bottom, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0) 100%), url("${torrent.posterURL}")` } // Foreground: name of the torrent, basic info like size, play button, // cast buttons if available, and delete var elements = [ renderTorrentMetadata(torrent), hx` dispatch('deleteTorrent', torrent)}> close `, hx` dispatch('openPlayer', torrent)}> play_arrow ` ] if (state.view.chromecast) { elements.push(hx` dispatch('openChromecast', torrent)}> cast `) } if (state.view.devices.airplay) { elements.push(hx` dispatch('openAirplay', torrent)}> airplay `) } return hx`${elements}` } // Renders the torrent name and download progress function renderTorrentMetadata (torrent) { return hx` ${torrent.name || 'Loading torrent...'} ${Math.floor(100 * torrent.progress)}% ${getFilesLength()} ${getPeers()} ${prettyBytes(torrent.downloadSpeed)}/s ${prettyBytes(torrent.uploadSpeed)}/s ` function getPeers () { var count = torrent.numPeers === 1 ? 'peer' : 'peers' return `${torrent.numPeers} ${count}` } function getFilesLength () { if (torrent.ready && torrent.files.length > 1) { return hx`${torrent.files.length} files` } } }