File size: 3,050 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
import { isEnabled } from '@automattic/calypso-config';
import { Plans } from '@automattic/data-stores';
import languages from '@automattic/languages';
import { addQueryArgs } from '@wordpress/url';
import { useFlowLocale } from '../hooks/use-flow-locale';

const plansPaths = Plans.plansSlugs;

/**
 * The first step (IntentGathering), which is found at the root route (/), is set as
 * `undefined`, as that's what matching our `path` pattern against a route with no explicit
 * step fragment will return.
 */
export const Step = {
	IntentGathering: undefined,
	DesignSelection: 'design',
	Style: 'style',
	Features: 'features',
	Signup: 'signup',
	Login: 'login',
	CreateSite: 'create-site',
	Domains: 'domains',
	Emails: 'emails',
	Plans: 'plans',
	DomainsModal: 'domains-modal',
	PlansModal: 'plans-modal',
	LanguageModal: 'language-modal',
} as const;

// We remove falsy `steps` with `.filter( Boolean )` as they'd mess up our |-separated route pattern.
export const steps = Object.values( Step ).filter( Boolean );

const routeFragments = {
	// We add the possibility of an empty step fragment through the `?` question mark at the end of that fragment.
	step: `:step(${ steps.join( '|' ) })?`,
	plan: `:plan(${ plansPaths.join( '|' ) })?`,
	lang: `:lang(${ languages.map( ( lang ) => lang.langSlug ).join( '|' ) })?`,
};

export const path = [ '', ...Object.values( routeFragments ) ].join( '/' );

export const getLoginUrl = ( {
	variationName,
	redirectTo,
	pageTitle,
	locale,
	customLoginPath,
	extra = {},
}: {
	/**
	 * Variation name is used to track the relevant login flow in the signup framework as explained in https://github.com/Automattic/wp-calypso/issues/67173
	 */
	variationName?: string | null;
	redirectTo?: string | null;
	pageTitle?: string | null;
	locale: string;
	customLoginPath?: string | null;
	extra?: Record< string, string | number >;
} ): string => {
	const defaultLoginPath = `/start/account/${
		isEnabled( 'signup/social-first' ) ? 'user-social' : 'user'
	}`;
	const loginPath = customLoginPath || defaultLoginPath;
	const localizedLoginPath = locale && locale !== 'en' ? `${ loginPath }/${ locale }` : loginPath;

	// Empty values are ignored down the call stack, so we don't need to check for them here.
	return addQueryArgs( localizedLoginPath, {
		variationName,
		redirect_to: redirectTo,
		pageTitle,
		toStepper: true,
		...extra,
	} );
};

export const useLoginUrl = ( {
	variationName,
	redirectTo,
	pageTitle,
	locale,
	customLoginPath,
	extra = {},
}: {
	/**
	 * Variation name is used to track the relevant login flow in the signup framework as explained in https://github.com/Automattic/wp-calypso/issues/67173
	 */
	variationName?: string | null;
	redirectTo?: string | null;
	pageTitle?: string | null;
	locale?: string;
	customLoginPath?: string | null;
	extra?: Record< string, string | number >;
} ): string => {
	const currentLocale = useFlowLocale();
	return getLoginUrl( {
		variationName,
		redirectTo,
		pageTitle,
		locale: locale ?? currentLocale,
		customLoginPath,
		extra,
	} );
};