File size: 13,228 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import config from '@automattic/calypso-config';
import {
	FEATURE_SFTP,
	FEATURE_SFTP_DATABASE,
	WPCOM_FEATURES_ATOMIC,
} from '@automattic/calypso-products';
import { Button } from '@automattic/components';
import { useTranslate } from 'i18n-calypso';
import React, { Fragment, useCallback, useState } from 'react';
import { useDispatch } from 'react-redux';
import DocumentHead from 'calypso/components/data/document-head';
import QueryJetpackModules from 'calypso/components/data/query-jetpack-modules';
import QueryReaderTeams from 'calypso/components/data/query-reader-teams';
import QuerySites from 'calypso/components/data/query-sites';
import FeatureExample from 'calypso/components/feature-example';
import NavigationHeader from 'calypso/components/navigation-header';
import Notice from 'calypso/components/notice';
import NoticeAction from 'calypso/components/notice/notice-action';
import { Panel } from 'calypso/components/panel';
import { ScrollToAnchorOnMount } from 'calypso/components/scroll-to-anchor-on-mount';
import { HostingUpsellNudge } from 'calypso/hosting/server-settings/components/hosting-upsell-nudge';
import PhpMyAdminCard from 'calypso/hosting/server-settings/components/phpmyadmin-card/card';
import RestorePlanSoftwareCard from 'calypso/hosting/server-settings/components/restore-plan-software-card';
import { SftpCard } from 'calypso/hosting/server-settings/components/sftp-card/card';
import HostingActivateStatus from 'calypso/hosting/server-settings/hosting-activate-status';
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker';
import TrackComponentView from 'calypso/lib/analytics/track-component-view';
import { TrialAcknowledgeModal } from 'calypso/my-sites/plans/trials/trial-acknowledge/acknowlege-modal';
import { WithOnclickTrialRequest } from 'calypso/my-sites/plans/trials/trial-acknowledge/with-onclick-trial-request';
import TrialBanner from 'calypso/my-sites/plans/trials/trial-banner';
import JetpackMonitor from 'calypso/my-sites/site-settings/form-jetpack-monitor';
import CacheCard from 'calypso/sites/settings/performance/form';
import DefensiveModeCard from 'calypso/sites/settings/server/defensive-mode-form';
import WebServerSettingsCard from 'calypso/sites/settings/server/server-configuration-form';
import { useSelector } from 'calypso/state';
import { recordTracksEvent } from 'calypso/state/analytics/actions';
import { transferStates } from 'calypso/state/automated-transfer/constants';
import { getAutomatedTransferStatus } from 'calypso/state/automated-transfer/selectors';
import { getAtomicHostingIsLoadingSftpData } from 'calypso/state/selectors/get-atomic-hosting-is-loading-sftp-data';
import isRequestingSiteFeatures from 'calypso/state/selectors/is-requesting-site-features';
import isSiteWpcomAtomic from 'calypso/state/selectors/is-site-wpcom-atomic';
import isSiteWpcomStaging from 'calypso/state/selectors/is-site-wpcom-staging';
import { isUserEligibleForFreeHostingTrial } from 'calypso/state/selectors/is-user-eligible-for-free-hosting-trial';
import siteHasFeature from 'calypso/state/selectors/site-has-feature';
import { isSiteOnBusinessTrial, isSiteOnECommerceTrial } from 'calypso/state/sites/plans/selectors';
import isJetpackSite from 'calypso/state/sites/selectors/is-jetpack-site';
import {
	getSelectedSite,
	getSelectedSiteId,
	getSelectedSiteSlug,
} from 'calypso/state/ui/selectors';

import './style.scss';

const HEADING_OFFSET = 30;

type CardEntry = {
	feature: string;
	content: React.ReactElement;
	type: 'basic' | 'advanced';
};

type ShowEnabledFeatureCardsProps = {
	availableTypes: CardEntry[ 'type' ][];
	cards: CardEntry[];
	showDisabledCards?: boolean;
};

const ShowEnabledFeatureCards = ( {
	availableTypes,
	cards,
	showDisabledCards = true,
}: ShowEnabledFeatureCardsProps ) => {
	const enabledCards = cards.filter(
		( card ) => ! card.type || availableTypes.includes( card.type )
	);
	const disabledCards = cards.filter(
		( card ) => card.type && ! availableTypes.includes( card.type )
	);

	return (
		<>
			{ enabledCards.map( ( card ) => {
				return <Fragment key={ card.feature }>{ card.content }</Fragment>;
			} ) }
			{ showDisabledCards && disabledCards.length > 0 && (
				<FeatureExample>
					{ disabledCards.map( ( card ) => {
						return <Fragment key={ card.feature }>{ card.content }</Fragment>;
					} ) }
				</FeatureExample>
			) }
		</>
	);
};

type AllCardsProps = {
	isAdvancedHostingDisabled?: boolean;
	isBasicHostingDisabled?: boolean;
	isBusinessTrial?: boolean;
	siteId: number | null;
	siteSlug: string | null;
	isJetpack: boolean | null;
};

