-
Notifications
You must be signed in to change notification settings - Fork 2k
/
index.tsx
304 lines (284 loc) · 11.2 KB
/
index.tsx
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
import { isBusiness, isEcommerce, isEnterprise } from '@automattic/calypso-products';
import { ThemeProvider } from '@emotion/react';
import { useTranslate } from 'i18n-calypso';
import page from 'page';
import { useEffect, useState, useRef } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import QueryJetpackPlugins from 'calypso/components/data/query-jetpack-plugins';
import EmptyContent from 'calypso/components/empty-content';
import WordPressWordmark from 'calypso/components/wordpress-wordmark';
import Item from 'calypso/layout/masterbar/item';
import Masterbar from 'calypso/layout/masterbar/masterbar';
import PageViewTracker from 'calypso/lib/analytics/page-view-tracker';
import MarketplaceProgressBar from 'calypso/my-sites/marketplace/components/progressbar';
import theme from 'calypso/my-sites/marketplace/theme';
import { waitFor } from 'calypso/my-sites/marketplace/util';
import { transferStates } from 'calypso/state/automated-transfer/constants';
import { getAutomatedTransferStatus } from 'calypso/state/automated-transfer/selectors';
import { getPurchaseFlowState } from 'calypso/state/marketplace/purchase-flow/selectors';
import { MARKETPLACE_ASYNC_PROCESS_STATUS } from 'calypso/state/marketplace/types';
import { installPlugin, activatePlugin } from 'calypso/state/plugins/installed/actions';
import { getPluginOnSite, getStatusForPlugin } from 'calypso/state/plugins/installed/selectors';
import { fetchPluginData as wporgFetchPluginData } from 'calypso/state/plugins/wporg/actions';
import { getPlugin, isFetched } from 'calypso/state/plugins/wporg/selectors';
import getPluginUploadError from 'calypso/state/selectors/get-plugin-upload-error';
import getPluginUploadProgress from 'calypso/state/selectors/get-plugin-upload-progress';
import getUploadedPluginId from 'calypso/state/selectors/get-uploaded-plugin-id';
import isPluginActive from 'calypso/state/selectors/is-plugin-active';
import isPluginUploadComplete from 'calypso/state/selectors/is-plugin-upload-complete';
import isSiteAutomatedTransfer from 'calypso/state/selectors/is-site-automated-transfer';
import { isJetpackSite } from 'calypso/state/sites/selectors';
import { initiateThemeTransfer as initiateTransfer } from 'calypso/state/themes/actions';
import {
getSelectedSite,
getSelectedSiteId,
getSelectedSiteSlug,
} from 'calypso/state/ui/selectors';
import './style.scss';
import { MarketplacePluginInstallProps } from './types';
const MarketplacePluginInstall = ( {
productSlug,
}: MarketplacePluginInstallProps ): JSX.Element => {
const isUploadFlow = ! productSlug;
const [ currentStep, setCurrentStep ] = useState( 0 );
const [ initializeInstallFlow, setInitializeInstallFlow ] = useState( false );
const [ atomicFlow, setAtomicFlow ] = useState( false );
const [ nonInstallablePlanError, setNonInstallablePlanError ] = useState( false );
const [ noDirectAccessError, setNoDirectAccessError ] = useState( false );
const translate = useTranslate();
const dispatch = useDispatch();
const selectedSiteSlug = useSelector( getSelectedSiteSlug );
const selectedSite = useSelector( getSelectedSite );
const siteId = useSelector( getSelectedSiteId ) as number;
const pluginUploadProgress = useSelector( ( state ) => getPluginUploadProgress( state, siteId ) );
const pluginUploadError = useSelector( ( state ) => getPluginUploadError( state, siteId ) );
const wporgPlugin = useSelector( ( state ) => getPlugin( state, productSlug ) );
const isWporgPluginFetched = useSelector( ( state ) => isFetched( state, productSlug ) );
const uploadedPluginSlug = useSelector( ( state ) =>
getUploadedPluginId( state, siteId )
) as string;
const pluginUploadComplete = useSelector( ( state ) => isPluginUploadComplete( state, siteId ) );
const installedPlugin = useSelector( ( state ) =>
getPluginOnSite( state, siteId, isUploadFlow ? uploadedPluginSlug : productSlug )
);
const pluginActive = useSelector( ( state ) =>
isPluginActive( state, siteId, isUploadFlow ? uploadedPluginSlug : productSlug )
);
const automatedTransferStatus = useSelector( ( state ) =>
getAutomatedTransferStatus( state, siteId )
);
const pluginInstallStatus = useSelector( ( state ) =>
getStatusForPlugin( state, siteId, productSlug )
);
const marketplacePluginInstallationInProgress = useSelector( ( state ) => {
const { pluginInstallationStatus, productSlugInstalled, primaryDomain } = getPurchaseFlowState(
state
);
if ( isUploadFlow ) {
return (
pluginInstallationStatus !== MARKETPLACE_ASYNC_PROCESS_STATUS.COMPLETED &&
primaryDomain === selectedSiteSlug
);
}
return (
pluginInstallationStatus !== MARKETPLACE_ASYNC_PROCESS_STATUS.COMPLETED &&
productSlugInstalled === productSlug &&
primaryDomain === selectedSiteSlug
);
} );
const isJetpack = useSelector( ( state ) => isJetpackSite( state, selectedSite?.ID ?? null ) );
const isAtomic = useSelector( ( state ) =>
isSiteAutomatedTransfer( state, selectedSite?.ID ?? null )
);
const isJetpackSelfHosted = selectedSite && isJetpack && ! isAtomic;
const supportsAtomicUpgrade = useRef< boolean >();
useEffect( () => {
supportsAtomicUpgrade.current =
selectedSite?.plan &&
( isBusiness( selectedSite.plan ) ||
isEnterprise( selectedSite.plan ) ||
isEcommerce( selectedSite.plan ) );
}, [ selectedSite ] );
// retrieve plugin data if not available
useEffect( () => {
if ( ! isWporgPluginFetched ) {
dispatch( wporgFetchPluginData( productSlug ) );
}
}, [ isWporgPluginFetched, productSlug ] );
// Check if the user plan is enough for installation or it is a self-hosted jetpack site
// if not, check again in 2s and show an error message
useEffect( () => {
if ( ! supportsAtomicUpgrade.current && ! isJetpackSelfHosted ) {
waitFor( 2 ).then( () => {
if ( ! supportsAtomicUpgrade.current && ! isJetpackSelfHosted ) {
setNonInstallablePlanError( true );
}
} );
}
} );
// Check that the site URL and the plugin slug are the same which were selected on the plugin page
useEffect( () => {
if ( ! marketplacePluginInstallationInProgress ) {
waitFor( 2 ).then( () => {
! marketplacePluginInstallationInProgress && setNoDirectAccessError( true );
} );
}
}, [ marketplacePluginInstallationInProgress ] );
// Upload flow startup
useEffect( () => {
if ( 100 === pluginUploadProgress ) {
// For smaller uploads or fast networks give
// the chance to Upload Plugin step to be shown
// before moving to next step.
waitFor( 1 ).then( () => setCurrentStep( 1 ) );
}
}, [ pluginUploadProgress, setCurrentStep ] );
// Installing plugin flow startup
useEffect( () => {
if (
marketplacePluginInstallationInProgress &&
! isUploadFlow &&
! initializeInstallFlow &&
wporgPlugin &&
selectedSite
) {
const triggerInstallFlow = () => {
setInitializeInstallFlow( true );
waitFor( 1 ).then( () => setCurrentStep( 1 ) );
};
if ( selectedSite.jetpack ) {
// initialize plugin installing
dispatch( installPlugin( siteId, wporgPlugin, false ) );
triggerInstallFlow();
} else if ( supportsAtomicUpgrade.current ) {
// initialize atomic flow
setAtomicFlow( true );
dispatch( initiateTransfer( siteId, null, productSlug ) );
triggerInstallFlow();
}
}
}, [
marketplacePluginInstallationInProgress,
isUploadFlow,
initializeInstallFlow,
selectedSite,
siteId,
wporgPlugin,
productSlug,
] );
// Validate completition of atomic transfer flow
useEffect( () => {
if ( atomicFlow && currentStep === 1 && transferStates.COMPLETE === automatedTransferStatus ) {
setCurrentStep( 2 );
}
}, [ atomicFlow, automatedTransferStatus ] );
// Validate plugin is already installed and activate
useEffect( () => {
if (
installedPlugin &&
currentStep === 1 &&
( ! isUploadFlow || ( isUploadFlow && pluginUploadComplete ) )
) {
dispatch(
activatePlugin( siteId, {
slug: installedPlugin?.slug,
id: installedPlugin?.id,
} )
);
setCurrentStep( 2 );
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ pluginUploadComplete, installedPlugin, setCurrentStep ] );
// Check completition of all flows and redirect to thank you page
useEffect( () => {
if (
( installedPlugin && pluginActive ) ||
( atomicFlow && transferStates.COMPLETE === automatedTransferStatus )
) {
waitFor( 1 ).then( () =>
page.redirect(
`/marketplace/thank-you/${ installedPlugin?.slug || productSlug }/${ selectedSiteSlug }`
)
);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ pluginActive, automatedTransferStatus ] ); // We need to trigger this hook also when `automatedTransferStatus` changes cause the plugin install is done on the background in that case.
const steps = [
isUploadFlow ? translate( 'Uploading plugin' ) : translate( 'Setting up plugin installation' ),
translate( 'Installing plugin' ),
translate( 'Activating plugin' ),
];
const renderError = () => {
if ( isUploadFlow && noDirectAccessError ) {
return (
<EmptyContent
illustration="/calypso/images/illustrations/error.svg"
title={ translate(
'This URL should not be accessed directly. Please try to upload the plugin again.'
) }
action={ translate( 'Go to the upload page' ) }
actionURL={ `/plugins/upload/${ selectedSite?.slug }` }
/>
);
}
if ( noDirectAccessError ) {
return (
<EmptyContent
illustration="/calypso/images/illustrations/error.svg"
title={ translate(
'This URL should not be accessed directly. Please click the Install button on the plugin page.'
) }
action={ translate( 'Go to the plugin page' ) }
actionURL={ `/plugins/${ productSlug }/${ selectedSite?.slug }` }
/>
);
}
if ( nonInstallablePlanError ) {
return (
<EmptyContent
illustration="/calypso/images/illustrations/error.svg"
title={ translate(
"Your current plan doesn't allow plugin installation. Please upgrade to Business plan first."
) }
action={ translate( 'Upgrade to Business Plan' ) }
actionURL={ `/checkout/${ selectedSite?.slug }/business?redirect_to=/marketplace/${ productSlug }/install/${ selectedSite?.slug }#step2` }
/>
);
}
if (
pluginUploadError ||
pluginInstallStatus.error ||
( atomicFlow && automatedTransferStatus === transferStates.FAILURE )
) {
return (
<EmptyContent
illustration="/calypso/images/illustrations/error.svg"
title={ translate( 'An error occurred while installing the plugin.' ) }
action={ translate( 'Back' ) }
actionURL={
isUploadFlow
? `/plugins/upload/${ selectedSiteSlug }`
: `/plugins/${ productSlug }/${ selectedSiteSlug }`
}
/>
);
}
};
return (
<ThemeProvider theme={ theme }>
<PageViewTracker
path="/marketplace/:productSlug?/install/:site?"
title="Plugins > Installing"
/>
{ siteId && <QueryJetpackPlugins siteIds={ [ siteId ] } /> }
<Masterbar>
<WordPressWordmark className="marketplace-plugin-upload-status__wpcom-wordmark" />
<Item>{ translate( 'Plugin Installation' ) }</Item>
</Masterbar>
<div className="marketplace-plugin-upload-status__root">
{ renderError() || <MarketplaceProgressBar steps={ steps } currentStep={ currentStep } /> }
</div>
</ThemeProvider>
);
};
export default MarketplacePluginInstall;