import { getPlan, PlanSlug, PLAN_MONTHLY_PERIOD, is100Year } from '@automattic/calypso-products'; import { Button, PlanPrice, LoadingPlaceholder } from '@automattic/components'; import { usePricingMetaForGridPlans } from '@automattic/data-stores/src/plans'; import { usePlanBillingDescription } from '@automattic/plans-grid-next'; import clsx from 'clsx'; import { useTranslate } from 'i18n-calypso'; import { useSelector, useDispatch } from 'react-redux'; import { useLocalizedMoment } from 'calypso/components/localized-moment'; import useCheckPlanAvailabilityForPurchase from 'calypso/my-sites/plans-features-main/hooks/use-check-plan-availability-for-purchase'; import { recordTracksEvent } from 'calypso/state/analytics/actions'; import { getCurrentPlan } from 'calypso/state/sites/plans/selectors'; import { getSelectedPurchase, getSelectedSite } from 'calypso/state/ui/selectors'; import './style.scss'; type PlanPricingProps = { inline?: boolean; }; export default function PlanPricing( { inline }: PlanPricingProps ) { const translate = useTranslate(); const dispatch = useDispatch(); const moment = useLocalizedMoment(); const site = useSelector( getSelectedSite ); const planDetails = site?.plan; const planSlug = ( planDetails?.product_slug || '' ) as PlanSlug; const planData = useSelector( ( state ) => getCurrentPlan( state, site?.ID ) ); const isFreePlan = planDetails?.is_free; const planPurchase = useSelector( getSelectedPurchase ); const pricing = usePricingMetaForGridPlans( { coupon: undefined, planSlugs: [ planSlug ], siteId: site?.ID, useCheckPlanAvailabilityForPurchase, } )?.[ planSlug ]; const is100YearPlan = planPurchase && is100Year( planPurchase ); const planPurchaseLoading = ! isFreePlan && planPurchase === null; const isLoading = ! pricing || ! planData || planPurchaseLoading; const planBillingDescription = usePlanBillingDescription( { siteId: site?.ID, planSlug, pricing: pricing ?? null, isMonthlyPlan: pricing?.billingPeriod === PLAN_MONTHLY_PERIOD, useCheckPlanAvailabilityForPurchase, } ); const getBillingDetails = () => { if ( isFreePlan ) { return null; } return <>{ planBillingDescription || getPlan( planSlug )?.getBillingTimeFrame?.() }.; }; const getExpireDetails = () => { if ( isFreePlan ) { return translate( 'No expiration date.' ); } return site?.plan?.expired ? translate( 'Your plan has expired.' ) : translate( 'Expires on %s.', { args: moment( planData?.expiryDate ).format( 'LL' ), } ); }; const renderPrice = () => { if ( is100YearPlan ) { return null; } const price = ( ); if ( inline ) { return isLoading ? ( ) : (

{ price } { getBillingDetails() }

); } return isLoading ? ( ) : ( <>
{ price } { translate( '/mo', { comment: '/mo is short for per month, referring to the monthly price of a site plan', } ) }

{ getBillingDetails() }

); }; return ( <> { renderPrice() } { isLoading ? ( ) : (
{ getExpireDetails() }
{ isFreePlan && ( ) } { site?.plan?.expired && ( <> ) }
) } ); }