import React, { useState, useEffect, useCallback } from "react";
import PropTypes from "prop-types";
import { connect } 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 mapStateToProps = (state) => ({
searching: state.torrentStore.get("searching"),
results: state.torrentStore.get("searchResults"),
});
const mapDispatchToProps = { addTorrent, searchTorrents };
const TorrentSearch = ({
searching,
searchTorrents,
results,
addTorrent,
history,
match,
}) => {
const [search, setSearch] = useState(match.params.search || "");
const [type, setType] = useState(match.params.type || "");
const [url, setUrl] = useState("");
const getUrl = useCallback(() => {
return `/torrents/search/${type}/${encodeURI(search)}`;
}, [type, search]);
useEffect(() => {
if (search === "") {
return;
}
if (type === "") {
return;
}
const url = getUrl();
searchTorrents(url);
history.push(url);
}, [url, getUrl, searchTorrents, history, search, type]);
return (
);
};
TorrentSearch.propTypes = {
searching: PropTypes.bool.isRequired,
results: PropTypes.instanceOf(List),
searchFromURL: PropTypes.string,
match: PropTypes.object,
history: PropTypes.object,
addTorrent: PropTypes.func.isRequired,
searchTorrents: PropTypes.func.isRequired,
};
const SearchButton = (props) => {
const variant = props.type === props.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 connect(mapStateToProps, mapDispatchToProps)(TorrentSearch);