import { Loader, Table, Text, Tooltip, Title, Group, Progress, Center } from '@mantine/core'; import { Download } from 'tabler-icons-react'; import { useEffect, useState } from 'react'; import axios from 'axios'; import { NormalizedTorrent } from '@ctrl/shared-torrent'; import { IModule } from '../modules'; import { useConfig } from '../../../tools/state'; import { AddItemShelfButton } from '../../AppShelf/AddAppShelfItem'; export const DownloadsModule: IModule = { title: 'Torrent', description: 'Show the current download speed of supported services', icon: Download, component: DownloadComponent, options: { hidecomplete: { name: 'Hide completed torrents', value: false, }, }, }; export default function DownloadComponent() { const { config } = useConfig(); const qBittorrentService = config.services .filter((service) => service.type === 'qBittorrent') .at(0); const delugeService = config.services.filter((service) => service.type === 'Deluge').at(0); const hideComplete: boolean = (config?.modules?.[DownloadsModule.title]?.options?.hidecomplete?.value as boolean) ?? false; const [delugeTorrents, setDelugeTorrents] = useState([]); const [qBittorrentTorrents, setqBittorrentTorrents] = useState([]); useEffect(() => { if (qBittorrentService) { setInterval(() => { axios .post('/api/modules/downloads?dlclient=qbit', { ...qBittorrentService }) .then((res) => { setqBittorrentTorrents(res.data.torrents); }); }, 3000); } if (delugeService) { setInterval(() => { axios.post('/api/modules/downloads?dlclient=deluge', { ...delugeService }).then((res) => { setDelugeTorrents(res.data.torrents); }); }, 3000); } }, [config.modules]); if (!qBittorrentService && !delugeService) { return ( Critical: No qBittorrent/Deluge instance found in services. Add a qBittorrent/Deluge service to view current downloads ); } if (qBittorrentTorrents.length === 0 && delugeTorrents.length === 0) { return (
); } const ths = ( Name Download Upload Progress ); // Loop over qBittorrent torrents merging with deluge torrents const torrents: NormalizedTorrent[] = []; delugeTorrents.forEach((delugeTorrent) => torrents.push({ ...delugeTorrent, progress: delugeTorrent.progress / 100 }) ); qBittorrentTorrents.forEach((torrent) => torrents.push(torrent)); const rows = torrents.map((torrent) => { if (torrent.progress === 1 && hideComplete) { return null; } const downloadSpeed = torrent.downloadSpeed / 1024 / 1024; const uploadSpeed = torrent.uploadSpeed / 1024 / 1024; return ( {torrent.name} {downloadSpeed > 0 ? `${downloadSpeed.toFixed(1)} Mb/s` : '-'} {uploadSpeed > 0 ? `${uploadSpeed.toFixed(1)} Mb/s` : '-'} {(torrent.progress * 100).toFixed(1)}% ); }); return ( Your torrents {ths}{rows}
); }