86 lines
2.2 KiB
JavaScript
86 lines
2.2 KiB
JavaScript
import React from "react"
|
|
|
|
import { DropdownButton, MenuItem } from "react-bootstrap"
|
|
|
|
export default class TorrentsButton extends React.PureComponent {
|
|
constructor(props) {
|
|
super(props);
|
|
this.handleClick = this.handleClick.bind(this);
|
|
}
|
|
handleClick(e, url) {
|
|
e.preventDefault();
|
|
this.props.addTorrent(url);
|
|
}
|
|
render() {
|
|
const entries = buildMenuItems(this.props.torrents);
|
|
const searchUrl = `#/torrents/search/movies/${encodeURI(this.props.movieTitle)}`;
|
|
return (
|
|
<DropdownButton className="btn btn-default btn-sm" title="Torrents" id="download-torrents-button" dropup>
|
|
<MenuItem className="text-warning" header>Advanced</MenuItem>
|
|
<MenuItem href={searchUrl} >
|
|
<i className="fa fa-search" aria-hidden="true"></i> Search
|
|
</MenuItem>
|
|
{entries.length > 0 &&
|
|
<MenuItem divider></MenuItem>
|
|
}
|
|
{entries.map(function(e, index) {
|
|
switch (e.type) {
|
|
case "header":
|
|
return (
|
|
<MenuItem key={index} className="text-warning" header>
|
|
{e.value}
|
|
</MenuItem>
|
|
);
|
|
case "divider":
|
|
return (
|
|
<MenuItem key={index} divider></MenuItem>
|
|
);
|
|
case "entry":
|
|
return (
|
|
<MenuItem key={index} href={e.url} onClick={(event) => this.handleClick(event, e.url)}>
|
|
{e.quality}
|
|
</MenuItem>
|
|
);
|
|
}
|
|
}, this)}
|
|
</DropdownButton>
|
|
);
|
|
}
|
|
}
|
|
|
|
function buildMenuItems(torrents) {
|
|
if (!torrents) {
|
|
return [];
|
|
}
|
|
|
|
const t = torrents.groupBy((el) => el.get("source"));
|
|
|
|
// Build the array of entries
|
|
let entries = [];
|
|
let dividerCount = t.size - 1;
|
|
for (let [source, torrentList] of t.entrySeq()) {
|
|
// Push the title
|
|
entries.push({
|
|
type: "header",
|
|
value: source,
|
|
});
|
|
|
|
// Push the torrents
|
|
for (let torrent of torrentList) {
|
|
entries.push({
|
|
type: "entry",
|
|
quality: torrent.get("quality"),
|
|
url: torrent.get("url"),
|
|
});
|
|
}
|
|
|
|
// Push the divider
|
|
if (dividerCount > 0) {
|
|
dividerCount--;
|
|
entries.push({ type: "divider" });
|
|
}
|
|
}
|
|
|
|
return entries;
|
|
}
|