const AllCards = ( {
	isAdvancedHostingDisabled,
	isBasicHostingDisabled,
	siteId,
	isJetpack,
}: AllCardsProps ) => {
	const allCards: CardEntry[] = [
		{
			feature: 'sftp',
			content: <SftpCard disabled={ isAdvancedHostingDisabled } />,
			type: 'advanced',
		},
		{
			feature: 'phpmyadmin',
			content: <PhpMyAdminCard disabled={ isAdvancedHostingDisabled } />,
			type: 'advanced',
		},
		{
			feature: 'restore-plan-software',
			content: <RestorePlanSoftwareCard />,
			type: 'basic',
		},
		{
			feature: 'cache',
			content: <CacheCard disabled={ isBasicHostingDisabled } />,
			type: 'basic',
		},
	];

	if ( config.isEnabled( 'hosting-server-settings-enhancements' ) ) {
		allCards.push( {
			feature: 'defensive-mode',
			content: <DefensiveModeCard disabled={ isAdvancedHostingDisabled } />,
			type: 'advanced',
		} );
	}

	allCards.push( {
		feature: 'web-server-settings',
		content: <WebServerSettingsCard disabled={ isAdvancedHostingDisabled } />,
		type: 'advanced',
	} );

	if ( isJetpack && siteId ) {
		allCards.push( {
			feature: 'jetpack-monitor',
			content: <JetpackMonitor />,
			type: 'basic',
		} );
	}

	const availableTypes: CardEntry[ 'type' ][] = [];

	if ( ! isAdvancedHostingDisabled ) {
		availableTypes.push( 'advanced' );
	}
	if ( ! isBasicHostingDisabled ) {
		availableTypes.push( 'basic' );
	}

	return <ShowEnabledFeatureCards cards={ allCards } availableTypes={ availableTypes } />;
};

const InnerDiv = ( { children }: { children: React.ReactNode } ) => <>{ children }</>;

const Content = ( {
	hasAtomicFeature,
	hasSftpFeature,
	hasTransfer,
	isBusinessTrial,
	isJetpack,
	isSiteAtomic,
	siteId,
	siteSlug,
}: {
	hasAtomicFeature: boolean;
	hasSftpFeature: boolean;
	hasTransfer: boolean;
	isBusinessTrial: boolean;
	isJetpack: boolean | null;
	isSiteAtomic: boolean;
	siteId: number | null;
	siteSlug: string | null;
} ) => {
	const WrapperComponent = ! isSiteAtomic ? FeatureExample : Fragment;

	return (
		<>
			{ isSiteAtomic && <QuerySites siteId={ siteId } /> }
			{ isJetpack && siteId && <QueryJetpackModules siteId={ siteId } /> }
			<WrapperComponent>
				<InnerDiv>
					<AllCards
						isAdvancedHostingDisabled={ ! hasSftpFeature || ! isSiteAtomic }
						isBasicHostingDisabled={ ! hasAtomicFeature || ! isSiteAtomic }
						isBusinessTrial={ isBusinessTrial && ! hasTransfer }
						siteId={ siteId }
						siteSlug={ siteSlug }
						isJetpack={ isJetpack }
					/>
				</InnerDiv>
			</WrapperComponent>
		</>
	);
};

type ServerSettingsProps = {
	fetchUpdatedData: () => void;
};

