File size: 9,545 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
import {
	PLAN_PREMIUM,
	WPCOM_FEATURES_PREMIUM_THEMES_UNLIMITED,
} from '@automattic/calypso-products';
import { PremiumBadge } from '@automattic/components';
import { isBlankCanvasDesign, useThemeDesignsQuery } from '@automattic/design-picker';
import { englishLocales } from '@automattic/i18n-utils';
import { shuffle } from '@automattic/js-utils';
import { addQueryArgs } from '@wordpress/url';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { useEffect, useLayoutEffect, useMemo, useRef } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import FormattedHeader from 'calypso/components/formatted-header';
import { THEME_TIER_PARTNER, THEME_TIER_PREMIUM } from 'calypso/components/theme-tier/constants';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { triggerGuidesForStep } from 'calypso/lib/guides/trigger-guides-for-step';
import StepWrapper from 'calypso/signup/step-wrapper';
import siteHasFeature from 'calypso/state/selectors/site-has-feature';
import { saveSignupStep, submitSignupStep } from 'calypso/state/signup/progress/actions';
import { getSiteId } from 'calypso/state/sites/selectors';
import DesignPicker from './design-picker';
import LetUsChoose from './let-us-choose';
import { useCategorization } from './use-categorization';
import './style.scss';

export default function DesignPickerStep( props ) {
	const {
		flowName,
		stepName,
		showDesignPickerCategories,
		showLetUsChoose,
		hideFullScreenPreview,
		hideDesignTitle,
		hideDescription,
		hideBadge,
		useDIFMThemes,
		signupDependencies: dependencies,
	} = props;

	const siteId = useSelector( ( state ) => getSiteId( state, dependencies.siteSlug ) );
	const isPremiumThemeAvailable = useSelector( ( state ) =>
		siteHasFeature( state, siteId, WPCOM_FEATURES_PREMIUM_THEMES_UNLIMITED )
	);

	const dispatch = useDispatch();
	const translate = useTranslate();

	const scrollTop = useRef( 0 );

	const getThemeFilters = () => {
		if ( useDIFMThemes ) {
			const isDIFMStoreFlow = 'do-it-for-me-store' === props.flowName;
			return isDIFMStoreFlow ? 'do-it-for-me-store' : 'do-it-for-me';
		}

		return 'auto-loading-homepage,full-site-editing';
	};

	const { data: apiThemes = [] } = useThemeDesignsQuery( {
		filter: getThemeFilters(),
		tier: 'all',
	} );

	useEffect(
		() => {
			dispatch( saveSignupStep( { stepName: stepName } ) );
			triggerGuidesForStep( flowName, stepName );
		},
		// Ignoring dependencies because we only want to save the step on first mount
		// eslint-disable-next-line react-hooks/exhaustive-deps
		[]
	);

	// Update Scroll position when section changes
	useLayoutEffect( () => {
		let timeoutID;
		if ( props.stepSectionName ) {
			scrollTop.current = document.scrollingElement.scrollTop;
		} else {
			// Defer restore scroll position to ensure DesignPicker is rendered
			timeoutID = window.setTimeout( () => {
				document.scrollingElement.scrollTop = scrollTop.current;
			} );
		}

		return () => {
			timeoutID && window.clearTimeout( timeoutID );
		};
	}, [ props.stepSectionName ] );

	const designs = useMemo( () => {
		const filteredThemes = apiThemes.filter( ( theme ) => ! isBlankCanvasDesign( theme ) );
		if ( useDIFMThemes ) {
			return filteredThemes;
		}
		return shuffle( filteredThemes );
	}, [ apiThemes, useDIFMThemes ] );

	const getEventPropsByDesign = ( design ) => ( {
		theme: design?.stylesheet ?? `pub/${ design?.theme }`,
		template: design?.template,
		tier: design?.design_tier,
		is_premium: design?.design_tier === THEME_TIER_PREMIUM,
		is_externally_managed: design?.design_tier === THEME_TIER_PARTNER,
		flow: flowName,
		intent: dependencies.intent,
	} );

	const getCategorizationOptionsForStep = () => {
		const result = {
			showAllFilter: props.showDesignPickerCategoriesAllFilter,
		};
		const intent = props.signupDependencies.intent;
		switch ( intent ) {
			case 'write':
				result.defaultSelection = 'blog';
				result.sort = sortBlogToTop;
				break;
			case 'sell':
				result.defaultSelection = 'store';
				result.sort = sortStoreToTop;
				break;
			default:
				result.defaultSelection = null;
				result.sort = sortBlogToTop;
				break;
		}

		return result;
	};
	const categorization = useCategorization( designs, getCategorizationOptionsForStep() );

	function pickDesign( _selectedDesign, additionalDependencies = {} ) {
		// Design picker preview will submit the defaultDependencies via next button,
		// So only do this when the user picks the design directly
		dispatch(
			submitSignupStep(
				{
					stepName: props.stepName,
				},
				{
					selectedDesign: _selectedDesign,
					selectedSiteCategory: categorization.selection,
					...additionalDependencies,
				}
			)
		);

		submitDesign( _selectedDesign );
	}

	function upgradePlan() {
		const relativeCurrentPath = window.location.href.replace( window.location.origin, '' );
		const checkoutUrl = addQueryArgs( `/checkout/${ siteId }/${ PLAN_PREMIUM }`, {
			redirect_to: relativeCurrentPath,
			cancel_to: relativeCurrentPath,
			signup: '1',
		} );

		window.location.href = checkoutUrl;
	}

	function upgradePlanFromDesignPicker( design ) {
		recordTracksEvent(
			'calypso_signup_design_upgrade_button_click',
			getEventPropsByDesign( design )
		);
		upgradePlan();
	}

	function submitDesign( _selectedDesign ) {
		recordTracksEvent( 'calypso_signup_select_design', getEventPropsByDesign( _selectedDesign ) );
		props.goToNextStep();
	}

	function renderDesignPicker() {
		return (
			<>
				<DesignPicker
					designs={ designs }
					theme="light"
					locale={ translate.localeSlug }
					onSelect={ pickDesign }
					onUpgrade={ upgradePlanFromDesignPicker }
					className={ clsx( {
						'design-picker-step__has-categories': showDesignPickerCategories,
					} ) }
					highResThumbnails
					premiumBadge={
						props.useDIFMThemes ? null : (
							<PremiumBadge isPremiumThemeAvailable={ isPremiumThemeAvailable } />
						)
					}
					categorization={ showDesignPickerCategories ? categorization : undefined }
					recommendedCategorySlug={ getCategorizationOptionsForStep().defaultSelection }
					categoriesHeading={
						<FormattedHeader
							id="step-header"
							headerText={ headerText() }
							subHeaderText={ subHeaderText() }
							align="left"
						/>
					}
					categoriesFooter={ renderCategoriesFooter() }
					hideFullScreenPreview={ hideFullScreenPreview }
					hideDesignTitle={ hideDesignTitle }
					hideDescription={ hideDescription }
					hideBadge={ hideBadge }
					isPremiumThemeAvailable={ isPremiumThemeAvailable }
				/>
			</>
		);
	}

	function renderCategoriesFooter() {
		return (
			<>
				{ showLetUsChoose && (
					<LetUsChoose flowName={ props.flowName } designs={ designs } onSelect={ pickDesign } />
				) }
			</>
		);
	}

	function headerText() {
		if ( useDIFMThemes ) {
			return translate( 'Design' );
		}

		if ( showDesignPickerCategories ) {
			return translate( 'Themes' );
		}

		return translate( 'Choose a design' );
	}

	function subHeaderText() {
		if ( ! showDesignPickerCategories ) {
			return translate(
				'Pick your favorite homepage layout. You can customize or change it later.'
			);
		}

		if ( useDIFMThemes ) {
			return translate(
				'We create a custom design based on the content you submit after checkout. Optionally, select a design to suggest inspiration.'
			);
		}

		const text = translate( 'Choose a starting theme. You can change it later.' );

		if ( englishLocales.includes( translate.localeSlug ) ) {
			// An English only trick so the line wraps between sentences.
			return text
				.replace( /\s/g, '\xa0' ) // Replace all spaces with non-breaking spaces
				.replace( /\.\s/g, '. ' ); // Replace all spaces at the end of sentences with a regular breaking space
		}

		return text;
	}

	function skipLabelText() {
		const { signupDependencies } = props;

		if ( signupDependencies?.intent === 'write' ) {
			return translate( 'Skip and draft first post' );
		}

		// Fall back to the default skip label used by <StepWrapper>
		return undefined;
	}

	const intent = props.signupDependencies.intent;
	const headerProps = showDesignPickerCategories
		? { hideFormattedHeader: true }
		: {
				fallbackHeaderText: headerText(),
				headerText: headerText(),
				fallbackSubHeaderText: subHeaderText(),
				subHeaderText: subHeaderText(),
		  };

	return (
		<StepWrapper
			{ ...props }
			className={ clsx( {
				'design-picker__has-categories': showDesignPickerCategories,
				'design-picker__hide-category-column': useDIFMThemes || 'sell' === intent,
			} ) }
			{ ...headerProps }
			stepContent={ renderDesignPicker() }
			align="left"
			skipButtonAlign="top"
			skipLabelText={ skipLabelText() }
		/>
	);
}

DesignPickerStep.propTypes = {
	goToNextStep: PropTypes.func.isRequired,
	signupDependencies: PropTypes.object.isRequired,
	stepName: PropTypes.string.isRequired,
};

// Ensures Blog category appears at the top of the design category list
// (directly below the All Themes category).
function sortBlogToTop( a, b ) {
	if ( a.slug === b.slug ) {
		return 0;
	} else if ( a.slug === 'blog' ) {
		return -1;
	} else if ( b.slug === 'blog' ) {
		return 1;
	}
	return 0;
}
// Ensures store category appears at the top of the design category list
// (directly below the All Themes category).
function sortStoreToTop( a, b ) {
	if ( a.slug === b.slug ) {
		return 0;
	} else if ( a.slug === 'store' ) {
		return -1;
	} else if ( b.slug === 'store' ) {
		return 1;
	}
	return 0;
}