signal-desktop/ts/components/installScreen/InstallScreenBackupImportStep.tsx

133 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-08-27 21:00:41 +00:00
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2024-09-11 18:03:18 +00:00
import React, { useState, useCallback } from 'react';
2024-08-27 21:00:41 +00:00
2024-09-04 02:56:13 +00:00
import type { LocalizerType } from '../../types/Util';
import { formatFileSize } from '../../util/formatFileSize';
import { TitlebarDragArea } from '../TitlebarDragArea';
import { ProgressBar } from '../ProgressBar';
2024-09-11 18:03:18 +00:00
import { ConfirmationDialog } from '../ConfirmationDialog';
2024-09-04 02:56:13 +00:00
import { InstallScreenSignalLogo } from './InstallScreenSignalLogo';
import { roundFractionForProgressBar } from '../../util/numbers';
2024-08-27 21:00:41 +00:00
// We can't always use destructuring assignment because of the complexity of this props
// type.
export type PropsType = Readonly<{
i18n: LocalizerType;
currentBytes?: number;
totalBytes?: number;
2024-09-11 18:03:18 +00:00
onCancel: () => void;
2024-08-27 21:00:41 +00:00
}>;
2024-09-04 02:56:13 +00:00
export function InstallScreenBackupImportStep({
2024-08-27 21:00:41 +00:00
i18n,
currentBytes,
totalBytes,
2024-09-11 18:03:18 +00:00
onCancel,
2024-08-27 21:00:41 +00:00
}: PropsType): JSX.Element {
2024-09-11 18:03:18 +00:00
const [isConfirmingCancel, setIsConfirmingCancel] = useState(false);
const confirmCancel = useCallback(() => {
setIsConfirmingCancel(true);
}, []);
const abortCancel = useCallback(() => {
setIsConfirmingCancel(false);
}, []);
const onCancelWrap = useCallback(() => {
onCancel();
setIsConfirmingCancel(false);
}, [onCancel]);
2024-08-27 21:00:41 +00:00
let progress: JSX.Element;
2024-09-11 18:03:18 +00:00
let isCancelPossible = true;
2024-08-27 21:00:41 +00:00
if (currentBytes != null && totalBytes != null) {
2024-09-11 18:03:18 +00:00
isCancelPossible = currentBytes !== totalBytes;
const fractionComplete = roundFractionForProgressBar(
currentBytes / totalBytes
);
2024-08-27 21:00:41 +00:00
progress = (
<>
<ProgressBar
fractionComplete={fractionComplete}
isRTL={i18n.getLocaleDirection() === 'rtl'}
/>
2024-09-11 18:03:18 +00:00
<div className="InstallScreenBackupImportStep__progressbar-hint">
2024-08-27 21:00:41 +00:00
{i18n('icu:BackupImportScreen__progressbar-hint', {
currentSize: formatFileSize(currentBytes),
totalSize: formatFileSize(totalBytes),
fractionComplete,
2024-08-27 21:00:41 +00:00
})}
</div>
</>
);
} else {
progress = (
<>
<ProgressBar
fractionComplete={0}
isRTL={i18n.getLocaleDirection() === 'rtl'}
/>
2024-09-11 18:03:18 +00:00
<div className="InstallScreenBackupImportStep__progressbar-hint">
{i18n('icu:BackupImportScreen__progressbar-hint--preparing')}
2024-08-27 21:00:41 +00:00
</div>
</>
);
}
return (
2024-09-11 18:03:18 +00:00
<div className="InstallScreenBackupImportStep">
2024-08-27 21:00:41 +00:00
<TitlebarDragArea />
<InstallScreenSignalLogo />
2024-09-11 18:03:18 +00:00
<div className="InstallScreenBackupImportStep__content">
<h3 className="InstallScreenBackupImportStep__title">
2024-08-27 21:00:41 +00:00
{i18n('icu:BackupImportScreen__title')}
</h3>
{progress}
2024-09-11 18:03:18 +00:00
<div className="InstallScreenBackupImportStep__description">
2024-08-27 21:00:41 +00:00
{i18n('icu:BackupImportScreen__description')}
</div>
</div>
2024-09-11 18:03:18 +00:00
{isCancelPossible && (
<button
className="InstallScreenBackupImportStep__cancel"
type="button"
onClick={confirmCancel}
>
{i18n('icu:BackupImportScreen__cancel')}
</button>
)}
{isConfirmingCancel && (
<ConfirmationDialog
dialogName="InstallScreenBackupImportStep.confirmCancel"
title={i18n('icu:BackupImportScreen__cancel-confirmation__title')}
cancelText={i18n(
'icu:BackupImportScreen__cancel-confirmation__cancel'
)}
actions={[
{
action: onCancelWrap,
style: 'negative',
text: i18n(
'icu:BackupImportScreen__cancel-confirmation__confirm'
),
},
]}
i18n={i18n}
onClose={abortCancel}
>
{i18n('icu:BackupImportScreen__cancel-confirmation__body')}
</ConfirmationDialog>
)}
2024-08-27 21:00:41 +00:00
</div>
);
}