import React, { useState } from "react"; import { useMutation, useQuery } from "@tanstack/react-query"; import axios from "axios"; import { Form, Field } from "react-final-form"; import { PROWLARR_SERVICE_BASE_URI, QBITTORRENT_SERVICE_BASE_URI, } from "../../constants/endpoints"; import { isEmpty, isNil } from "lodash"; import ellipsize from "ellipsize"; import prettyBytes from "pretty-bytes"; interface TorrentSearchPanelProps { issueName: string; comicObjectId: string; } interface SearchFormValues { issueName: string; } interface TorrentResult { fileName: string; seeders: number; leechers: number; size: number; files: number; indexer: string; downloadUrl: string; } interface TorrentDownloadPayload { comicObjectId: string; torrentToDownload: string; } export const TorrentSearchPanel = (props: TorrentSearchPanelProps) => { const { issueName, comicObjectId } = props; // Initialize searchTerm with issueName from props const [searchTerm, setSearchTerm] = useState({ issueName }); const [torrentToDownload, setTorrentToDownload] = useState(""); const { data, isSuccess, isLoading } = useQuery({ queryKey: ["searchResults", searchTerm.issueName], queryFn: async () => { return await axios({ url: `${PROWLARR_SERVICE_BASE_URI}/search`, method: "POST", data: { prowlarrQuery: { port: "9696", apiKey: "38c2656e8f5d4790962037b8c4416a8f", offset: 0, categories: [7030], query: searchTerm.issueName, host: "localhost", limit: 100, type: "search", indexerIds: [2], }, }, }); }, enabled: !isNil(searchTerm.issueName) && searchTerm.issueName.trim() !== "", // Make sure searchTerm is not empty }); const mutation = useMutation({ mutationFn: async (newTorrent: TorrentDownloadPayload) => axios.post(`${QBITTORRENT_SERVICE_BASE_URI}/addTorrent`, newTorrent), onSuccess: async () => { // Torrent added successfully }, }); const searchIndexer = (values: SearchFormValues) => { setSearchTerm({ issueName: values.issueName }); // Update searchTerm based on the form submission }; const downloadTorrent = (downloadUrl: string) => { const newTorrent: TorrentDownloadPayload = { comicObjectId, torrentToDownload: downloadUrl, }; mutation.mutate(newTorrent); }; return ( <>
| Name | Indexer | Action |
|---|---|---|
|
{ellipsize(result.fileName, 90)} {/* Seeders/Leechers */}
{result.seeders} seeders
{result.leechers} leechers
{/* Size */}
{prettyBytes(result.size)}
{/* Files */}
{result.files} files
|
{result.indexer} |