File size: 8,774 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { FEATURE_SFTP, getPlan, PLAN_BUSINESS } from '@automattic/calypso-products';
import page from '@automattic/calypso-router';
import { useHasEnTranslation } from '@automattic/i18n-utils';
import { Spinner } from '@wordpress/components';
import { translate } from 'i18n-calypso';
import { useRef, useEffect } from 'react';
import { HostingCard, HostingCardGrid } from 'calypso/components/hosting-card';
import { HostingHero, HostingHeroButton } from 'calypso/components/hosting-hero';
import InlineSupportLink from 'calypso/components/inline-support-link';
import { useSiteTransferStatusQuery } from 'calypso/landing/stepper/hooks/use-site-transfer/query';
import { useSelector, useDispatch } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { transferStates } from 'calypso/state/atomic-transfer/constants';
import isSiteWpcomAtomic from 'calypso/state/selectors/is-site-wpcom-atomic';
import siteHasFeature from 'calypso/state/selectors/site-has-feature';
import { getSiteSlug } from 'calypso/state/sites/selectors';
import { getSelectedSite, getSelectedSiteId } from 'calypso/state/ui/selectors';
import HostingActivationButton from './hosting-activation-button';

import './hosting-features.scss';

type PromoCardProps = {
	title: string;
	text: string;
	supportContext: string;
};

const PromoCard = ( { title, text, supportContext }: PromoCardProps ) => (
	<HostingCard inGrid className="hosting-features__card" title={ title }>
		<p>{ text }</p>
		{ translate( '{{supportLink}}Learn more{{/supportLink}}', {
			components: {
				supportLink: <InlineSupportLink supportContext={ supportContext } showIcon={ false } />,
			},
		} ) }
	</HostingCard>
);

type HostingFeaturesProps = {
	showAsTools?: boolean;
};

