File size: 6,046 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
import { generatePath } from 'react-router';
import { Primitive } from 'utility-types';
import { STEPS } from 'calypso/landing/stepper/declarative-flow/internals/steps';
import { ImporterPlatform } from 'calypso/lib/importer/types';
import { addQueryArgs } from 'calypso/lib/url';

/**
 * Builds a path helper function that can be used to build paths with query params and path params.
 * @param path - The path to build.
 * @returns A function that can be used to build paths with query params and path params.
 * @example
 * const path = buildPathHelper<{
 * 	queryParams: {
 * 		from: string;
 * 	};
 * 	params: {
 * 		id: string;
 * 	};
 * }>( '/test/:id' );
 *
 * path( { from: 'test' }, { id: '123' } ); // '/test/123?from=test'
 * path( { from: 'test' } ); // '/test?from=test'
 * path( null, { id: '123' } ); // '/test/123'
 */
export function buildPathHelper<
	T extends { queryParams?: Record< string, Primitive | null >; params?: Record< string, string > },
	U extends string = string,
>( path: U ) {
	return ( queryParams?: T[ 'queryParams' ] | null, params?: T[ 'params' ] ) => {
		const pathWithParams = generatePath(
			path as string,
			params as Record< string, string >
		) as `${ U }?${ string }`;

		if ( ! queryParams ) {
			return pathWithParams;
		}

		return addQueryArgs( queryParams, pathWithParams ) as `${ U }?${ string }`;
	};
}

export const siteCreationPath = buildPathHelper<
	{
		queryParams: {
			from?: string | null;
			platform: ImporterPlatform;
		};
	},
	typeof STEPS.SITE_CREATION_STEP.slug
>( STEPS.SITE_CREATION_STEP.slug );

export const sitePickerPath = buildPathHelper<
	{
		queryParams: { from: string | null; platform: ImporterPlatform };
	},
	typeof STEPS.PICK_SITE.slug
>( STEPS.PICK_SITE.slug );

export const importOrMigratePath = buildPathHelper<
	{
		queryParams: {
			from?: string;
			siteSlug: string;
			siteId?: number | string;
		};
	},
	typeof STEPS.SITE_MIGRATION_IMPORT_OR_MIGRATE.slug
>( STEPS.SITE_MIGRATION_IMPORT_OR_MIGRATE.slug );

export const howToMigratePath = buildPathHelper<
	{
		queryParams: {
			from?: string | null;
			siteSlug: string;
			siteId?: number | string;
		};
	},
	typeof STEPS.SITE_MIGRATION_HOW_TO_MIGRATE.slug
>( STEPS.SITE_MIGRATION_HOW_TO_MIGRATE.slug );

export const processingPath = buildPathHelper<
	{
		queryParams: {
			from?: string | null;
			platform: ImporterPlatform;
			action: string | null;
		};
	},
	typeof STEPS.PROCESSING.slug
>( STEPS.PROCESSING.slug );

export const credentialsPath = buildPathHelper<
	{
		queryParams: {
			siteId?: number | string;
			siteSlug?: string;
			from?: string | null;
			authorizationUrl?: string;
			backTo?: string;
		};
	},
	typeof STEPS.SITE_MIGRATION_CREDENTIALS.slug
>( STEPS.SITE_MIGRATION_CREDENTIALS.slug );

export const upgradePlanPath = buildPathHelper<
	{
		queryParams: {
			siteId?: number | string;
			siteSlug?: string;
			from?: string | null;
			destination?: string;
			how?: string;
		};
	},
	typeof STEPS.SITE_MIGRATION_UPGRADE_PLAN.slug
>( STEPS.SITE_MIGRATION_UPGRADE_PLAN.slug );

export const alreadyWpcomPath = buildPathHelper<
	{
		queryParams: {
			siteId?: number | string;
			siteSlug?: string;
			from?: string | null;
		};
	},
	typeof STEPS.SITE_MIGRATION_ALREADY_WPCOM.slug
>( STEPS.SITE_MIGRATION_ALREADY_WPCOM.slug );

export const instructionsPath = buildPathHelper<
	{
		queryParams: {
			siteId?: number | string;
			siteSlug?: string;
			from?: string | null;
		};
	},
	typeof STEPS.SITE_MIGRATION_INSTRUCTIONS.slug
>( STEPS.SITE_MIGRATION_INSTRUCTIONS.slug );

export const otherPlatformDetectedImportPath = buildPathHelper<
	{
		queryParams: {
			siteId?: number | string;
			siteSlug?: string;
			from?: string | null;
			platform?: ImporterPlatform;
		};
	},
	typeof STEPS.SITE_MIGRATION_OTHER_PLATFORM_DETECTED_IMPORT.slug
>( STEPS.SITE_MIGRATION_OTHER_PLATFORM_DETECTED_IMPORT.slug );

export const applicationPasswordAuthorizationPath = buildPathHelper<
	{
		queryParams: {
			siteId?: number | string;
			siteSlug?: string;
			from?: string | null;
			authorizationUrl?: string | null;
			success?: string | null;
			password?: string | null;
			user_login?: string | null;
		};
	},
	typeof STEPS.SITE_MIGRATION_APPLICATION_PASSWORD_AUTHORIZATION.slug
>( STEPS.SITE_MIGRATION_APPLICATION_PASSWORD_AUTHORIZATION.slug );

export const fallbackCredentialsPath = buildPathHelper<
	{
		queryParams: {
			siteId?: number | string;
			siteSlug?: string;
			from?: string | null;
			authorizationUrl?: string | null;
			backTo?: string;
		};
	},
	typeof STEPS.SITE_MIGRATION_FALLBACK_CREDENTIALS.slug
>( STEPS.SITE_MIGRATION_FALLBACK_CREDENTIALS.slug );

export const supportInstructionsPath = buildPathHelper<
	{
		queryParams: {
			siteId?: number | string;
			siteSlug?: string;
			from?: string | null;
			preventTicketCreation?: boolean;
		};
	},
	typeof STEPS.SITE_MIGRATION_SUPPORT_INSTRUCTIONS.slug
>( STEPS.SITE_MIGRATION_SUPPORT_INSTRUCTIONS.slug );

export const identifyPath = buildPathHelper<
	{
		queryParams: { from: string | null };
	},
	typeof STEPS.SITE_MIGRATION_IDENTIFY.slug
>( STEPS.SITE_MIGRATION_IDENTIFY.slug );

export const siteSetupImportListPath = buildPathHelper< {
	queryParams: {
		from: string | null;
		siteId?: number | string;
		siteSlug: string;
		origin: string;
		backToFlow: string;
	};
} >( `/setup/site-setup/${ STEPS.IMPORT_LIST.slug }` );

export const calypsoImporterPath = buildPathHelper< {
	queryParams: { engine: string; ref: string };
	params: { siteSlug: string };
} >( '/import/:siteSlug' );

export const siteSetupImportWordpressPath = buildPathHelper< {
	queryParams: {
		siteId?: number | string;
		siteSlug: string;
		from: string;
		backToFlow: string;
	};
} >( '/setup/site-setup/importerWordpress' );

export const calypsoOverviewPath = buildPathHelper< {
	queryParams: { ref: string };
	params: { siteSlug: string };
} >( '/overview/:siteSlug' );

export const siteSetupGoalsPath = buildPathHelper< {
	queryParams: {
		siteId?: number | string;
		siteSlug: string;
	};
} >( '/setup/site-setup/goals' );