import { Fragment } from 'react' import { DownloadItem } from '@janhq/core' import { Progress, Modal, Button } from '@janhq/joi' import { useAtomValue } from 'jotai' import useAbortDownload from '@/hooks/useAbortDownload' import { downloadStateListAtom } from '@/hooks/useDownloadState' import { formatDownloadPercentage } from '@/utils/converter' const DownloadStatus: React.FC = () => { const downloadStates = useAtomValue(downloadStateListAtom) const { abortDownload } = useAbortDownload() const totalTransfferedSize = downloadStates.reduce( (partialSum: number, state) => partialSum + state.children.reduce( (partialSum: number, downloadItem: DownloadItem) => partialSum + downloadItem.size.transferred, 0 ), 0 ) const totalDownloadSize = downloadStates.reduce( (partialSum: number, state) => partialSum + state.children.reduce( (partialSum: number, downloadItem: DownloadItem) => partialSum + downloadItem.size.total, 0 ), 0 ) const totalPercentage = totalDownloadSize !== 0 ? ((totalTransfferedSize / totalDownloadSize) * 100).toFixed(2) : 0 const downloadTitle = `Downloading ${downloadStates .map((state) => state.type) .filter((value, index, self) => self.indexOf(value) === index) .join(', ') .trim()}` return ( {Object.values(downloadStates)?.length > 0 && (
{totalPercentage}%
} content={
{Object.values(downloadStates).map((item, i) => { // TODO: move this to another component const transferred = item.children.reduce( (sum: number, downloadItem: DownloadItem) => sum + downloadItem.size.transferred, 0 ) const total = item.children.reduce( (sum: number, downloadItem: DownloadItem) => sum + downloadItem.size.total, 0 ) return (

{item.title}

{formatDownloadPercentage(transferred / total)}
) })}
} /> )}
) } export default DownloadStatus