import { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import { api, type CurrentUser, type DownloadClient, type DownloadItem, type DownloadState } from "../api/client"; import ReadNowButton from "../components/ReadNowButton"; import EmptyState from "../components/EmptyState"; import TimeAgo from "../components/TimeAgo"; import { BlockSkeleton } from "../components/Skeleton"; import { ErrorNotice } from "../components/Notice"; import { formatBytes } from "./settings/StorageSettings"; import { readableError } from "../utils/errors"; import PageIntro from "../components/PageIntro"; const KIND_NAMES: Record = { qbittorrent: "qBittorrent", sabnzbd: "SABnzbd" }; const SHOWN = 25; const STATE_BADGE: Record = { downloading: "bg-blue-lt text-blue", queued: "bg-secondary-lt text-secondary", waiting: "bg-secondary-lt text-secondary", stalled: "bg-yellow-lt text-yellow", paused: "bg-secondary-lt text-secondary", processing: "bg-azure-lt text-azure", error: "bg-red-lt text-red", seeding: "bg-green-lt text-green", completed: "bg-green-lt text-green", failed: "bg-red-lt text-red", other: "bg-secondary-lt text-secondary", }; const speedText = (n: number | null): string => (n === null ? "—" : `${formatBytes(n)}/s`); /** "2 h 5 min", "14 min", "40 s". */ function durationText(seconds: number): string { if (seconds >= 86400) return `${Math.floor(seconds / 86400)} d ${Math.floor((seconds % 86400) / 3600)} h`; if (seconds >= 3600) return `${Math.floor(seconds / 3600)} h ${Math.floor((seconds % 3600) / 60)} min`; if (seconds >= 60) return `${Math.round(seconds / 60)} min`; return `${Math.max(1, Math.round(seconds))} s`; } function Badge({ c }: { c: DownloadClient }) { if (!c.enabled) return Off; if (!c.state) return Not read yet; if (!c.fresh) return Can’t be read; if (c.problems.some((x) => x.severity === "critical")) return Needs a look; if (c.problems.length > 0) return Needs a look; return In order; } function Tile({ label, value, sub, warn }: { label: string; value: string; sub?: string | null; warn?: boolean }) { return (
{label}
{value}
{sub &&
{sub}
}
); } function ItemsTable({ items, stuck }: { items: DownloadItem[]; stuck: Set }) { return ( <>
{items.slice(0, SHOWN).map((i) => { const pct = i.progress === null ? null : Math.round(i.progress * 100); const isStuck = stuck.has(i.id); return ( ); })}
Name State Progress Speed Left Category
{i.name} {isStuck ? stuck : {i.state}} {i.rawState.toLowerCase() !== i.state &&
{i.rawState}
} {isStuck && i.state === "stalled" && i.activityAt && (
nothing moved
)}
{pct === null ? ( — ) : ( <>
{pct}%{i.size ? ` of ${formatBytes(i.size)}` : ""}
)}
{i.speed ? speedText(i.speed) : "—"} {i.left !== null && i.left > 0 ? formatBytes(i.left) : "—"} {i.eta !== null &&
{durationText(i.eta)}
}
{i.category ?? "—"}
{items.length > SHOWN &&
{items.length - SHOWN} more are listed by the client (the ones needing a look first).
} ); } function Card({ c, canRead, onRead }: { c: DownloadClient; canRead: boolean; onRead: () => void }) { const s = c.state; const stuck = new Set(s?.stuck ?? []); const failedRecent = new Set(s?.failedRecent ?? []); const failed = (s?.failed ?? []).filter((f) => failedRecent.has(f.id)); const pending = s ? s.total - s.counts.seeding - s.counts.completed - s.counts.failed : 0; const fullest = s?.disks && s.disks.length > 0 ? s.disks.reduce((a, b) => (a.free < b.free ? a : b)) : null; return (

{c.name} {KIND_NAMES[c.kind]}{s?.version ? ` ${s.version}` : ""}

{c.status ? <>read : "not read yet"} {canRead && c.enabled && }
{c.status && !c.status.ok &&
The last attempt to read it failed: {c.status.error ?? "no answer"}.
} {c.problems.length > 0 && (
    {c.problems.map((x) => (
  • {x.message}
  • ))}
)} {s && ( <>
0 ? `${s.counts.stalled} stalled` : null, s.counts.error > 0 ? `${s.counts.error} in error` : null, c.kind === "qbittorrent" ? `${s.counts.seeding} seeding` : null].filter(Boolean).join(", ")} /> {c.kind === "qbittorrent" ? ( 0 ? "in error, or nothing moving for a day" : "nothing stuck"} warn={stuck.size > 0} /> ) : ( 0} /> )} {c.kind === "qbittorrent" ? ( ) : ( 0} /> )} {fullest && }
{s.disks && s.disks.some((d) => d.total) && (

Disks

{s.disks .filter((d) => d.total) .map((d) => { const free = d.total ? d.free / d.total : 1; return (
{d.label}: {formatBytes(d.free)} free of {formatBytes(d.total!)} ({Math.floor(free * 100)}%)
); })}
)} {s.items.length > 0 && ( <>

{c.kind === "sabnzbd" ? "Queue" : "Not finished"}

{s.counts.seeding + s.counts.completed > 0 &&
{s.counts.seeding + s.counts.completed} more are finished and idle (seeding or stopped), and not listed.
} )} {s.items.length === 0 &&
{s.total === 0 ? "Nothing in the queue." : `${s.total} item${s.total === 1 ? "" : "s"}, all finished and idle.`}
} {failed.length > 0 && ( <>

Failed in the last day

    {failed.map((f) => (
  • {f.name} {f.message &&
    {f.message}
    }
  • ))}
)} {s.warnings && s.warnings.count > 0 && (

{KIND_NAMES[c.kind]}’s own warnings ({s.warnings.count} in its list — cleared in its own Warnings page)

    {s.warnings.recent.map((w, i) => (
  • {w}
  • ))}
)} )}
); } /** qBittorrent and SABnzbd: how fast they are going, what is in their queues and in what state, what is stuck or failed, and the room they have. */ export default function Downloads({ user }: { user: CurrentUser }) { const [data, setData] = useState(null); const [error, setError] = useState(null); const load = () => api.downloads .list() .then((r) => { setData(r.clients); setError(null); }) .catch((err) => setError(readableError(err))); useEffect(() => { void load(); const t = setInterval(load, 60_000); return () => clearInterval(t); }, []); const canRead = user.role === "admin" || user.role === "operator"; return ( <>

Download clients

qBittorrent and SABnzbd, read every few minutes (see Integrations) — nothing is added, paused or removed. Downloads that are stuck, no connection, a queue left paused or getting nothing and{" "} a disk nearly full are alerts — see Alerts. What Sonarr, Radarr and the others think of their downloads is on Media managers. {!data && !error && } {data && data.length === 0 && (
)} {data?.map((c) => void load()} />)} ); }