|
|
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 ); |
|
|
|
|
|
|
|
|
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 ( ! product ) { |
|
|
return null; |
|
|
} |
|
|
|
|
|
const prices = addOnPrices[ key ]; |
|
|
const formattedCost = prices?.formattedMonthlyPrice || ''; |
|
|
const displayCost = |
|
|
product.term === 'month' |
|
|
? |
|
|
translate( '%(formattedCost)s/month, billed monthly', { |
|
|
args: { |
|
|
formattedCost, |
|
|
}, |
|
|
} ) |
|
|
: |
|
|
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; |
|
|
|