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 Loader from "../loader/loader"; import { OverlayTrigger, Tooltip } from "react-bootstrap"; import { prettySize } from "../../utils"; export const TorrentSearch = ({ history, match }) => { const dispatch = useDispatch(); 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(); }} />
); }; TorrentSearch.propTypes = { search: 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 = ({ search }) => { const searching = useSelector((state) => state.torrents.searching); const results = useSelector((state) => state.torrents.searchResults); if (searching) { return ; } if (search === "") { return null; } if (results.size === 0) { return (

No results

); } return ( {results.map(function (el, index) { return ; })} ); }; TorrentList.propTypes = { search: PropTypes.string, }; const Torrent = ({ torrent }) => { const dispatch = useDispatch(); return (
{torrent.name}
{torrent.size !== 0 && ( {prettySize(torrent.size)} )} {torrent.quality} {torrent.source} {torrent.upload_user}
dispatch(addTorrent({ url: torrent.url, metadata: null })) } >
); }; Torrent.propTypes = { torrent: PropTypes.object.isRequired, }; const TorrentHealth = ({ url, seeders = 0, 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: {leechers}

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