const ServerSettings = ( { fetchUpdatedData }: ServerSettingsProps ) => {
	const translate = useTranslate();
	const dispatch = useDispatch();

	const clickActivate = () =>
		dispatch( recordTracksEvent( 'calypso_hosting_configuration_activate_click' ) );

	const siteId = useSelector( getSelectedSiteId );

	const requestingSiteFeatures = useSelector( ( state ) =>
		isRequestingSiteFeatures( state, siteId )
	);
	const hasAtomicFeature = useSelector( ( state ) =>
		siteHasFeature( state, siteId, WPCOM_FEATURES_ATOMIC )
	);
	const hasSftpFeature = useSelector( ( state ) => siteHasFeature( state, siteId, FEATURE_SFTP ) );
	const site = useSelector( getSelectedSite );
	const isEligibleForHostingTrial =
		useSelector( isUserEligibleForFreeHostingTrial ) && site && site.plan?.is_free;
	const isSiteAtomic = useSelector( ( state ) => isSiteWpcomAtomic( state, siteId ) );

	const isJetpack = useSelector( ( state ) => isJetpackSite( state, siteId ) );
	const isECommerceTrial = useSelector( ( state ) => isSiteOnECommerceTrial( state, siteId ) );
	const isBusinessTrial = useSelector( ( state ) => isSiteOnBusinessTrial( state, siteId ) );
	const transferState = useSelector( ( state ) => getAutomatedTransferStatus( state, siteId ) );
	const isLoadingSftpData = useSelector( ( state ) =>
		getAtomicHostingIsLoadingSftpData( state, siteId )
	);
	const siteSlug = useSelector( ( state ) => getSelectedSiteSlug( state ) );
	const isWpcomStagingSite = useSelector( ( state ) => isSiteWpcomStaging( state, siteId ) );

	const [ isTrialAcknowledgeModalOpen, setIsTrialAcknowledgeModalOpen ] = useState( false );
	const [ hasTransfer, setHasTransferring ] = useState(
		!! transferState &&
			transferState !== transferStates.NONE &&
			transferState !== transferStates.INQUIRING &&
			transferState !== transferStates.ERROR &&
			transferState !== transferStates.COMPLETED &&
			transferState !== transferStates.COMPLETE &&
			transferState !== transferStates.REVERTED
	);

	const canSiteGoAtomic = ! isSiteAtomic && hasSftpFeature;
	const showHostingActivationBanner = canSiteGoAtomic && ! hasTransfer;

	const requestUpdatedSiteData = useCallback(
		( isTransferring?: boolean, wasTransferring?: boolean, isTransferCompleted?: boolean ) => {
			if ( isTransferring && ! hasTransfer ) {
				setHasTransferring( true );
			}

			if ( ! isTransferring && wasTransferring && isTransferCompleted ) {
				fetchUpdatedData();
			}
		},
		[ fetchUpdatedData, hasTransfer ]
	);

	const getPageTitle = () => {
		return translate( 'Server settings' );
	};

	const getUpgradeBanner = () => {
		if ( hasTransfer ) {
			return null;
		}
		// The eCommerce Trial requires a different upsell path.
		const targetPlan = ! isECommerceTrial
			? undefined
			: {
					callToAction: translate( 'Upgrade your plan' ),
					feature: FEATURE_SFTP_DATABASE,
					href: `/plans/${ siteSlug }?feature=${ encodeURIComponent( FEATURE_SFTP_DATABASE ) }`,
					title: translate( 'Upgrade your plan to access all hosting features' ),
			  };
		return <HostingUpsellNudge siteId={ siteId } targetPlan={ targetPlan } />;
	};

	const getAtomicActivationNotice = () => {
		if ( showHostingActivationBanner ) {
			return (
				<Notice
					className="hosting__activating-notice"
					status="is-info"
					showDismiss={ false }
					text={ translate( 'Please activate the hosting access to begin using these features.' ) }
					icon="globe"
				>
					<TrackComponentView eventName="calypso_hosting_configuration_activate_impression" />
					<NoticeAction onClick={ clickActivate } href={ `/hosting-config/activate/${ siteSlug }` }>
						{ translate( 'Activate' ) }
					</NoticeAction>
				</Notice>
			);
		}
	};

	/* We want to show the upsell banner for the following cases:
	 * 1. The site is on an eCommerce trial.
	 * 2. The site does not have the Atomic feature.
	 * 3. The site is Atomic, is not transferring, and doesn't have advanced hosting features.
	 * Otherwise, we show the activation notice, which may be empty.
	 */
	const shouldShowUpgradeBanner =
		( ! isLoadingSftpData || isECommerceTrial ) &&
		( ! hasAtomicFeature || ( ! hasTransfer && ! hasSftpFeature && ! isWpcomStagingSite ) );
	const banner = shouldShowUpgradeBanner ? getUpgradeBanner() : getAtomicActivationNotice();

	if ( requestingSiteFeatures ) {
		return null;
	}

	return (
		<Panel wide={ false } className="page-server-settings">
			{ ! isLoadingSftpData && (
				<ScrollToAnchorOnMount
					offset={ HEADING_OFFSET }
					timeout={ 250 }
					container={
						document.querySelector< HTMLElement >( '.hosting-dashboard-item-view__content' ) ??
						undefined
					}
				/>
			) }
			<PageViewTracker path="/hosting-config/:site" title="Hosting" />
			<DocumentHead title={ getPageTitle() } />
			<NavigationHeader
				navigationItems={ [] }
				title={ getPageTitle() }
				subtitle={ translate( 'Access your websites database and more advanced settings.' ) }
			/>
			{ ! showHostingActivationBanner && ! isTrialAcknowledgeModalOpen && (
				<HostingActivateStatus
					context="hosting"
					onTick={ requestUpdatedSiteData }
					keepAlive={ ! isSiteAtomic && hasTransfer }
				/>
			) }
			{ ! isBusinessTrial && banner }
			{ isBusinessTrial && ( ! hasTransfer || isSiteAtomic ) && (
				<TrialBanner
					callToAction={
						<Button primary href={ `/plans/${ siteSlug }` }>
							{ translate( 'Upgrade plan' ) }
						</Button>
					}
				/>
			) }
			<Content
				hasAtomicFeature={ hasAtomicFeature }
				hasSftpFeature={ hasSftpFeature }
				hasTransfer={ hasTransfer }
				isBusinessTrial={ isBusinessTrial }
				isJetpack={ isJetpack }
				isSiteAtomic={ isSiteAtomic }
				siteId={ siteId }
				siteSlug={ siteSlug }
			/>
			{ isEligibleForHostingTrial && isTrialAcknowledgeModalOpen && (
				<TrialAcknowledgeModal
					setOpenModal={ ( isOpen ) => {
						setIsTrialAcknowledgeModalOpen( isOpen );
					} }
					trialRequested={ () => {
						setHasTransferring( true );
					} }
				/>
			) }
			<QueryReaderTeams />
		</Panel>
	);
};

export default WithOnclickTrialRequest( ServerSettings );