File size: 2,590 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useSelector } from 'react-redux';
import QueryRewindBackups from 'calypso/components/data/query-rewind-backups';
import BackupDelta from 'calypso/components/jetpack/backup-delta';
import BackupGettingStarted from 'calypso/components/jetpack/backup-getting-started';
import BackupPlaceholder from 'calypso/components/jetpack/backup-placeholder';
import DailyBackupStatus from 'calypso/components/jetpack/daily-backup-status';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import useDateWithOffset from 'calypso/lib/jetpack/hooks/use-date-with-offset';
import isRewindBackupsInitialized from 'calypso/state/rewind/selectors/is-rewind-backups-initialized';
import getSelectedSiteId from 'calypso/state/ui/selectors/get-selected-site-id';
import { useDailyBackupStatus, useRealtimeBackupStatus } from './hooks';

export const DailyStatus = ( { selectedDate } ) => {
	const siteId = useSelector( getSelectedSiteId );

	const { isLoading, lastBackupBeforeDate, lastBackupAttemptOnDate, deltas } = useDailyBackupStatus(
		siteId,
		selectedDate
	);

	const lastBackupDate = useDateWithOffset( lastBackupBeforeDate?.activityTs );

	if ( isLoading ) {
		return <BackupPlaceholder showDatePicker={ false } />;
	}

	return (
		<DailyBackupStatus
			{ ...{
				selectedDate,
				lastBackupDate,
				backup: lastBackupAttemptOnDate,
				deltas,
				lastBackupAttemptOnDate,
			} }
		/>
	);
};

export const RealtimeStatus = ( { selectedDate } ) => {
	const siteId = useSelector( getSelectedSiteId );

	const moment = useLocalizedMoment();

	const {
		isLoading,
		lastBackupBeforeDate,
		lastBackupAttempt,
		lastBackupAttemptOnDate,
		lastSuccessfulBackupOnDate,
		backupAttemptsOnDate,
		refetch,
	} = useRealtimeBackupStatus( siteId, selectedDate );

	const lastBackupDate = useDateWithOffset( lastBackupBeforeDate?.activityTs );

	const isInitialized = useSelector( ( state ) => isRewindBackupsInitialized( state, siteId ) );

	if ( isLoading || ! isInitialized ) {
		return (
			<>
				<BackupPlaceholder showDatePicker={ false } />
				<QueryRewindBackups siteId={ siteId } />
			</>
		);
	}

	return (
		<>
			<DailyBackupStatus
				{ ...{
					selectedDate,
					lastBackupDate,
					backup: lastSuccessfulBackupOnDate || lastBackupAttemptOnDate,
					lastBackupAttempt,
					lastBackupAttemptOnDate,
					refetch,
				} }
			/>

			<BackupGettingStarted />

			{ lastBackupAttemptOnDate && (
				<BackupDelta
					{ ...{
						realtimeBackups: backupAttemptsOnDate,
						isToday: moment().isSame( selectedDate, 'day' ),
					} }
				/>
			) }
		</>
	);
};