const HostingFeatures = ( { showAsTools }: HostingFeaturesProps ) => {
	const dispatch = useDispatch();
	const { searchParams } = new URL( document.location.toString() );
	const siteId = useSelector( getSelectedSiteId );
	const siteSlug = useSelector( ( state ) => getSiteSlug( state, siteId ) || '' );
	const isSiteAtomic = useSelector( ( state ) => isSiteWpcomAtomic( state, siteId ) );
	const hasSftpFeature = useSelector( ( state ) => siteHasFeature( state, siteId, FEATURE_SFTP ) );
	const isPlanExpired = useSelector( ( state ) => !! getSelectedSite( state )?.plan?.expired );

	// The ref is required to persist the value of redirect_to after renders
	const redirectToRef = useRef( searchParams.get( 'redirect_to' ) );

	let redirectUrl = redirectToRef.current as string;
	if ( ! redirectUrl ) {
		redirectUrl = hasSftpFeature ? `/hosting-config/${ siteSlug }` : `/overview/${ siteId }`;
	}

	const hasEnTranslation = useHasEnTranslation();

	const { data: siteTransferData } = useSiteTransferStatusQuery( siteId || undefined, {
		refetchIntervalInBackground: true,
	} );

	const shouldRenderActivatingCopy =
		( siteTransferData?.isTransferring || siteTransferData?.status === transferStates.COMPLETED ) &&
		! isPlanExpired;

	useEffect( () => {
		if ( isSiteAtomic && ! isPlanExpired ) {
			page.replace( redirectUrl );
		}
	}, [ isSiteAtomic, isPlanExpired, redirectUrl ] );

	const upgradeLink = `https://wordpress.com/checkout/${ encodeURIComponent( siteSlug ) }/business`;
	const promoCards = [
		{
			title: translate( 'Deployments' ),
			text: translate(
				'Automate updates from GitHub to streamline workflows, reduce errors, and enable faster deployments.'
			),
			supportContext: 'github-deployments',
		},
		{
			title: translate( 'Monitoring' ),
			text: translate(
				"Proactively monitor your site's performance, including requests per minute and average response time."
			),
			supportContext: 'site-monitoring-metrics',
		},
		{
			title: translate( 'Logs' ),
			text: translate(
				'View and download PHP error and web server logs to diagnose and resolve issues quickly.'
			),
			supportContext: 'site-monitoring-logs',
		},
		{
			title: translate( 'Staging Site' ),
			text: translate( 'Preview and troubleshoot changes before updating your production site.' ),
			supportContext: 'hosting-staging-site',
		},
		{
			title: hasEnTranslation( 'Server Settings' )
				? translate( 'Server Settings' )
				: translate( 'Server Configuration' ),
			text: translate(
				"Access your site's database and tailor your server settings to your specific needs."
			),
			supportContext: 'hosting-configuration',
		},
		{
			title: translate( 'SFTP & SSH Access' ),
			text: translate(
				'Securely access and edit your site via SFTP, or use SSH for file management and WP-CLI commands.'
			),
			supportContext: 'hosting-sftp',
		},
	];

	const canSiteGoAtomic = ! isSiteAtomic && hasSftpFeature;
	const showActivationButton = canSiteGoAtomic;

	const activateTitle = hasEnTranslation( 'Activate all hosting features' )
		? translate( 'Activate all hosting features' )
		: translate( 'Activate all developer tools' );

	const activateTitleAsTools = translate( 'Activate all advanced tools' );

	const activationStatusTitle = translate( 'Activating hosting features' );
	const activationStatusTitleAsTools = translate( 'Activating advanced tools' );

	const activationStatusDescription = translate(
		"The hosting features will appear here automatically when they're ready!",
		{
			comment: 'Description of the hosting features page when the features are being activated.',
		}
	);
	const activationStatusDescriptionAsTools = translate(
		"The advanced tools will appear here automatically when they're ready!",
		{
			comment: 'Description of the advanced tools page when the features are being activated.',
		}
	);

	const unlockTitle = hasEnTranslation( 'Unlock all hosting features' )
		? translate( 'Unlock all hosting features' )
		: translate( 'Unlock all developer tools' );

	const unlockTitleAsTools = translate( 'Unlock all advanced tools' );

	const activateDescription = hasEnTranslation(
		'Your plan includes all the hosting features listed below. Click "Activate now" to begin.'
	)
		? translate(
				'Your plan includes all the hosting features listed below. Click "Activate now" to begin.'
		  )
		: translate(
				'Your plan includes all the developer tools listed below. Click "Activate now" to begin.'
		  );

	const activateDescriptionAsTools = translate(
		'Your plan includes all the advanced tools listed below. Click "Activate now" to begin.'
	);

	const unlockDescription = hasEnTranslation(
		'Upgrade to the %(planName)s plan or higher to get access to all hosting features'
	)
		? // translators: %(planName)s is a plan name. E.g. Business plan.
		  translate(
				'Upgrade to the %(planName)s plan or higher to get access to all hosting features',
				{
					args: {
						planName: getPlan( PLAN_BUSINESS )?.getTitle() ?? '',
					},
				}
		  )
		: // translators: %(planName)s is a plan name. E.g. Business plan.
		  translate(
				'Upgrade to the %(planName)s plan or higher to get access to all developer tools',
				{
					args: {
						planName: getPlan( PLAN_BUSINESS )?.getTitle() ?? '',
					},
				}
		  );

	const unlockDescriptionAsTools = translate(
		'Upgrade to the %(planName)s plan or higher to get access to all advanced tools',
		{
			args: {
				planName: getPlan( PLAN_BUSINESS )?.getTitle() ?? '',
			},
		}
	);

	let title;
	let description;
	let buttons;
	if ( shouldRenderActivatingCopy ) {
		title = showAsTools ? activationStatusTitleAsTools : activationStatusTitle;
		description = showAsTools ? activationStatusDescriptionAsTools : activationStatusDescription;
	} else if ( showActivationButton ) {
		title = showAsTools ? activateTitleAsTools : activateTitle;
		description = showAsTools ? activateDescriptionAsTools : activateDescription;
		buttons = <HostingActivationButton redirectUrl={ redirectUrl } />;
	} else {
		title = showAsTools ? unlockTitleAsTools : unlockTitle;
		description = showAsTools ? unlockDescriptionAsTools : unlockDescription;
		buttons = (
			<HostingHeroButton
				href={ upgradeLink }
				onClick={ () =>
					dispatch( recordTracksEvent( 'calypso_hosting_features_upgrade_plan_click' ) )
				}
			>
				{ translate( 'Upgrade to %(planName)s', {
					args: { planName: getPlan( PLAN_BUSINESS )?.getTitle() ?? '' },
				} ) }
			</HostingHeroButton>
		);
	}

	return (
		<div className="hosting-features">
			<HostingHero>
				{ shouldRenderActivatingCopy && <Spinner className="hosting-features__content-spinner" /> }
				<h1>{ title }</h1>
				<p>{ description }</p>
				{ buttons }
			</HostingHero>
			<HostingCardGrid>
				{ promoCards.map( ( card ) => (
					<PromoCard
						key={ card.title }
						title={ card.title }
						text={ card.text }
						supportContext={ card.supportContext }
					/>
				) ) }
			</HostingCardGrid>
		</div>
	);
};

export default HostingFeatures;