module.exports = TorrentList
var h = require('virtual-dom/h')
var hyperx = require('hyperx')
var hx = hyperx(h)
var prettyBytes = require('prettier-bytes')
var TorrentPlayer = require('../lib/torrent-player')
function TorrentList (state, dispatch) {
var torrentRows = state.saved.torrents.map(
(torrentSummary) => renderTorrent(torrentSummary))
return hx`
${torrentRows}
Drop a torrent file here or paste a magnet link
`
// 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 (torrentSummary) {
// Get ephemeral data (like progress %) directly from the WebTorrent handle
var infoHash = torrentSummary.infoHash
var torrent = state.client.torrents.find((x) => x.infoHash === infoHash)
var isSelected = state.selectedInfoHash === infoHash
// Background image: show some nice visuals, like a frame from the movie, if possible
var style = {}
if (torrentSummary.posterURL) {
var gradient = isSelected
? 'linear-gradient(to bottom, rgba(0, 0, 0, 0.8) 0%, rgba(0, 0, 0, 0.4) 100%)'
: 'linear-gradient(to bottom, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0) 100%)'
// Work around a Chrome bug (reproduced in vanilla Chrome, not just Electron):
// Backslashes in URLS in CSS cause bizarre string encoding issues
var cleanURL = torrentSummary.posterURL.replace(/\\/g, '/')
style.backgroundImage = gradient + `, url('${cleanURL}')`
}
// Foreground: name of the torrent, basic info like size, play button,
// cast buttons if available, and delete
var classes = ['torrent']
// playStatus turns the play button into a loading spinner or error icon
if (torrentSummary.playStatus) classes.push(torrentSummary.playStatus)
if (isSelected) classes.push('selected')
classes = classes.join(' ')
return hx`
dispatch('toggleSelectTorrent', infoHash)}>
${renderTorrentMetadata(torrent, torrentSummary)}
${renderTorrentButtons(torrentSummary)}
${isSelected ? renderTorrentDetails(torrent, torrentSummary) : ''}
`
}
// Show name, download status, % complete
function renderTorrentMetadata (torrent, torrentSummary) {
var name = torrentSummary.name || 'Loading torrent...'
var elements = [hx`
${name}
`]
// If a torrent is paused and we only get the torrentSummary
// If it's downloading/seeding then we have more information
if (torrent) {
var progress = Math.floor(100 * torrent.progress)
var downloaded = prettyBytes(torrent.downloaded)
var total = prettyBytes(torrent.length || 0)
if (downloaded !== total) downloaded += ` / ${total}`
elements.push(hx`
${getFilesLength()}
${getPeers()}
↓ ${prettyBytes(torrent.downloadSpeed || 0)}/s
↑ ${prettyBytes(torrent.uploadSpeed || 0)}/s
`)
elements.push(hx`
${progress}%
${downloaded}
`)
}
return hx`${elements}
`
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`
}
}
}
// Download button toggles between torrenting (DL/seed) and paused
// Play button starts streaming the torrent immediately, unpausing if needed
function renderTorrentButtons (torrentSummary) {
return hx`
handleButton('play', e)}>
${torrentSummary.playStatus === 'timeout' ? 'warning' : 'play_arrow'}
handleButton('toggleTorrent', e)}>
${torrentSummary.status === 'seeding' ? 'file_upload' : 'file_download'}
handleButton('deleteTorrent', e)}>
close
`
function handleButton (action, e) {
// Prevent propagation so that we don't select/unselect the torrent
e.stopPropagation()
dispatch(action, torrentSummary)
}
}
// Show files, per-file download status and play buttons, and so on
function renderTorrentDetails (torrent, torrentSummary) {
// If we're currently torrenting, show details directly from WebTorrent, including % downloaded
// Otherwise, show a summary of files in the torrent, if available
var files = (torrent && torrent.files) || torrentSummary.files
var filesElement
if (!files) {
// We don't know what files this torrent contains
var message = torrent
? 'Downloading torrent data using magnet link...'
: 'Failed to download torrent data from magnet link. Click the download button to try again...'
filesElement = hx`${message}
`
} else {
// We do know the files. List them and show download stats for each one
var fileRows = files.map((file, index) => renderFileRow(torrent, torrentSummary, file, index))
filesElement = hx`
`
}
return hx`
${filesElement}
`
function handleOpenFolder (e) {
e.stopPropagation()
dispatch('openFolder', torrentSummary)
}
}
// Show a single file in the details view for a single torrent
// File can either be a WebTorrent file (if we're currently torrenting it) or an element of torrentSummary.files
function renderFileRow (torrent, torrentSummary, file, index) {
// First, find out how much of the file we've downloaded
// (If we're not currently torrenting, just say whether we have the file or not)
if (file._startPiece) {
var numPieces = file._endPiece - file._startPiece + 1
var numPiecesPresent = 0
for (var piece = file._startPiece; piece <= file._endPiece; piece++) {
if (torrent.bitfield.get(piece)) numPiecesPresent++
}
var progress = Math.round(100 * numPiecesPresent / numPieces) + '%'
var isDone = numPieces === numPiecesPresent
} else {
var isDone = file.isDownloaded
var progress = hx`${isDone ? 'check' : 'close'}`
}
// Second, render the file as a table row
var icon
var iconClass = ''
if (state.playing.infoHash === torrentSummary.infoHash && state.playing.fileIndex === index) {
icon = 'pause_arrow' /* playing? add option to pause */
} else if (TorrentPlayer.isPlayable(file)) {
icon = 'play_arrow' /* playable? add option to play */
} else {
icon = 'description' /* file icon, opens in OS default app */
iconClass = isDone ? '' : 'disabled'
}
return hx`
|
${icon}
|
${file.name} |
${progress} |
${prettyBytes(file.length)} |
`
// Finally, let the user click on the row to play media or open files
function handleClick (e) {
e.stopPropagation()
if (icon === 'pause_arrow') {
throw new Error('Unimplemented') // TODO: pause audio
} else if (icon === 'play_arrow') {
dispatch('play', torrentSummary, index)
} else if (isDone) {
dispatch('openFile', torrentSummary, index)
}
}
}
}