File size: 7,978 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
import { safeImageUrl } from '@automattic/calypso-url';
import { CompactCard } from '@automattic/components';
import { Icon, globe } from '@wordpress/icons';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import Site from 'calypso/blocks/site';
import FormattedHeader from 'calypso/components/formatted-header';
import { decodeEntities } from 'calypso/lib/formatting';
import { getPluginTitle } from 'calypso/lib/login';
import { login } from 'calypso/lib/paths';
import versionCompare from 'calypso/lib/version-compare';
import { getCurrentUser } from 'calypso/state/current-user/selectors';
import { getAuthorizationData } from 'calypso/state/jetpack-connect/selectors';
import getPartnerSlugFromQuery from 'calypso/state/selectors/get-partner-slug-from-query';
import { authQueryPropTypes } from './utils';

export class AuthFormHeader extends Component {
	static propTypes = {
		authQuery: authQueryPropTypes.isRequired,
		isWooJPC: PropTypes.bool,
		wooDnaConfig: PropTypes.object,
		isFromAutomatticForAgenciesPlugin: PropTypes.bool,

		// Connected props
		translate: PropTypes.func.isRequired,
		user: PropTypes.object,
		disableSiteCard: PropTypes.bool,
	};

	getState() {
		const { user, authorize, partnerSlug } = this.props;

		if ( partnerSlug ) {
			return 'partner';
		}

		if ( ! user ) {
			return 'logged-out';
		}

		if ( authorize.authorizeSuccess ) {
			return 'logged-in-success';
		}

		if ( authorize.isAuthorizing ) {
			return 'auth-in-progress';
		}

		return 'logged-in';
	}

	getHeaderText() {
		const { translate, partnerSlug, isWooJPC, wooDnaConfig, isFromAutomatticForAgenciesPlugin } =
			this.props;

		if ( wooDnaConfig && wooDnaConfig.isWooDnaFlow() ) {
			return wooDnaConfig.getServiceName();
		}

		let host = '';
		switch ( partnerSlug ) {
			case 'dreamhost':
				host = 'Dreamhost';
				break;
			case 'pressable':
				host = 'Pressable';
				break;
			case 'milesweb':
				host = 'Milesweb';
				break;
			case 'bluehost':
				host = 'Bluehost';
				break;
			case 'eurodns':
				host = 'EuroDNS';
				break;
		}

		if ( host && ! isWooJPC ) {
			return translate( 'Jetpack, in partnership with %(host)s', {
				args: { host },
				comment: '%(host)s is the company name of a hosting partner. Ex. - Pressable',
			} );
		}

		const currentState = this.getState();

		if ( isWooJPC ) {
			switch ( currentState ) {
				case 'logged-out':
					return translate( 'Create an account' );
				default:
					return translate( 'Connect your account' );
			}
		}

		if ( isFromAutomatticForAgenciesPlugin ) {
			switch ( currentState ) {
				case 'logged-out':
					/** @todo redirect to landing page when user is not signed up for A4A. */
					return translate( 'Create an account to set up Automattic for Agencies' );
				case 'logged-in-success':
					return translate( "You're all set!" );
				case 'auth-in-progress':
					return translate( 'Connecting your site' );
				case 'logged-in':
				default:
					return translate( 'Finish connecting your site' );
			}
		}

		switch ( currentState ) {
			case 'logged-out':
				return translate( 'Create an account to set up Jetpack' );
			case 'logged-in-success':
				return translate( "You're all set!" );
			case 'logged-in':
			default:
				return translate( 'Completing setup' );
		}
	}

