|
| 1 | +import { PRODUCT_JETPACK_STATS_YEARLY } from '@automattic/calypso-products'; |
| 2 | +import { PlanPrice } from '@automattic/components'; |
| 3 | +import { ProductsList } from '@automattic/data-stores'; |
| 4 | +import { Button } from '@wordpress/components'; |
| 5 | +import { useTranslate } from 'i18n-calypso'; |
| 6 | +import { useMemo } from 'react'; |
| 7 | +import useNoticeVisibilityMutation from 'calypso/my-sites/stats/hooks/use-notice-visibility-mutation'; |
| 8 | +import { useNoticeVisibilityQuery } from 'calypso/my-sites/stats/hooks/use-notice-visibility-query'; |
| 9 | +import { useSelector } from 'calypso/state'; |
| 10 | +import { getProductBySlug } from 'calypso/state/products-list/selectors'; |
| 11 | +import { getSiteSlug } from 'calypso/state/sites/selectors'; |
| 12 | +import { getSelectedSiteId } from 'calypso/state/ui/selectors'; |
| 13 | +import useIsPricingGridEligible from './hooks/use-eligibility'; |
| 14 | +import './style.scss'; |
| 15 | + |
| 16 | +const NOTICE_ID = 'pricing_grid'; |
| 17 | +const TRACKS_REFERRER = 'jetpack-stats-pricing-grid'; |
| 18 | + |
| 19 | +/** A feature is either present, absent, or present with a qualifier to compare against. */ |
| 20 | +type FeatureValue = boolean | string; |
| 21 | + |
| 22 | +interface Feature { |
| 23 | + name: string; |
| 24 | + free: FeatureValue; |
| 25 | + paid: FeatureValue; |
| 26 | +} |
| 27 | + |
| 28 | +export default function PricingGrid() { |
| 29 | + const translate = useTranslate(); |
| 30 | + const siteId = useSelector( getSelectedSiteId ); |
| 31 | + const siteSlug = useSelector( ( state ) => getSiteSlug( state, siteId ) ); |
| 32 | + |
| 33 | + const { isEligible, isLoading: isLoadingEligibility } = useIsPricingGridEligible( siteId ); |
| 34 | + |
| 35 | + // The server only reports this id while a dismissal is *not* in effect, so it reads |
| 36 | + // as visibility rather than dismissal. |
| 37 | + const { data: isVisible, isLoading: isLoadingVisibility } = useNoticeVisibilityQuery( |
| 38 | + siteId, |
| 39 | + NOTICE_ID |
| 40 | + ); |
| 41 | + const { mutate: dismiss } = useNoticeVisibilityMutation( siteId, NOTICE_ID, 'dismissed' ); |
| 42 | + |
| 43 | + const product = useSelector( ( state ) => |
| 44 | + getProductBySlug( state, PRODUCT_JETPACK_STATS_YEARLY ) |
| 45 | + ) as ProductsList.RawAPIProduct | null; |
| 46 | + |
| 47 | + // The four paid differentiators lead; everything below them is shared by both plans. |
| 48 | + const features: Feature[] = useMemo( |
| 49 | + () => [ |
| 50 | + { |
| 51 | + name: translate( 'UTM tracking' ), |
| 52 | + free: false, |
| 53 | + paid: translate( 'Included' ), |
| 54 | + }, |
| 55 | + { |
| 56 | + name: translate( 'Device stats' ), |
| 57 | + free: false, |
| 58 | + paid: translate( 'Included' ), |
| 59 | + }, |
| 60 | + { |
| 61 | + name: translate( 'Locations' ), |
| 62 | + free: translate( 'Country-level' ), |
| 63 | + paid: translate( 'Region and city' ), |
| 64 | + }, |
| 65 | + { |
| 66 | + name: translate( 'Priority support' ), |
| 67 | + free: false, |
| 68 | + paid: translate( 'Included' ), |
| 69 | + }, |
| 70 | + { name: translate( 'Views and visitors' ), free: true, paid: true }, |
| 71 | + { name: translate( 'Top posts and pages' ), free: true, paid: true }, |
| 72 | + { name: translate( 'Referrers and clicks' ), free: true, paid: true }, |
| 73 | + { name: translate( 'Search terms' ), free: true, paid: true }, |
| 74 | + { name: translate( 'Authors' ), free: true, paid: true }, |
| 75 | + { name: translate( 'Downloads and video plays' ), free: true, paid: true }, |
| 76 | + { name: translate( 'Insights and subscribers' ), free: true, paid: true }, |
| 77 | + { name: translate( 'Full history' ), free: true, paid: true }, |
| 78 | + { name: translate( 'GDPR-compliant' ), free: true, paid: true }, |
| 79 | + ], |
| 80 | + [ translate ] |
| 81 | + ); |
| 82 | + |
| 83 | + if ( isLoadingEligibility || isLoadingVisibility || ! isEligible || ! isVisible || ! product ) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + // Priced yearly, shown per month: the tier the price belongs to is spelled out below it. |
| 88 | + const monthlyPrice = product.cost / 12; |
| 89 | + const currencyCode = product.currency_code; |
| 90 | + |
| 91 | + const renderValue = ( value: FeatureValue ) => { |
| 92 | + if ( typeof value === 'string' ) { |
| 93 | + return value; |
| 94 | + } |
| 95 | + return ( |
| 96 | + <> |
| 97 | + <span aria-hidden="true" className={ value ? 'is-included' : 'is-excluded' }> |
| 98 | + { value ? '✓' : '✕' } |
| 99 | + </span> |
| 100 | + <span className="stats-pricing-grid__sr-only"> |
| 101 | + { value ? translate( 'Included' ) : translate( 'Not included' ) } |
| 102 | + </span> |
| 103 | + </> |
| 104 | + ); |
| 105 | + }; |
| 106 | + |
| 107 | + const renderFeatures = ( plan: 'free' | 'paid' ) => ( |
| 108 | + <dl className="stats-pricing-grid__features"> |
| 109 | + { features.map( ( feature ) => ( |
| 110 | + <div className="stats-pricing-grid__feature" key={ feature.name }> |
| 111 | + <dt>{ feature.name }</dt> |
| 112 | + <dd>{ renderValue( feature[ plan ] ) }</dd> |
| 113 | + </div> |
| 114 | + ) ) } |
| 115 | + </dl> |
| 116 | + ); |
| 117 | + |
| 118 | + return ( |
| 119 | + <div className="stats-pricing-grid"> |
| 120 | + <div className="stats-pricing-grid__intro"> |
| 121 | + <h2>{ translate( 'Choose your Stats plan' ) }</h2> |
| 122 | + <p>{ translate( 'Clear, concise, and actionable analysis of your site performance.' ) }</p> |
| 123 | + </div> |
| 124 | + |
| 125 | + <div className="stats-pricing-grid__plans"> |
| 126 | + <section className="stats-pricing-grid__plan is-primary"> |
| 127 | + <h3>{ translate( 'Paid' ) }</h3> |
| 128 | + <PlanPrice rawPrice={ monthlyPrice } currencyCode={ currencyCode } /> |
| 129 | + <p className="stats-pricing-grid__billing"> |
| 130 | + { translate( 'per month, from 10k monthly views, billed yearly' ) } |
| 131 | + </p> |
| 132 | + <Button |
| 133 | + className="stats-pricing-grid__cta" |
| 134 | + variant="primary" |
| 135 | + __next40pxDefaultSize |
| 136 | + href={ `/stats/purchase/${ siteSlug }?from=${ TRACKS_REFERRER }` } |
| 137 | + onClick={ () => dismiss() } |
| 138 | + > |
| 139 | + { translate( 'Get Paid Stats' ) } |
| 140 | + </Button> |
| 141 | + { renderFeatures( 'paid' ) } |
| 142 | + </section> |
| 143 | + |
| 144 | + <section className="stats-pricing-grid__plan"> |
| 145 | + <h3>{ translate( 'Free' ) }</h3> |
| 146 | + <PlanPrice rawPrice={ 0 } currencyCode={ currencyCode } /> |
| 147 | + <p className="stats-pricing-grid__billing"> |
| 148 | + { translate( 'Free forever, with the basics covered' ) } |
| 149 | + </p> |
| 150 | + <Button |
| 151 | + className="stats-pricing-grid__cta" |
| 152 | + variant="secondary" |
| 153 | + __next40pxDefaultSize |
| 154 | + href={ `/stats/day/${ siteSlug }` } |
| 155 | + onClick={ () => dismiss() } |
| 156 | + > |
| 157 | + { translate( 'Start for free' ) } |
| 158 | + </Button> |
| 159 | + { renderFeatures( 'free' ) } |
| 160 | + </section> |
| 161 | + </div> |
| 162 | + </div> |
| 163 | + ); |
| 164 | +} |
0 commit comments