|
|
import { stop as stopDataCollection } from '@automattic/browser-data-collector'; |
|
|
import { useLayoutEffect, useMemo } from 'react'; |
|
|
import { useStore } from 'react-redux'; |
|
|
import { |
|
|
isPerformanceTrackingEnabled, |
|
|
startPerformanceTracking, |
|
|
} from 'calypso/lib/performance-tracking/lib'; |
|
|
import { |
|
|
getCurrentUserCountryCode, |
|
|
getCurrentUserLocale, |
|
|
} from 'calypso/state/current-user/selectors'; |
|
|
import { getSelectedSiteId } from 'calypso/state/ui/selectors'; |
|
|
import type { Collector, Report } from '@automattic/browser-data-collector'; |
|
|
|
|
|
|
|
|
|
|
|
export function startStepperPerformanceTracking( { |
|
|
fullPageLoad = false, |
|
|
}: { |
|
|
fullPageLoad?: boolean; |
|
|
} ) { |
|
|
startPerformanceTracking( 'stepper', { fullPageLoad } ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useStartStepperPerformanceTracking( flow: string, step: string ) { |
|
|
useMemo( () => { |
|
|
startStepperPerformanceTracking( { fullPageLoad: false } ); |
|
|
|
|
|
|
|
|
}, [ flow, step ] ); |
|
|
} |
|
|
|
|
|
|
|
|
const EMPTY_STATE = { |
|
|
ui: {}, |
|
|
}; |
|
|
|
|
|
function buildStepperCollector( state: object, flow: string, step: string ): Collector { |
|
|
const siteId = getSelectedSiteId( state ); |
|
|
const userCountryCode = getCurrentUserCountryCode( state ); |
|
|
const userLocale = getCurrentUserLocale( state ); |
|
|
|
|
|
return ( report: Report ) => { |
|
|
|
|
|
report.data.set( 'siteId', siteId || '' ); |
|
|
report.data.set( 'userCountryCode', userCountryCode ); |
|
|
report.data.set( 'userLocale', userLocale ); |
|
|
|
|
|
report.data.set( 'flow', flow ); |
|
|
report.data.set( 'step', step ); |
|
|
|
|
|
return report; |
|
|
}; |
|
|
} |
|
|
|
|
|
function stopPerformanceTracking( |
|
|
name: string, |
|
|
{ state = EMPTY_STATE, flow = '', step = '' } = {} |
|
|
) { |
|
|
if ( isPerformanceTrackingEnabled() ) { |
|
|
stopDataCollection( name, { |
|
|
collectors: [ buildStepperCollector( state, flow, step ) ], |
|
|
} ); |
|
|
} |
|
|
} |
|
|
|
|
|
function useStepperPerformanceTrackerStop( flow: string, step: string ) { |
|
|
const store = useStore(); |
|
|
|
|
|
|
|
|
useLayoutEffect( () => { |
|
|
requestAnimationFrame( () => { |
|
|
stopPerformanceTracking( 'stepper', { |
|
|
|
|
|
state: store.getState(), |
|
|
flow, |
|
|
step, |
|
|
} ); |
|
|
} ); |
|
|
}, [ store, flow, step ] ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function StepperPerformanceTrackerStop( { flow, step }: { flow: string; step: string } ) { |
|
|
useStepperPerformanceTrackerStop( flow, step ); |
|
|
|
|
|
|
|
|
return null; |
|
|
} |
|
|
|