import React, { useState, useEffect, useCallback } from "react"; import PropTypes from "prop-types"; import { useDispatch, useSelector } from "react-redux"; import { addTorrent, searchTorrents } from "../../actions/torrents"; import { Map, List } from "immutable"; import Loader from "../loader/loader"; import { OverlayTrigger, Tooltip } from "react-bootstrap"; import { prettySize } from "../../utils"; const TorrentSearch = ({ history, match }) => { const dispatch = useDispatch(); const searching = useSelector((state) => state.torrentStore.get("searching")); const results = useSelector((state) => state.torrentStore.get("searchResults") ); const [search, setSearch] = useState(match.params.search || ""); const [type, setType] = useState(match.params.type || ""); const url = useCallback(() => { if (search === "" || type === "") { return ""; } return `/torrents/search/${type}/${encodeURI(search)}`; }, [type, search]); const searchFunc = useCallback(() => { const searchURL = url(); if (searchURL === "") { return; } dispatch(searchTorrents(searchURL)); history.push(searchURL); }, [dispatch, history, url]); useEffect(() => { searchFunc(); }, [searchFunc]); return (
setSearch(e.target.value)} />
{ setType("movies"); searchFunc(); }} /> { setType("shows"); searchFunc(); }} />
dispatch(addTorrent(url))} searchFromURL={search} />
); }; TorrentSearch.propTypes = { searchFromURL: PropTypes.string, match: PropTypes.object, history: PropTypes.object, }; const SearchButton = ({ type, typeFromURL, text, handleClick }) => { const variant = type === typeFromURL ? "primary" : "secondary"; return ( ); }; SearchButton.propTypes = { type: PropTypes.string, typeFromURL: PropTypes.string, text: PropTypes.string, handleClick: PropTypes.func.isRequired, }; const TorrentList = (props) => { if (props.searching) { return ; } if (props.searchFromURL === "") { return null; } if (props.results.size === 0) { return (

No results

); } return ( {props.results.map(function (el, index) { return ; })} ); }; TorrentList.propTypes = { searching: PropTypes.bool.isRequired, results: PropTypes.instanceOf(List), searchFromURL: PropTypes.string, addTorrent: PropTypes.func.isRequired, }; const Torrent = (props) => (
{props.data.get("name")}
{props.data.get("size") !== 0 && ( {prettySize(props.data.get("size"))} )} {props.data.get("quality")} {props.data.get("source")} {props.data.get("upload_user")}
props.addTorrent(props.data.get("url"))} >
); Torrent.propTypes = { data: PropTypes.instanceOf(Map), addTorrent: PropTypes.func.isRequired, }; const TorrentHealth = (props) => { const seeders = props.seeders || 0; const leechers = props.leechers || 1; let color; let health; let ratio = seeders / leechers; if (seeders > 20) { health = "good"; color = "success"; } else { if (ratio > 1) { health = "medium"; color = "warning"; } else { health = "bad"; color = "danger"; } } const className = `align-self-start text text-center text-${color}`; const tooltip = (

Health: {health}

Seeders: {seeders}

Leechers: {props.leechers}

); return ( ); }; TorrentHealth.propTypes = { url: PropTypes.string, seeders: PropTypes.number, leechers: PropTypes.number, }; export default TorrentSearch;