Back

0% Statements 0/46
0% Branches 0/24
0% Functions 0/11
0% Lines 0/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                                                                                                                                                                                                                                                                                                                             
/* eslint-disable jsdoc/require-jsdoc */
 
import { Button, Card, CardBody, CardHeader, Icon, Notice } from '@wordpress/components';
import { useCallback, useEffect, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { cloud, arrowLeft } from '@wordpress/icons';
import { useNavigate, useSearchParams } from 'react-router';
import { useFormattedTime } from '../../data/use-formatted-time';
import { JetpackBackupRoutes } from '../../routes';
import DownloadError from './error';
import DownloadForm from './form';
import DownloadProgress from './progress';
import styles from './style.module.scss';
import DownloadSuccess from './success';
 
type DownloadStep = 'form' | 'progress' | 'success' | 'error';
 
function DownloadScreen() {
	const navigate = useNavigate();
	const [ searchParams, setSearchParams ] = useSearchParams();
	const rewindId = searchParams.get( 'rewindId' ) ?? '';
	const downloadIdParam = searchParams.get( 'downloadId' );
 
	// When the Overview navigates here with a downloadId in the URL (the
	// granular-download path kicked off in backup-details.tsx), jump
	// straight to the progress step. Otherwise start on the form.
	const initialDownloadId = downloadIdParam ? Number( downloadIdParam ) : null;
	const [ currentStep, setCurrentStep ] = useState< DownloadStep >(
		initialDownloadId ? 'progress' : 'form'
	);
	const [ downloadId, setDownloadId ] = useState< number | null >( initialDownloadId );
	const [ downloadUrl, setDownloadUrl ] = useState< string | null >( null );
	const [ fileSizeBytes, setFileSizeBytes ] = useState< string | undefined >();
	const [ errorMessage, setErrorMessage ] = useState< string | null >( null );
 
	// Clean up the downloadId query param once we've captured it — otherwise
	// reloading the success page would spin up a fresh progress poll.
	useEffect( () => {
		if ( downloadIdParam ) {
			setSearchParams(
				prev => {
					const next = new URLSearchParams( prev );
					next.delete( 'downloadId' );
					return next;
				},
				{ replace: true }
			);
		}
	}, [ downloadIdParam, setSearchParams ] );
 
	const handleDownloadInitiate = useCallback( ( newDownloadId: number ) => {
		setCurrentStep( 'progress' );
		setDownloadId( newDownloadId );
	}, [] );
 
	const handleDownloadComplete = useCallback(
		( newDownloadUrl: string, newFileSizeBytes?: string ) => {
			setCurrentStep( 'success' );
			setDownloadUrl( newDownloadUrl );
			setFileSizeBytes( newFileSizeBytes );
		},
		[]
	);
 
	const handleDownloadError = useCallback( () => {
		setCurrentStep( 'error' );
	}, [] );
 
	const handleRetry = useCallback( () => {
		setCurrentStep( 'form' );
		setDownloadId( null );
		setDownloadUrl( null );
		setFileSizeBytes( undefined );
		setErrorMessage( null );
	}, [] );
 
	const handleFormError = useCallback( ( message: string ) => {
		setErrorMessage( message );
	}, [] );
 
	const handleDismissError = useCallback( () => {
		setErrorMessage( null );
	}, [] );
 
	const handleBack = useCallback( () => {
		const target = rewindId
			? `${ JetpackBackupRoutes.Overview }?rewindId=${ rewindId }`
			: JetpackBackupRoutes.Overview;
		navigate( target );
	}, [ navigate, rewindId ] );
 
	const downloadPointDate = useFormattedTime(
		rewindId ? new Date( parseFloat( rewindId ) * 1000 ).toISOString() : '',
		{ dateStyle: 'medium', timeStyle: 'short' }
	);
 
	const renderStep = () => {
		switch ( currentStep ) {
			case 'form':
				return (
					<DownloadForm
						rewindId={ rewindId }
						onDownloadInitiate={ handleDownloadInitiate }
						onError={ handleFormError }
					/>
				);
			case 'progress':
				return downloadId ? (
					<DownloadProgress
						downloadId={ downloadId }
						onDownloadComplete={ handleDownloadComplete }
						onDownloadError={ handleDownloadError }
					/>
				) : null;
			case 'success':
				return downloadUrl ? (
					<DownloadSuccess
						downloadPointDate={ downloadPointDate }
						downloadUrl={ downloadUrl }
						fileSizeBytes={ fileSizeBytes }
					/>
				) : null;
			case 'error':
				return <DownloadError onRetry={ handleRetry } />;
		}
	};
 
	return (
		<div className={ styles.screenOuter }>
			<div className={ styles.screen }>
				<Button variant="tertiary" icon={ arrowLeft } onClick={ handleBack }>
					{ __( 'Back to overview', 'jetpack-backup-pkg' ) }
				</Button>
				<Card>
					<CardHeader>
						<div className={ styles.header }>
							<Icon icon={ cloud } />
							<div>
								<strong>{ __( 'Download backup', 'jetpack-backup-pkg' ) }</strong>
								{ downloadPointDate && (
									<div className={ styles.headerSubtitle }>
										{ sprintf(
											/* translators: %s is the date of the download point */
											__( 'Download point: %s', 'jetpack-backup-pkg' ),
											downloadPointDate
										) }
									</div>
								) }
							</div>
						</div>
					</CardHeader>
					<CardBody>
						{ errorMessage && (
							<Notice status="error" onRemove={ handleDismissError }>
								{ errorMessage }
							</Notice>
						) }
						{ renderStep() }
					</CardBody>
				</Card>
			</div>
		</div>
	);
}
 
export default DownloadScreen;