File size: 4,711 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Button } from '@automattic/components';
import { useTranslate } from 'i18n-calypso';
import { useSelector } from 'react-redux';
import useAtomicSiteHasEquivalentFeatureToPlugin from 'calypso/my-sites/plugins/use-atomic-site-has-equivalent-feature-to-plugin';
import { getBillingInterval } from 'calypso/state/marketplace/billing-interval/selectors';
import getSiteConnectionStatus from 'calypso/state/selectors/get-site-connection-status';
import isSiteAutomatedTransfer from 'calypso/state/selectors/is-site-automated-transfer';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import {
	getSelectedSite,
	getSelectedSiteId,
	getSelectedSiteSlug,
} from 'calypso/state/ui/selectors';
import { PluginPrice } from '../plugin-price';
import PreinstalledPremiumPluginPriceDisplay from '../plugin-price/preinstalled-premium-plugin-price-display';
import usePreinstalledPremiumPlugin from '../use-preinstalled-premium-plugin';
import CTAButton from './CTA-button';

export default function PluginDetailsCTAPreinstalledPremiumPlugins( {
	isPluginInstalledOnsite,
	plugin,
} ) {
	const translate = useTranslate();

	const selectedSite = useSelector( getSelectedSite );
	const selectedSiteId = useSelector( getSelectedSiteId );
	const selectedSiteSlug = useSelector( getSelectedSiteSlug );
	const isAtomic = useSelector( ( state ) => isSiteAutomatedTransfer( state, selectedSiteId ) );
	const isJetpack = useSelector( ( state ) => isJetpackSite( state, selectedSiteId ) );
	const isJetpackSelfHosted = selectedSiteId && isJetpack && ! isAtomic;
	const isSimple = selectedSiteId && ! isAtomic && ! isJetpack;
	const isSiteConnected = useSelector( ( state ) =>
		getSiteConnectionStatus( state, selectedSiteId )
	);

	const billingPeriod = useSelector( getBillingInterval );
	const {
		preinstalledPremiumPluginProduct,
		isPreinstalledPremiumPluginFreeInstalled,
		isPreinstalledPremiumPluginPaidInstalled,
	} = usePreinstalledPremiumPlugin( plugin.slug );

	// Atomic sites already include features such as Jetpack backup, scan, videopress, publicize, and search. So
	// therefore we should prevent users from installing these standalone plugin equivalents.
	const atomicSiteHasEquivalentFeatureToPlugin = useAtomicSiteHasEquivalentFeatureToPlugin(
		plugin.slug
	);

	const managedPluginMessage = (
		<span className="plugin-details-cta__preinstalled">
			{ translate( '%s is automatically managed for you.', { args: plugin.name } ) }
		</span>
	);
	const pluginPrice = (
		<>
			<div className="plugin-details-cta__price">
				<PluginPrice plugin={ plugin } billingPeriod={ billingPeriod }>
					{ ( { isFetching, price, period } ) =>
						isFetching ? (
							<div className="plugin-details-cta__price-placeholder">...</div>
						) : (
							<PreinstalledPremiumPluginPriceDisplay
								className="plugin-details-cta__period"
								period={ period }
								pluginSlug={ plugin.slug }
								price={ price }
							/>
						)
					}
				</PluginPrice>
			</div>
		</>
	);

	const includedWithAtomicPlanButton = (
		<div className="plugin-details-cta__install">
			<Button className="plugin-details-cta__install-button" disabled>
				{ translate( 'Included with your plan' ) }
			</Button>
		</div>
	);

	const startForFreeButton = (
		<div className="plugin-details-cta__install">
			<Button
				className="plugin-details-cta__install-button"
				href={ `/checkout/${ selectedSiteSlug }/${ preinstalledPremiumPluginProduct }` }
				primary
			>
				{ translate( 'Start for Free' ) }
			</Button>
		</div>
	);

	const upgradeButton = (
		<div className="plugin-details-cta__install">
			<Button
				className="plugin-details-cta__install-button"
				href={ `/checkout/${ selectedSiteSlug }/${ preinstalledPremiumPluginProduct }` }
				primary
			>
				{ translate( 'Upgrade %s', { args: plugin.name } ) }
			</Button>
		</div>
	);
	const activateButton = (
		<div className="plugin-details-cta__install">
			<CTAButton
				billingPeriod={ billingPeriod }
				isJetpackSelfHosted={ isJetpackSelfHosted }
				isSiteConnected={ isSiteConnected }
				plugin={ plugin }
				selectedSite={ selectedSite }
			/>
		</div>
	);

	if ( atomicSiteHasEquivalentFeatureToPlugin ) {
		return includedWithAtomicPlanButton;
	}

	if ( isPreinstalledPremiumPluginPaidInstalled ) {
		return managedPluginMessage;
	}

	if ( ! isPreinstalledPremiumPluginFreeInstalled ) {
		return startForFreeButton;
	}

	if ( isSimple ) {
		return (
			<>
				{ pluginPrice }
				{ managedPluginMessage }
				{ upgradeButton }
			</>
		);
	}

	if ( ! isPluginInstalledOnsite ) {
		return (
			<>
				{ pluginPrice }
				{ activateButton }
			</>
		);
	}

	return (
		<>
			{ pluginPrice }
			{ upgradeButton }
		</>
	);
}