File size: 932 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
import { useDispatch } from '@wordpress/data';
import { ONBOARD_STORE } from 'calypso/landing/stepper/stores';
import type { Navigate } from '../declarative-flow/internals/types';

type UseExitFlowParams =
	| { processing: true; navigate: Navigate }
	| { processing?: false; navigate?: Navigate }
	| undefined;

/**
 * The useExitFlow hook provides a function to handle exiting a flow
 * by setting a pending action that redirects the browser to a specified URL,
 * with an optional navigation step if processing is required.
 */
export const useExitFlow = ( params?: UseExitFlowParams ) => {
	const { setPendingAction } = useDispatch( ONBOARD_STORE );

	const exitFlow = ( to: string ) => {
		setPendingAction( () => {
			return new Promise( () => {
				window.location.assign( to );
			} );
		} );

		if ( params?.processing && params?.navigate ) {
			return params.navigate( 'processing' );
		}
	};

	return {
		exitFlow,
	};
};