// Copyright 2024 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { useState } from 'react'; import type { LocalizerType } from '../types/Util'; import { formatFileSize } from '../util/formatFileSize'; import { roundFractionForProgressBar } from '../util/numbers'; import { ProgressCircle } from './ProgressCircle'; import { ContextMenu } from './ContextMenu'; import { BackupMediaDownloadCancelConfirmationDialog } from './BackupMediaDownloadCancelConfirmationDialog'; import { LeftPaneDialog } from './LeftPaneDialog'; import { WidthBreakpoint } from './_util'; export type PropsType = Readonly<{ i18n: LocalizerType; downloadedBytes: number; totalBytes: number; isIdle: boolean; isOnline: boolean; isPaused: boolean; widthBreakpoint: WidthBreakpoint; handleCancel: VoidFunction; handleClose: VoidFunction; handleResume: VoidFunction; handlePause: VoidFunction; }>; export function BackupMediaDownloadProgress({ i18n, downloadedBytes, totalBytes, isIdle, isOnline, isPaused, handleCancel: handleConfirmedCancel, handleClose, handleResume, handlePause, widthBreakpoint, }: PropsType): JSX.Element | null { const [isShowingCancelConfirmation, setIsShowingCancelConfirmation] = useState(false); if (totalBytes <= 0) { return null; } function handleCancel() { setIsShowingCancelConfirmation(true); } const fractionComplete = roundFractionForProgressBar( downloadedBytes / totalBytes ); let content: JSX.Element | undefined; let icon: JSX.Element | undefined; const isCompleted = fractionComplete === 1; const actionButton = isCompleted || isIdle ? ( ) : null} ); icon = (
); } else if (isIdle) { icon = (
); content = ( <>
{i18n('icu:BackupMediaDownloadProgress__title-idle', { currentSize: formatFileSize(downloadedBytes), totalSize: formatFileSize(totalBytes), })}
{i18n('icu:BackupMediaDownloadProgress__description-idle')}
); } else if (!isOnline) { content = ( <>
{i18n('icu:BackupMediaDownloadProgress__title-offline')}
{i18n('icu:BackupMediaDownloadProgress__description-offline')}
); icon = (
); } else { content = ( <>
{i18n('icu:BackupMediaDownloadProgress__title-in-progress')}
{i18n('icu:BackupMediaDownloadProgress__progressbar-hint', { currentSize: formatFileSize(downloadedBytes), totalSize: formatFileSize(totalBytes), })}
); icon = (
); } return (
{content}
{widthBreakpoint !== WidthBreakpoint.Narrow ? actionButton : null} {isShowingCancelConfirmation ? ( setIsShowingCancelConfirmation(false)} handleConfirmCancel={handleConfirmedCancel} /> ) : null}
); }