// @flow import React, { useState } from 'react'; import { shell } from 'electron'; import Icon from 'component/common/icon'; import * as ICONS from 'constants/icons'; type Props = { downloadList: any, stopDownload: (outpoint: string, sd_hash: string) => void, }; function DownloadProgress(props: Props) { const [isShow, setIsShow] = useState(true); const [cancelHash] = useState({}); const handleCancel = (hash, value) => { cancelHash[hash] = value; }; // return ( //
// // //
// ); // console.log('DownloadProgress', props.downloadList); if (!props.downloadList) return null; const downloadList = []; Object.keys(props.downloadList).map((key) => { const item = props.downloadList[key]; if (item.status === 'running') downloadList.push(item); }); if (downloadList.length === 0) return null; if (!isShow) { return ( ); } return (
{downloadList.map((item, index) => { let releaseTime = ''; if (item.metadata && item.metadata.release_time) { releaseTime = new Date(parseInt(item.metadata.release_time) * 1000).toISOString().split('T')[0]; } return ( <> {index !== 0 &&
} ); })}
); } type DownloadStateProps = { fileName: string, writtenBytes: number, totalBytes: number, addedOn: number, title: string, releaseTime: string, directory: string, outpoint: string, sd_hash: string, isCancel: boolean, stopDownload: (outpoint: string, sd_hash: string) => void, handleCancel: (hash: string, value: boolean) => void, }; function DownloadState({ fileName, writtenBytes, totalBytes, addedOn, title, releaseTime, directory, outpoint, sd_hash, isCancel, stopDownload, handleCancel, }: DownloadStateProps) { const processStopDownload = () => { handleCancel(sd_hash, false); stopDownload(outpoint, sd_hash); }; const percent = ((writtenBytes / totalBytes) * 100).toFixed(0); const sizeTypeText = ['Bytes', 'KB', 'MB', 'GB']; const getSizeType = (size) => Math.floor(Math.log(size) / Math.log(1024)); const convertSizeUnit = (size, sizeType) => { const unitSize = size / Math.pow(1024, sizeType); if (unitSize > 100) return unitSize.toFixed(0); if (unitSize > 10) return unitSize.toFixed(1); return unitSize.toFixed(2); }; const openDownloadFolder = () => { shell.openPath(directory); }; let text = ''; const downloadSpeed = Math.ceil(writtenBytes / (Date.now() / 1000 - addedOn)); const remainingSecond = Math.ceil((totalBytes - writtenBytes) / downloadSpeed); const remainingMinutes = Math.floor(remainingSecond / 60); if (remainingMinutes > 0) text += `${remainingMinutes} minutes `; text += `${remainingSecond - 60 * remainingMinutes} seconds remaining`; text += ' -- '; let sizeType = getSizeType(totalBytes); text += ` ${convertSizeUnit(writtenBytes, sizeType)} of ${convertSizeUnit(totalBytes, sizeType)} ${ sizeTypeText[sizeType] }`; sizeType = getSizeType(downloadSpeed); text += ` (${convertSizeUnit(downloadSpeed, sizeType)} ${sizeTypeText[sizeType]}/sec)`; return (

{title}

{ handleCancel(sd_hash, true); }} > ×
{fileName}

{releaseTime}

{text}

{isCancel && (

Do you cancel download this file?

)}
); } export default DownloadProgress;