	getSubHeaderText() {
		const { translate, isWooJPC, wooDnaConfig, isFromAutomatticForAgenciesPlugin } = this.props;
		const currentState = this.getState();

		if ( isWooJPC ) {
			const pluginName = getPluginTitle( this.props.authQuery?.plugin_name, translate );
			const reviewDocLink = (
				<a
					href="https://woocommerce.com/document/connect-your-store-to-a-wordpress-com-account/"
					target="_blank"
					rel="noreferrer"
				/>
			);
			const translateParams = {
				components: {
					br: <br />,
					a: (
						<a
							href={ login( {
								isJetpack: true,
								redirectTo: window.location.href,
								from: this.props.authQuery.from,
								plugin_name: this.props.authQuery.plugin_name,
							} ) }
						/>
					),
				},
				args: { pluginName },
				comment:
					'Link displayed on the Jetpack Connect signup page for users to log in with a WordPress.com account',
			};

			switch ( currentState ) {
				case 'logged-out':
					return translate(
						'To access all of the features and functionality %(pluginName)s, you’ll first need to connect your store to a WordPress.com account. Please create one now, or {{a}}log in{{/a}}. For more information, please {{doc}}review our documentation{{/doc}}.',
						{
							...translateParams,
							components: {
								...translateParams.components,
								doc: reviewDocLink,
							},
						}
					);
				default:
					return translate(
						'To access all of the features and functionality %(pluginName)s, you’ll first need to connect your store to a WordPress.com account. For more information, please {{doc}}review our documentation{{/doc}}.',
						{
							args: { pluginName },
							components: {
								doc: reviewDocLink,
							},
						}
					);
			}
		}

		if ( wooDnaConfig && wooDnaConfig.isWooDnaFlow() ) {
			switch ( currentState ) {
				case 'logged-in-success':
				case 'auth-in-progress':
					return translate( 'Connecting your store' );
				default:
					if ( wooDnaConfig.getFlowName() === 'woodna:woocommerce-payments' ) {
						return translate(
							'Approve your connection. Your account will enable you to start using the features and benefits offered by WooPayments'
						);
					} else if ( wooDnaConfig.getFlowName() === 'woodna:blaze-ads-on-woo' ) {
						const pluginName = wooDnaConfig.getServiceName();
						/* translators: pluginName is the name of the Woo extension that initiated the connection flow */
						return translate(
							'Approve your connection. Your account will enable you to start using the features and benefits offered by %(pluginName)s.',
							{
								args: {
									pluginName,
								},
							}
						);
					}
					return translate( 'Approve your connection' );
			}
		}

		if ( isFromAutomatticForAgenciesPlugin ) {
			return undefined;
		}

		switch ( currentState ) {
			case 'logged-out':
				return translate( 'You are moments away from a better WordPress.' );
			case 'logged-in-success':
				return translate( 'Thank you for flying with Jetpack' );
			case 'partner':
				return translate( 'Your new plan requires a connection to WordPress.com' );
			case 'logged-in':
			default:
				return undefined;
		}
	}

	getSiteCard() {
		const { isWooJPC } = this.props;
		const { jpVersion } = this.props.authQuery;
		if (
			// Always show the site card for Woo Core Profiler
			! isWooJPC &&
			! versionCompare( jpVersion, '4.0.3', '>' )
		) {
			return null;
		}

		const { blogname, homeUrl, siteIcon, siteUrl } = this.props.authQuery;
		const safeIconUrl = siteIcon ? safeImageUrl( siteIcon ) : false;
		const icon = safeIconUrl ? { img: safeIconUrl } : false;
		const url = decodeEntities( homeUrl );
		const parsedUrl = new URL( url );
		const path = parsedUrl.pathname === '/' ? '' : parsedUrl.pathname;
		const site = {
			admin_url: decodeEntities( siteUrl + '/wp-admin' ),
			domain: parsedUrl.host + path,
			icon,
			ID: null,
			is_vip: false,
			title: decodeEntities( blogname ),
			url: url,
		};

		return (
			<CompactCard className="jetpack-connect__site">
				<Site site={ site } defaultIcon={ isWooJPC ? <Icon icon={ globe } /> : null } />
			</CompactCard>
		);
	}

	render() {
		return (
			<div>
				<FormattedHeader
					headerText={ this.getHeaderText() }
					subHeaderText={ this.getSubHeaderText() }
				/>
				{ ! this.props.disableSiteCard && this.getSiteCard() }
			</div>
		);
	}
}

export default connect( ( state ) => {
	return {
		authorize: getAuthorizationData( state ),
		user: getCurrentUser( state ),
		partnerSlug: getPartnerSlugFromQuery( state ),
	};
} )( localize( AuthFormHeader ) );