File size: 1,785 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
import { Moment } from 'moment';
import { useCallback } from 'react';
import { useLocalizedMoment } from 'calypso/components/localized-moment';
import useDateWithOffset from 'calypso/lib/jetpack/hooks/use-date-with-offset';
import { useFirstMatchingBackupAttempt } from '../hooks';

type CanGoToDateHook = (
	siteId: number,
	selectedDate: Moment,
	oldestDateAvailable?: Moment,
	hasNoBackups?: boolean
) => ( desiredDate: Moment ) => boolean;

export const useCanGoToDate: CanGoToDateHook = (
	siteId,
	selectedDate,
	oldestDateAvailable,
	hasNoBackups
) => {
	const moment = useLocalizedMoment();
	const today = useDateWithOffset( moment() );

	return useCallback(
		( desiredDate ) => {
			const goingForwardInTime = desiredDate.isAfter( selectedDate, 'day' );
			const goingBackwardInTime = desiredDate.isBefore( selectedDate, 'day' );

			if ( goingBackwardInTime ) {
				// If there are no backups, we won't show the navigation
				if ( hasNoBackups ) {
					return false;
				}

				// If we don't know the oldest date with data,
				// always allow backward navigation
				if ( ! oldestDateAvailable ) {
					return true;
				}

				// Only go as far back as the oldest date we have information on
				return desiredDate.isSameOrAfter( oldestDateAvailable, 'day' );
			}

			if ( goingForwardInTime ) {
				// Just make sure we don't let anyone accidentally slip
				// into the future
				return desiredDate.isSameOrBefore( today );
			}

			// If we're going neither forward nor backward,
			// then everything's fine (this should never happen)
			return true;
		},
		[ selectedDate, today, oldestDateAvailable, hasNoBackups ]
	);
};

export const useFirstKnownBackupAttempt = ( siteId: number ) => {
	return useFirstMatchingBackupAttempt( siteId, { sortOrder: 'asc' } );
};