File size: 2,794 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
import { PRODUCT_1GB_SPACE } from '@automattic/calypso-products';
import { useMemo } from '@wordpress/element';
import { useTranslate } from 'i18n-calypso';
import * as ProductsList from '../../products-list';
import * as Site from '../../site';
import { getAddOnsList } from '../add-ons-list';
import useAddOnCheckoutLink from './use-add-on-checkout-link';
import { createAddOnPriceKey, useAddOnPrices } from './use-add-on-prices';
import type { AddOnMeta } from '../types';

interface Props {
	selectedSiteId?: number | null | undefined;
}

const useAddOns = ( { selectedSiteId }: Props = {} ): ( AddOnMeta | null )[] => {
	const translate = useTranslate();
	const checkoutLink = useAddOnCheckoutLink();
	const addOns = getAddOnsList();
	const addOnPrices = useAddOnPrices( addOns );
	const productSlugs = addOns.map( ( item ) => item.productSlug );
	const productsList = ProductsList.useProducts( productSlugs );
	const mediaStorage = Site.useSiteMediaStorage( { siteIdOrSlug: selectedSiteId } );

	return useMemo(
		() =>
			addOns.map( ( addOnMeta ) => {
				const { productSlug, quantity } = addOnMeta;
				const key = createAddOnPriceKey( addOnMeta );

				// TODO: can we not specify the product slug here for the storage add-on?
				const isLoading =
					productsList.isLoading || ( productSlug === PRODUCT_1GB_SPACE && mediaStorage.isLoading );
				const product = productsList.data?.[ productSlug ];
				const name = addOnMeta.name ? addOnMeta.name : product?.name || '';
				const description = addOnMeta.description ?? ( product?.description || '' );

				/**
				 * If the product is not found in the products list, remove the add-on.
				 * This should signal a wrong slug or a product that doesn't exist i.e. some sort of Bug.
				 * (not sure if add-on without a connected product is a valid use case)
				 */
				if ( ! product ) {
					return null;
				}

				const prices = addOnPrices[ key ];
				const formattedCost = prices?.formattedMonthlyPrice || '';
				const displayCost =
					product.term === 'month'
						? /* Translators: %(formattedCost)s: monthly price formatted with currency */
						  translate( '%(formattedCost)s/month, billed monthly', {
								args: {
									formattedCost,
								},
						  } )
						: /* Translators: %(monthlyCost)s: monthly price formatted with currency */
						  translate( '%(monthlyCost)s/month, billed yearly', {
								args: {
									monthlyCost: formattedCost,
								},
						  } );

				return {
					...addOnMeta,
					name,
					description,
					isLoading,
					prices,
					displayCost,
					checkoutLink: checkoutLink( selectedSiteId ?? null, productSlug, quantity ),
				};
			} ),
		[ addOnPrices, productsList.data, productsList.isLoading, checkoutLink, selectedSiteId ]
	);
};

export default useAddOns;