import React from "react" import PropTypes from "prop-types" import { List } from "immutable" import Dropdown from "react-bootstrap/Dropdown" function buildMenuItems(torrents) { if (!torrents || torrents.size === 0) { 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; } const TorrentsButton = (props) => { const entries = buildMenuItems(props.torrents); const searchUrl = `#/torrents/search/movies/${encodeURI(props.movieTitle)}`; return ( Torrents Advanced Search {entries.length > 0 && } {entries.map((e, index) => { switch (e.type) { case "header": return ( {e.value} ); case "divider": return ( ); case "entry": return ( props.addTorrent(e.url)}> {e.quality} ); } })} ); } TorrentsButton.propTypes = { torrents: PropTypes.instanceOf(List), addTorrent: PropTypes.func.isRequired, movieTitle: PropTypes.string.isRequired, } export default TorrentsButton;