import { Table, Text, Tooltip, Title, Group, Progress, Skeleton, ScrollArea, Center, Image, } from '@mantine/core'; import { IconDownload as Download } from '@tabler/icons'; 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'; import { useSetSafeInterval } from '../../../tools/hooks/useSetSafeInterval'; 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 downloadServices = config.services.filter( (service) => service.type === 'qBittorrent' || service.type === 'Transmission' || service.type === 'Deluge' ) ?? []; const hideComplete: boolean = (config?.modules?.[DownloadsModule.title]?.options?.hidecomplete?.value as boolean) ?? false; const [torrents, setTorrents] = useState([]); const setSafeInterval = useSetSafeInterval(); const [isLoading, setIsLoading] = useState(true); useEffect(() => { setIsLoading(true); if (downloadServices.length === 0) return; setSafeInterval(() => { // Send one request with each download service inside axios.post('/api/modules/downloads', { config }).then((response) => { setTorrents(response.data); setIsLoading(false); }); }, 1000); }, [config.services]); if (downloadServices.length === 0) { return ( No supported download clients found! Add a download service to view your current downloads... ); } if (isLoading) { return ( <> ); } const ths = ( Name Download Upload Progress ); // Loop over qBittorrent torrents merging with deluge torrents const rows = torrents.map((torrent) => { if (torrent.progress === 1 && hideComplete) { return []; } 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)}% ); }); const easteregg = (
); return ( Your torrents {rows.length > 0 ? ( {ths}{rows}
) : ( easteregg )}
); }