"use client";
import { useState, useEffect, createContext, useContext, type ReactNode } from "react";
import { Icon } from "./ui";
import { useTranslation } from "@/lib/i18n/context";
const QbConfigContext = createContext(false);
export function QbittorrentProvider({ children }: { children: ReactNode }) {
const [configured, setConfigured] = useState(false);
useEffect(() => {
fetch("/api/settings/qbittorrent")
.then((r) => (r.ok ? r.json() : null))
.then((data) => {
setConfigured(!!(data && data.url && data.url.trim() && data.username && data.username.trim()));
})
.catch(() => setConfigured(false));
}, []);
return {children};
}
export function QbittorrentDownloadButton({ downloadUrl, releaseId }: { downloadUrl: string; releaseId: string }) {
const { t } = useTranslation();
const configured = useContext(QbConfigContext);
const [sending, setSending] = useState(false);
const [sent, setSent] = useState(false);
const [error, setError] = useState(null);
if (!configured) return null;
async function handleSend() {
setSending(true);
setError(null);
try {
const resp = await fetch("/api/qbittorrent/add", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url: downloadUrl }),
});
const data = await resp.json();
if (data.error) {
setError(data.error);
} else if (data.success) {
setSent(true);
} else {
setError(data.message || t("prowlarr.sentError"));
}
} catch {
setError(t("prowlarr.sentError"));
} finally {
setSending(false);
}
}
return (
);
}