File size: 6,892 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
import { WordPressWordmark, WordPressLogo } from '@automattic/components';
import {
	isDefaultLocale,
	addLocaleToPath,
	addLocaleToPathLocaleInFront,
} from '@automattic/i18n-utils';
import { getLocaleSlug, localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';
import AsyncLoad from 'calypso/components/async-load';
import { withCurrentRoute } from 'calypso/components/route';
import { isDomainConnectAuthorizePath } from 'calypso/lib/domains/utils';
import { login } from 'calypso/lib/paths';
import { addQueryArgs } from 'calypso/lib/route';
import Item from './item';
import Masterbar from './masterbar';

class MasterbarLoggedOut extends Component {
	static propTypes = {
		redirectUri: PropTypes.string,
		sectionName: PropTypes.string,
		title: PropTypes.string,
		isCheckout: PropTypes.bool,
		isCheckoutPending: PropTypes.bool,
		isCheckoutFailed: PropTypes.bool,

		// Connected props
		currentQuery: PropTypes.oneOfType( [ PropTypes.bool, PropTypes.object ] ),
		currentRoute: PropTypes.string,
		userSiteCount: PropTypes.number,
	};

	static defaultProps = {
		sectionName: '',
		title: '',
	};

	renderTagsItem() {
		const { translate } = this.props;
		const tagsUrl = addLocaleToPathLocaleInFront( '/tags' );

		return (
			<Item url={ tagsUrl }>
				{ translate( 'Popular Tags', {
					context: 'Toolbar',
					comment: 'Should be shorter than ~15 chars',
				} ) }
			</Item>
		);
	}

	renderSearchItem() {
		const { translate } = this.props;
		const searchUrl = addLocaleToPathLocaleInFront( '/reader/search' );

		return (
			<Item url={ searchUrl }>
				{ translate( 'Search', {
					context: 'Toolbar',
					comment: 'Should be shorter than ~12 chars',
				} ) }
			</Item>
		);
	}

	renderDiscoverItem() {
		const { translate } = this.props;
		const discoverUrl = addLocaleToPathLocaleInFront( '/discover' );

		return (
			<Item url={ discoverUrl }>
				{ translate( 'Discover', {
					context: 'Toolbar',
					comment: 'Should be shorter than ~12 chars',
				} ) }
			</Item>
		);
	}

	renderLoginItem() {
		const { currentQuery, currentRoute, sectionName, translate, redirectUri } = this.props;
		if ( sectionName === 'login' ) {
			return null;
		}

		let redirectTo = null;
		if ( redirectUri ) {
			redirectTo = redirectUri;
		} else if ( currentRoute ) {
			redirectTo = currentQuery ? addQueryArgs( currentQuery, currentRoute ) : currentRoute;
		}

		const isJetpack = 'jetpack-connect' === sectionName;

		let loginUrl = login( {
			// We may know the email from Jetpack connection details
			emailAddress: isJetpack && ( currentQuery?.user_email ?? false ),
			isJetpack,
			locale: getLocaleSlug(),
			redirectTo,
		} );

		if ( currentQuery?.partner_id ) {
			loginUrl = addQueryArgs( { partner_id: currentQuery.partner_id }, loginUrl );
		}

		return (
			<Item url={ loginUrl }>
				{ translate( 'Log In', {
					context: 'Toolbar',
					comment: 'Should be shorter than ~12 chars',
				} ) }
			</Item>
		);
	}

	renderSignupItem() {
		const { currentQuery, currentRoute, locale, sectionName, translate } = this.props;

		// Hide for some sections
		if ( sectionName === 'signup' ) {
			return null;
		}

		/**
		 * Hide signup from Jetpack connect authorization step. This step handles signup as part of
		 * the flow.
		 */
		if ( currentRoute.startsWith( '/jetpack/connect/authorize' ) ) {
			return null;
		}

		/**
		 * Hide signup for OAuth flow (it doesn't correctly route the arugments)
		 */
		if ( currentQuery?.client_id !== null && currentQuery?.redirect_to != null ) {
			return null;
		}

		/**
		 * Hide signup from the screen when we have been sent to the login page from a redirect
		 * by a service provider to authorize a Domain Connect template application.
		 */
		const redirectTo = currentQuery?.redirect_to ?? '';
		if ( isDomainConnectAuthorizePath( redirectTo ) ) {
			return null;
		}

		let signupUrl = '/start';
		const signupFlow = currentQuery?.signup_flow;
		if (
			// Match locales like `/log-in/jetpack/es`
			currentRoute.startsWith( '/log-in/jetpack' )
		) {
			// Basic validation that we're in a valid Jetpack Authorization flow
			if (
				currentQuery?.redirect_to?.includes( '/jetpack/connect/authorize' ) &&
				currentQuery?.redirect_to?.includes( '_wp_nonce' )
			) {
				/**
				 * `log-in/jetpack/:locale` is reached as part of the Jetpack connection flow. In
				 * this case, the redirect_to will handle signups as part of the flow. Use the
				 * `redirect_to` parameter directly for signup.
				 */
				signupUrl = currentQuery.redirect_to;
			} else {
				signupUrl = '/jetpack/connect';
			}
		} else if ( 'jetpack-connect' === sectionName ) {
			signupUrl = '/jetpack/connect';
		} else if ( signupFlow ) {
			signupUrl += '/' + signupFlow;
		}

		if ( ! isDefaultLocale( locale ) ) {
			signupUrl = addLocaleToPath( signupUrl, locale );
		}

		// Use Reader-specific signup stepper
		// and add referrer query parameter for tracking
		if ( sectionName === 'reader' ) {
			signupUrl += '/reader';
			signupUrl = addQueryArgs( { ref: 'reader-lp' }, signupUrl );
		}

		return (
			<Item url={ signupUrl }>
				{ translate( 'Sign Up', {
					context: 'Toolbar',
					comment: 'Should be shorter than ~12 chars',
				} ) }
			</Item>
		);
	}

	renderWordPressItem() {
		const { locale, translate } = this.props;

		let homeUrl = '/';
		if ( ! isDefaultLocale( locale ) ) {
			homeUrl = addLocaleToPath( homeUrl, locale );
		}

		return (
			<Item
				url={ homeUrl }
				className="masterbar__item-logo masterbar__item--always-show-content"
				ariaLabel={ translate( 'WordPress.com logo' ) }
			>
				<WordPressLogo className="masterbar__wpcom-logo" />
				<WordPressWordmark className="masterbar__wpcom-wordmark" />
			</Item>
		);
	}

	render() {
		const { title, isCheckout, isCheckoutPending, isCheckoutFailed, sectionName } = this.props;

		if ( isCheckout || isCheckoutPending || isCheckoutFailed ) {
			return (
				<AsyncLoad
					require="calypso/layout/masterbar/checkout.tsx"
					placeholder={ null }
					title={ title }
					isLeavingAllowed={ ! isCheckoutPending }
					shouldClearCartWhenLeaving={ ! isCheckoutFailed }
				/>
			);
		}

		return (
			<Masterbar className="masterbar__loggedout">
				{ this.renderWordPressItem() }
				{ title && <Item className="masterbar__item-title">{ title }</Item> }
				{ sectionName === 'reader' && (
					<div className="masterbar__login-links">
						{ this.renderDiscoverItem() }
						{ this.renderTagsItem() }
						{ this.renderSearchItem() }
						{ this.renderLoginItem() }
						{ this.renderSignupItem() }
					</div>
				) }
				{ sectionName !== 'reader' && (
					<div className="masterbar__login-links">
						{ this.renderLoginItem() }
						{ this.renderSignupItem() }
					</div>
				) }
			</Masterbar>
		);
	}
}

export default withCurrentRoute( localize( MasterbarLoggedOut ) );