import React, { useState } from "react"; import PropTypes from "prop-types"; import { List } from "immutable"; import { connect } from "react-redux"; import { prettySize } from "../../utils"; import { addTorrent } from "../../actions/torrents"; 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"), size: torrent.get("size"), }); } // Push the divider if (dividerCount > 0) { dividerCount--; entries.push({ type: "divider" }); } } return entries; } const torrentsButton = ({ torrents, search, searching, addTorrent, url }) => { /* eslint-disable */ const [show, setShow] = useState(false); /* eslint-enable */ const entries = buildMenuItems(torrents); const count = torrents && torrents.size !== 0 ? torrents.size : 0; const onSelect = (eventKey) => { // Close the dropdown if the eventkey is not specified if (eventKey === null) { setShow(false); } }; const onToggle = (isOpen, event, metadata) => { // Don't close on select if (metadata && metadata.source !== "select") { setShow(isOpen); } }; return ( Torrents {count} Automatic search Manual search {entries.length > 0 && } {entries.map((e, index) => { switch (e.type) { case "header": return ( {e.value} ); case "divider": return ; case "entry": return ( addTorrent(e.url)}> {e.quality} {e.size !== 0 && ( ({prettySize(e.size)}) )} ); } })} ); }; torrentsButton.propTypes = { torrents: PropTypes.instanceOf(List), searching: PropTypes.bool, search: PropTypes.func.isRequired, addTorrent: PropTypes.func.isRequired, url: PropTypes.string, }; torrentsButton.defaultProps = { torrents: List(), }; export const TorrentsButton = connect(null, { addTorrent })(torrentsButton);