import React, { useEffect, useContext, ReactElement } from "react"; import { getDownloadProgress, getBundlesForComic, } from "../../actions/airdcpp.actions"; import { useDispatch, useSelector } from "react-redux"; import { RootState } from "threetwo-ui-typings"; import { isEmpty, isNil, map } from "lodash"; import prettyBytes from "pretty-bytes"; import dayjs from "dayjs"; import ellipsize from "ellipsize"; import { AirDCPPSocketContext } from "../../context/AirDCPPSocket"; interface IDownloadsPanelProps { data: any; comicObjectId: string; } export const DownloadsPanel = ( props: IDownloadsPanelProps, ): ReactElement | null => { const downloadProgressTick = useSelector( (state: RootState) => state.airdcpp.downloadProgressData, ); const bundles = useSelector((state: RootState) => { return state.airdcpp.bundles; }); // AirDCPP Socket initialization const userSettings = useSelector((state: RootState) => state.settings.data); const { ADCPPSocket } = useContext(AirDCPPSocketContext); const dispatch = useDispatch(); // Fetch the downloaded files and currently-downloading file(s) from AirDC++ useEffect(() => { try { if (!isEmpty(userSettings)) { dispatch( getBundlesForComic(props.comicObjectId, ADCPPSocket, { username: `${userSettings.directConnect.client.host.username}`, password: `${userSettings.directConnect.client.host.password}`, }), ); dispatch( getDownloadProgress(props.comicObjectId, ADCPPSocket, { username: `${userSettings.directConnect.client.host.username}`, password: `${userSettings.directConnect.client.host.password}`, }), ); } } catch (error) { throw new Error(error); } }, [dispatch]); const ProgressTick = (props) => { return (
{JSON.stringify(props.data.downloadProgressTick)}
{props.data.name}
{prettyBytes(props.data.downloaded_bytes)} of{" "} {prettyBytes(props.data.size)}{" "} {(parseInt(props.data.downloaded_bytes) / parseInt(props.data.size)) * 100} %
{prettyBytes(props.data.speed)} per second.
Time left: {Math.round(parseInt(props.data.seconds_left) / 60)}
{props.data.target}
); }; const Bundles = (props) => { return !isEmpty(props.data) ? (
{map(props.data, (bundle) => ( ))}
Filename Size Download Time
{ellipsize(bundle.name, 58)}
{bundle.target}
{prettyBytes(bundle.size)} {dayjs .unix(bundle.time_finished) .format("h:mm on ddd, D MMM, YYYY")}
) : (
{"No Downloads Found"}
); }; return !isNil(props.data) ? ( <>
{!isNil(downloadProgressTick) ? ( ) : null} {!isEmpty(ADCPPSocket) ? ( ) : (
AirDC++ is not configured. Please configure it in{" "} Settings.
)}
) : null; }; export default DownloadsPanel;