File size: 3,497 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
import { SiteDetails } from '@automattic/data-stores';
import debugFactory from 'debug';
import { useEffect } from 'react';
import { useSelector } from 'calypso/state';
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import type { Flow, FlowV2, StepperStep } from '../../types';

const debug = debugFactory( 'calypso:stepper:preloading' );

export const lazyCache = new WeakMap<
	StepperStep[ 'asyncComponent' ],
	Awaited< ReturnType< StepperStep[ 'asyncComponent' ] > >[ 'default' ]
>();

async function tryPreload( step?: StepperStep, followingStep?: StepperStep ) {
	if ( step && 'asyncComponent' in step ) {
		debug( 'Preloading step:', step.slug );
		const { default: component } = await step.asyncComponent();
		lazyCache.set( step.asyncComponent, component );
	}
	// Flows are indeterminate, they often pick one of the two next steps based on user input, so load two steps ahead.
	if ( followingStep ) {
		tryPreload( followingStep );
	}
}

/**
 * Preload the next step in the flow, if it's an async component.
 * @param siteSlugOrId The site slug or ID.
 * @param selectedSite The selected site.
 * @param currentStepRoute The current step route.
 * @param flowSteps The flow steps.
 * @param flow The flow.
 */
export function usePreloadSteps(
	siteSlugOrId: string | number,
	selectedSite: SiteDetails | undefined | null,
	currentStepRoute: string,
	flowSteps: readonly StepperStep[],
	flow: Flow | FlowV2< any >
) {
	const isLoggedIn = useSelector( isUserLoggedIn );

	useEffect( () => {
		if ( siteSlugOrId && ! selectedSite ) {
			// If this step depends on a selected site, only preload after we have the data.
			// Otherwise, we're still waiting to render something meaningful, and we don't want to
			// potentially slow that down by having the CPU busy initialising future steps.
			return;
		}
		if ( currentStepRoute ) {
			// The user step is a special case, as it's not part of the flow steps. It always comes in the end of the steps array.
			if ( currentStepRoute === 'user' ) {
				// Load the first steps that requires authentication.
				const nextStepIndex = flowSteps.findIndex( ( step ) => step.requiresLoggedInUser );
				const nextStep = flowSteps[ nextStepIndex ];
				const nextNextStep = flowSteps[ nextStepIndex + 1 ];
				tryPreload( nextStep, nextNextStep );
			} else {
				// If any step requires authentication, preload the user step.
				if ( ! isLoggedIn && flowSteps.some( ( step ) => step.requiresLoggedInUser ) ) {
					const userStep = flowSteps.find( ( step ) => step.slug === 'user' );
					tryPreload( userStep );
				}

				const currentStepIndex = flowSteps.findIndex( ( step ) => step.slug === currentStepRoute );
				if ( currentStepIndex !== -1 ) {
					const nextStep = flowSteps[ currentStepIndex + 1 ];
					const nextNextStep = flowSteps[ currentStepIndex + 2 ];

					tryPreload( nextStep, nextNextStep );
				}
			}
		}
		// Most flows sadly instantiate a new steps array on every call to `flow.useSteps()`,
		// which means that we don't want to depend on `flowSteps` here, or this would end up
		// running on every render. We thus depend on `flow` instead.
		//
		// This should be safe, because flows shouldn't return different lists of steps at
		// different points. But even if they do, worst case scenario we only fail to preload
		// some steps, and they'll simply be loaded later.
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, [ siteSlugOrId, selectedSite, currentStepRoute, flow ] );
}