| | import { OnboardSelect, StepperInternalSelect } from '@automattic/data-stores'; |
| | import { useSelect } from '@wordpress/data'; |
| | import { useCallback, useMemo } from '@wordpress/element'; |
| | import { |
| | STEPPER_TRACKS_EVENT_STEP_NAV_EXIT_FLOW, |
| | STEPPER_TRACKS_EVENT_STEP_NAV_GO_BACK, |
| | STEPPER_TRACKS_EVENT_STEP_NAV_GO_NEXT, |
| | STEPPER_TRACKS_EVENT_STEP_NAV_GO_TO, |
| | STEPPER_TRACKS_EVENT_STEP_NAV_SUBMIT, |
| | } from 'calypso/landing/stepper/constants'; |
| | import { ONBOARD_STORE, STEPPER_INTERNAL_STORE } from 'calypso/landing/stepper/stores'; |
| | import { |
| | recordStepNavigation, |
| | type RecordStepNavigationParams, |
| | } from '../../analytics/record-step-navigation'; |
| | import type { Flow, FlowV2, Navigate, ProvidedDependencies, StepperStep } from '../../types'; |
| |
|
| | interface Params { |
| | flow: Flow | FlowV2< any >; |
| | stepSlugs: string[]; |
| | currentStepRoute: StepperStep[ 'slug' ]; |
| | navigate: Navigate; |
| | } |
| |
|
| | export const useStepNavigationWithTracking = ( { |
| | flow, |
| | stepSlugs, |
| | currentStepRoute, |
| | navigate, |
| | }: Params ) => { |
| | |
| | |
| | const stepNavigation: any = flow.useStepNavigation( currentStepRoute, navigate ); |
| | const { intent, goals } = useSelect( ( select ) => { |
| | const onboardStore = select( ONBOARD_STORE ) as OnboardSelect; |
| | return { |
| | intent: onboardStore.getIntent(), |
| | goals: onboardStore.getGoals(), |
| | }; |
| | }, [] ); |
| |
|
| | const stepData = useSelect( |
| | ( select ) => ( select( STEPPER_INTERNAL_STORE ) as StepperInternalSelect ).getStepData(), |
| | [] |
| | ); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | const canUserGoBack = |
| | stepData?.previousStep && |
| | currentStepRoute !== stepSlugs[ 0 ] && |
| | history.length > 1 && |
| | stepData.previousStep !== currentStepRoute; |
| |
|
| | const tracksEventPropsFromFlow = flow.useTracksEventProps?.(); |
| |
|
| | const handleRecordStepNavigation = useCallback( |
| | ( { |
| | event, |
| | providedDependencies, |
| | }: Omit< RecordStepNavigationParams, 'step' | 'intent' | 'goals' | 'flow' | 'variant' > ) => { |
| | const { eventProps, ...dependencies } = providedDependencies || {}; |
| |
|
| | recordStepNavigation( { |
| | event, |
| | intent: intent ?? '', |
| | goals: goals ?? [], |
| | flow: flow.name, |
| | step: currentStepRoute, |
| | variant: flow.variantSlug, |
| | providedDependencies: dependencies, |
| | additionalProps: { |
| | ...( eventProps ?? {} ), |
| | |
| | |
| | |
| | ...( tracksEventPropsFromFlow?.isLoading |
| | ? undefined |
| | : tracksEventPropsFromFlow?.eventsProperties?.[ event ] ?? {} ), |
| | }, |
| | } ); |
| | }, |
| | [ intent, tracksEventPropsFromFlow, goals, currentStepRoute, flow ] |
| | ); |
| |
|
| | return useMemo( |
| | () => ( { |
| | ...( stepNavigation.submit && { |
| | |
| | submit: ( providedDependencies: ProvidedDependencies = {} ) => { |
| | if ( ! providedDependencies?.shouldSkipSubmitTracking ) { |
| | handleRecordStepNavigation( { |
| | event: STEPPER_TRACKS_EVENT_STEP_NAV_SUBMIT, |
| | providedDependencies, |
| | } ); |
| | } |
| | |
| | |
| | if ( 'initialize' in flow ) { |
| | stepNavigation.submit?.( { slug: currentStepRoute, providedDependencies } ); |
| | } else { |
| | stepNavigation.submit?.( providedDependencies ); |
| | } |
| | }, |
| | } ), |
| | ...( stepNavigation.exitFlow && { |
| | exitFlow: ( to: string ) => { |
| | handleRecordStepNavigation( { |
| | event: STEPPER_TRACKS_EVENT_STEP_NAV_EXIT_FLOW, |
| | providedDependencies: { |
| | eventProps: { |
| | to, |
| | }, |
| | }, |
| | } ); |
| | stepNavigation.exitFlow?.( to ); |
| | }, |
| | } ), |
| | |
| | |
| | |
| | |
| | |
| | ...( canUserGoBack && { |
| | goBack: () => { |
| | handleRecordStepNavigation( { |
| | event: STEPPER_TRACKS_EVENT_STEP_NAV_GO_BACK, |
| | } ); |
| | history.back(); |
| | }, |
| | } ), |
| | |
| | |
| | |
| | ...( stepNavigation.goBack && { |
| | goBack: () => { |
| | handleRecordStepNavigation( { |
| | event: STEPPER_TRACKS_EVENT_STEP_NAV_GO_BACK, |
| | } ); |
| | stepNavigation.goBack?.(); |
| | }, |
| | } ), |
| | ...( stepNavigation.goNext && { |
| | goNext: () => { |
| | handleRecordStepNavigation( { |
| | event: STEPPER_TRACKS_EVENT_STEP_NAV_GO_NEXT, |
| | } ); |
| | stepNavigation.goNext?.(); |
| | }, |
| | } ), |
| | ...( stepNavigation.goToStep && { |
| | goToStep: ( step: string ) => { |
| | handleRecordStepNavigation( { |
| | event: STEPPER_TRACKS_EVENT_STEP_NAV_GO_TO, |
| | providedDependencies: { |
| | eventProps: { |
| | to: step, |
| | }, |
| | }, |
| | } ); |
| | stepNavigation.goToStep?.( step ); |
| | }, |
| | } ), |
| | } ), |
| | [ handleRecordStepNavigation, stepNavigation ] |
| | ); |
| | }; |
| |
|