// Copyright 2025 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import React, { useEffect, useState } from 'react'; import classNames from 'classnames'; import type { BackupMediaDownloadStatusType, BackupsSubscriptionType, BackupStatusType, } from '../types/backups'; import type { LocalizerType } from '../types/I18N'; import { formatTimestamp } from '../util/formatTimestamp'; import { SettingsControl as Control, FlowingSettingsControl as FlowingControl, LightIconLabel, SettingsRow, } from './PreferencesUtil'; import { missingCaseError } from '../util/missingCaseError'; import { Button, ButtonVariant } from './Button'; import type { PreferencesBackupPage } from '../types/PreferencesBackupPage'; import { SettingsPage } from '../types/Nav'; import { I18n } from './I18n'; import { PreferencesLocalBackups } from './PreferencesLocalBackups'; import type { ShowToastAction } from '../state/ducks/toast'; import type { PromptOSAuthReasonType, PromptOSAuthResultType, } from '../util/os/promptOSAuthMain'; import { ConfirmationDialog } from './ConfirmationDialog'; import { BackupMediaDownloadProgressSettings } from './BackupMediaDownloadProgressSettings'; export const SIGNAL_BACKUPS_LEARN_MORE_URL = 'https://support.signal.org/hc/articles/360007059752-Backup-and-Restore-Messages'; export function PreferencesBackups({ accountEntropyPool, backupKeyViewed, backupSubscriptionStatus, cloudBackupStatus, i18n, locale, localBackupFolder, onBackupKeyViewedChange, pickLocalBackupFolder, backupMediaDownloadStatus, cancelBackupMediaDownload, pauseBackupMediaDownload, resumeBackupMediaDownload, page, promptOSAuth, refreshCloudBackupStatus, refreshBackupSubscriptionStatus, setPage, showToast, }: { accountEntropyPool: string | undefined; backupKeyViewed: boolean; backupSubscriptionStatus: BackupsSubscriptionType; cloudBackupStatus?: BackupStatusType; localBackupFolder: string | undefined; i18n: LocalizerType; locale: string; onBackupKeyViewedChange: (keyViewed: boolean) => void; page: PreferencesBackupPage; backupMediaDownloadStatus: BackupMediaDownloadStatusType | undefined; cancelBackupMediaDownload: () => void; pauseBackupMediaDownload: () => void; resumeBackupMediaDownload: () => void; pickLocalBackupFolder: () => Promise; promptOSAuth: ( reason: PromptOSAuthReasonType ) => Promise; refreshCloudBackupStatus: () => void; refreshBackupSubscriptionStatus: () => void; setPage: (page: PreferencesBackupPage) => void; showToast: ShowToastAction; }): JSX.Element | null { const [authError, setAuthError] = useState>(); const [isAuthPending, setIsAuthPending] = useState(false); useEffect(() => { if (page === SettingsPage.Backups) { refreshBackupSubscriptionStatus(); } else if (page === SettingsPage.BackupsDetails) { refreshBackupSubscriptionStatus(); refreshCloudBackupStatus(); } }, [page, refreshBackupSubscriptionStatus, refreshCloudBackupStatus]); if (page === SettingsPage.BackupsDetails) { if (backupSubscriptionStatus.status === 'off') { setPage(SettingsPage.Backups); return null; } return ( ); } if ( page === SettingsPage.LocalBackups || page === SettingsPage.LocalBackupsKeyReference || page === SettingsPage.LocalBackupsSetupFolder || page === SettingsPage.LocalBackupsSetupKey ) { return ( ); } const learnMoreLink = (parts: Array) => ( {parts} ); const isLocalBackupsSetup = localBackupFolder && backupKeyViewed; return ( <>
{i18n('icu:Preferences--backup-section-description')}
{backupSubscriptionStatus.status === 'off' ? ( {i18n('icu:Preferences--signal-backups')}{' '}
} right={null} />
) : (
)}
{authError && ( setAuthError(undefined)} cancelButtonVariant={ButtonVariant.Secondary} cancelText={i18n('icu:ok')} > {getOSAuthErrorString(authError) ?? i18n('icu:error')} )} ); } function getSubscriptionDetails({ i18n, subscriptionStatus, locale, }: { i18n: LocalizerType; locale: string; subscriptionStatus: BackupsSubscriptionType; }): JSX.Element | null { if (subscriptionStatus.status === 'active') { return ( <> {subscriptionStatus.cost ? (
{new Intl.NumberFormat(locale, { style: 'currency', currency: subscriptionStatus.cost.currencyCode, currencyDisplay: 'narrowSymbol', }).format(subscriptionStatus.cost.amount)}{' '} / month
) : null} {subscriptionStatus.renewalTimestamp ? (
{i18n('icu:Preferences--backup-plan__renewal-date', { date: formatTimestamp(subscriptionStatus.renewalTimestamp, { dateStyle: 'medium', }), })}
) : null} ); } if (subscriptionStatus.status === 'pending-cancellation') { return ( <>
{i18n('icu:Preferences--backup-plan__canceled')}
{subscriptionStatus.expiryTimestamp ? (
{i18n('icu:Preferences--backup-plan__expiry-date', { date: formatTimestamp(subscriptionStatus.expiryTimestamp, { dateStyle: 'medium', }), })}
) : null} ); } return null; } export function renderBackupsSubscriptionDetails({ subscriptionStatus, i18n, locale, }: { locale: string; subscriptionStatus?: BackupsSubscriptionType; i18n: LocalizerType; }): JSX.Element | null { if (!subscriptionStatus) { return null; } const { status } = subscriptionStatus; switch (status) { case 'off': return null; case 'active': case 'pending-cancellation': return ( <>
{i18n('icu:Preferences--backup-media-plan__description')}
{getSubscriptionDetails({ i18n, locale, subscriptionStatus })}
{subscriptionStatus.status === 'active' ? (
) : (
)}
{i18n('icu:Preferences--backup-media-plan__note')}
); case 'free': return ( <>
{i18n('icu:Preferences--backup-messages-plan__description', { mediaDayCount: subscriptionStatus.mediaIncludedInBackupDurationDays, })}
{i18n( 'icu:Preferences--backup-messages-plan__cost-description' )}
{i18n('icu:Preferences--backup-messages-plan__note')}
); case 'not-found': case 'expired': return ( <>
{i18n('icu:Preferences--backup-plan-not-found__description')}
{i18n('icu:Preferences--backup-plan__not-found__note')}
); default: throw missingCaseError(status); } } export function renderBackupsSubscriptionSummary({ subscriptionStatus, i18n, locale, }: { locale: string; subscriptionStatus?: BackupsSubscriptionType; i18n: LocalizerType; }): JSX.Element | null { if (!subscriptionStatus) { return null; } const { status } = subscriptionStatus; switch (status) { case 'off': return null; case 'active': case 'pending-cancellation': return (
{i18n('icu:Preferences--backup-media-plan__description')}
{getSubscriptionDetails({ i18n, locale, subscriptionStatus })}
); case 'free': return (
{i18n('icu:Preferences--backup-messages-plan__description', { mediaDayCount: subscriptionStatus.mediaIncludedInBackupDurationDays, })}
{i18n('icu:Preferences--backup-messages-plan__cost-description')}
); case 'not-found': case 'expired': return (
{i18n('icu:Preferences--backup-plan-not-found__description')}
); default: throw missingCaseError(status); } } function BackupsDetailsPage({ cloudBackupStatus, backupSubscriptionStatus, i18n, locale, cancelBackupMediaDownload, pauseBackupMediaDownload, resumeBackupMediaDownload, backupMediaDownloadStatus, }: { cloudBackupStatus?: BackupStatusType; backupSubscriptionStatus: BackupsSubscriptionType; i18n: LocalizerType; locale: string; cancelBackupMediaDownload: () => void; pauseBackupMediaDownload: () => void; resumeBackupMediaDownload: () => void; backupMediaDownloadStatus?: BackupMediaDownloadStatusType; }): JSX.Element { const shouldShowMediaProgress = backupMediaDownloadStatus && backupMediaDownloadStatus.completedBytes < backupMediaDownloadStatus.totalBytes; return ( <>
{renderBackupsSubscriptionDetails({ subscriptionStatus: backupSubscriptionStatus, i18n, locale, })}
{cloudBackupStatus || shouldShowMediaProgress ? ( {cloudBackupStatus?.createdTimestamp ? (
{/* TODO (DESKTOP-8509) */} {i18n('icu:Preferences--backup-created-by-phone')} {formatTimestamp(cloudBackupStatus.createdTimestamp, { dateStyle: 'medium', timeStyle: 'short', })}
) : null} {shouldShowMediaProgress && backupMediaDownloadStatus ? (
) : null}
) : null} ); } export function getOSAuthErrorString( authError: Omit | undefined ): string | undefined { if (!authError) { return undefined; } // TODO: DESKTOP-8895 if (authError === 'unauthorized') { return 'This action could not be completed because system authentication failed. Please try again or open the Signal app on your mobile device and go to Backup Settings to view your backup key.'; } if (authError === 'unauthorized-no-windows-ucv') { return 'This action could not be completed because Windows Hello is not enabled on your computer. Please set up Windows Hello and try again, or open the Signal app on your mobile device and go to Backup Settings to view your backup key.'; } return 'The action could not be completed because authentication is not available on this computer. Please open the Signal app on your mobile device and go to Backup Settings to view your backup key.'; }