File size: 4,824 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
	PRODUCT_JETPACK_BACKUP_ADDON_STORAGE_10GB_MONTHLY,
	PRODUCT_JETPACK_BACKUP_ADDON_STORAGE_100GB_MONTHLY,
	PRODUCT_JETPACK_BACKUP_ADDON_STORAGE_1TB_MONTHLY,
	useJetpack100GbStorageAmountText,
	useJetpack10GbStorageAmountText,
	useJetpack1TbStorageAmountText,
} from '@automattic/calypso-products';
import { useMemo } from 'react';
import { STORAGE_ESTIMATION_ADDITIONAL_BUFFER } from 'calypso/components/backup-retention-management/constants';
import slugToSelectorProduct from 'calypso/my-sites/plans/jetpack-plans/slug-to-selector-product';
import { SelectorProduct } from 'calypso/my-sites/plans/jetpack-plans/types';
import useItemPrice from 'calypso/my-sites/plans/jetpack-plans/use-item-price';
import { useSelector } from 'calypso/state';
import { getCurrentUserCurrencyCode } from 'calypso/state/currency-code/selectors';
import {
	getActivityLogVisibleDays,
	getBackupCurrentSiteSize,
	getBackupRetentionDays,
	getRewindBytesAvailable,
} from 'calypso/state/rewind/selectors';
import type { Purchase } from 'calypso/lib/purchases/types';
import type { TranslateResult } from 'i18n-calypso';

export type SelectorProductWithStorage = SelectorProduct & {
	storage: TranslateResult;
};

export const BYTES_10GB = 10737418240;
export const BYTES_100GB = 107374182400;
export const BYTES_1TB = 1073741824000;

export const storageToUpsellProduct: Record< number, string > = {
	[ BYTES_10GB ]: PRODUCT_JETPACK_BACKUP_ADDON_STORAGE_10GB_MONTHLY,
	[ BYTES_100GB ]: PRODUCT_JETPACK_BACKUP_ADDON_STORAGE_100GB_MONTHLY,
	[ BYTES_1TB ]: PRODUCT_JETPACK_BACKUP_ADDON_STORAGE_1TB_MONTHLY,
};
export const isJetpackProductSlugMatch =
	( slugList: Array< string > ) =>
	( { subscriptionStatus, productSlug }: Purchase ) =>
		subscriptionStatus === 'active' && slugList.includes( productSlug );

export default ( siteId: number, retentionSelected?: number ) => {
	const TEN_GIGABYTES = useJetpack10GbStorageAmountText();
	const HUNDRED_GIGABYTES = useJetpack100GbStorageAmountText();
	const ONE_TERABYTES = useJetpack1TbStorageAmountText();

	const currencyCode = useSelector( getCurrentUserCurrencyCode );
	const lastBackupSize = useSelector( ( state ) =>
		getBackupCurrentSiteSize( state, siteId )
	) as number;
	const bytesAvailable = useSelector( ( state ) =>
		getRewindBytesAvailable( state, siteId )
	) as number;
	const estimatedCurrentSiteSize = lastBackupSize * ( STORAGE_ESTIMATION_ADDITIONAL_BUFFER + 1 );
	// Retention period included in customer plan
	const planRetentionPeriod = useSelector( ( state ) =>
		getActivityLogVisibleDays( state, siteId )
	);

	// Retention period set by customer (if any)
	const customerRetentionPeriod = useSelector( ( state ) =>
		getBackupRetentionDays( state, siteId )
	);
	// The retention days that currently applies for this customer.
	const currentRetentionPeriod =
		retentionSelected || customerRetentionPeriod || planRetentionPeriod || 0;

	const upsellSlug = useMemo( () => {
		const ADD_ON_STORAGE_MAP: Record< string, TranslateResult > = {
			[ PRODUCT_JETPACK_BACKUP_ADDON_STORAGE_10GB_MONTHLY ]: TEN_GIGABYTES,
			[ PRODUCT_JETPACK_BACKUP_ADDON_STORAGE_100GB_MONTHLY ]: HUNDRED_GIGABYTES,
			[ PRODUCT_JETPACK_BACKUP_ADDON_STORAGE_1TB_MONTHLY ]: ONE_TERABYTES,
		};

		//if usage has crossed over the storage limit, then dynamically calculate the upgrade option
		const additionalBytesNeeded =
			estimatedCurrentSiteSize * currentRetentionPeriod - bytesAvailable;

		//Since 1TB is our max upgrade but the additional storage needed is greater than 1TB, then just return 1TB
		if ( additionalBytesNeeded > BYTES_1TB ) {
			return {
				...slugToSelectorProduct( PRODUCT_JETPACK_BACKUP_ADDON_STORAGE_1TB_MONTHLY ),
				storage: ADD_ON_STORAGE_MAP[ PRODUCT_JETPACK_BACKUP_ADDON_STORAGE_1TB_MONTHLY ],
			} as SelectorProductWithStorage;
		}
		const matchedBytes =
			Object.keys( storageToUpsellProduct )
				.map( Number ) // .keys returns as strings, so convert it to a integer
				.sort( ( a, b ) => a - b ) // order of keys is not always guaranteed, so sort it before we do the search
				.find( ( bytes ) => bytes > additionalBytesNeeded ) ?? BYTES_10GB; // find the first add-on with storage aboev the current usage

		const upsellSlugId = storageToUpsellProduct[ matchedBytes ];
		return {
			...slugToSelectorProduct( upsellSlugId ),
			storage: ADD_ON_STORAGE_MAP[ upsellSlugId ],
		} as SelectorProductWithStorage;
	}, [
		TEN_GIGABYTES,
		HUNDRED_GIGABYTES,
		ONE_TERABYTES,
		estimatedCurrentSiteSize,
		currentRetentionPeriod,
		bytesAvailable,
	] );

	const { originalPrice, isFetching } = useItemPrice(
		siteId,
		upsellSlug,
		upsellSlug?.monthlyProductSlug || ''
	);

	return useMemo(
		() => ( { upsellSlug, originalPrice, isPriceFetching: isFetching as boolean, currencyCode } ),
		[ upsellSlug, currencyCode, originalPrice, isFetching ]
	);
};