File size: 1,320 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
import { SITE_SETUP_FLOW } from '@automattic/onboarding';
import { ImporterPlatform } from 'calypso/lib/importer/types';
import { addQueryArgs } from 'calypso/lib/url';
import { getFinalImporterUrl } from '../../internals/steps-repository/import/helper';

const isWpAdminImporter = ( importerPath: string ) =>
	importerPath.startsWith( 'http' ) || importerPath.startsWith( '/import' );

interface GoToImporterParams {
	platform: ImporterPlatform;
	siteId: string;
	siteSlug: string;
	backToFlow?: string;
	replaceHistory?: boolean;
	from?: string | null;
	ref?: string;
}

/**
 * @deprecated generate the full flow URL in your flow instead
 * @see backToFlow property
 */

const goTo = ( path: string, replaceHistory: boolean ) => {
	if ( replaceHistory ) {
		return window.location.replace( path );
	}

	return window.location.assign( path );
};

export const goToImporter = ( {
	platform,
	siteId,
	siteSlug,
	replaceHistory = false,
	backToFlow,
	from,
	ref,
}: GoToImporterParams ) => {
	const path = getFinalImporterUrl( siteSlug, from || '', platform, backToFlow );

	if ( isWpAdminImporter( path ) ) {
		return goTo( path, replaceHistory );
	}

	const stepURL = addQueryArgs(
		{
			siteId,
			siteSlug,
			ref: ref,
		},
		`/setup/${ SITE_SETUP_FLOW }/${ path }`
	);

	return goTo( stepURL, replaceHistory );
};