import React, { useState, useEffect } from "react";
import PropTypes from "prop-types";
import { useDispatch, useSelector } from "react-redux";
import Loader from "../loader/loader";
import { OverlayTrigger, Tooltip } from "react-bootstrap";
import { prettySize } from "../../utils";
import {
addTorrent,
searchTorrents,
clearTorrentSearch,
} from "../../actions/torrents";
export const TorrentSearch = ({ history, match }) => {
const dispatch = useDispatch();
const [search, setSearch] = useState(match.params.search || "");
const [type, setType] = useState(match.params.type || "");
const searchFunc = (type = "") => {
if (search === "" || type === "") {
return;
}
const url = `/torrents/search/${type}/${encodeURI(search)}`;
setType(type);
dispatch(searchTorrents(url));
history.push(url);
};
useEffect(() => {
searchFunc(type);
return () => dispatch(clearTorrentSearch());
}, []); // eslint-disable-line react-hooks/exhaustive-deps
return (
);
};
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 = () => {
const searching = useSelector((state) => state.torrents.searching);
const results = useSelector((state) => state.torrents.searchResults);
if (searching) {
return ;
}
if (results.size === 0) {
return (
No results
);
}
return (
{results.map(function (el, index) {
return ;
})}
);
};
const Torrent = ({ torrent }) => {
const dispatch = useDispatch();
return (
{torrent.result.name}
{torrent.result.size !== 0 && (
{prettySize(torrent.result.size)}
)}
{torrent.quality}
{torrent.result.source}
{torrent.result.upload_user}
dispatch(addTorrent(torrent))}
>
);
};
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,
};