Back

100% Statements 9/9
100% Branches 4/4
100% Functions 5/5
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                3x                     9x                   18x                   18x   18x             18x       8x 8x      
import analytics from '@automattic/jetpack-analytics';
import { useCallback } from '@wordpress/element';
import useVideoPressCheckout from '../../client/hooks/use-videopress-checkout';
import { VIDEOPRESS_ADMIN_PAGE } from '../utils/constants';
 
// Tracks event recorded when the upgrade CTA is clicked. Carried over verbatim
// from the legacy dashboard's `UpgradeTrigger` so both dashboards report
// against the same funnel.
const UPGRADE_CLICK_EVENT = 'jetpack_videopress_upgrade_trigger_link_click';
 
export type UpgradeLocation =
	'overview' | 'library_at_limit' | 'settings_at_limit' | 'upload_limit' | 'pricing_upsell';
 
/**
 * Record an upgrade action without changing its checkout workflow.
 *
 * @param location - Surface whose upgrade action was selected.
 */
export function recordVideoPressUpgradeClick( location: UpgradeLocation ): void {
	analytics.tracks.recordEvent( UPGRADE_CLICK_EVENT, { location } );
}
 
/**
 * Read the inlined initial state, guarding for environments (tests, the legacy
 * page) where the `var JPVIDEOPRESS_INITIAL_STATE` is absent.
 *
 * @return The initial-state payload, or undefined when it isn't present.
 */
function getInitialState() {
	return typeof JPVIDEOPRESS_INITIAL_STATE !== 'undefined' ? JPVIDEOPRESS_INITIAL_STATE : undefined;
}
 
/**
 * Record the upgrade click and start the platform-appropriate checkout.
 *
 * @param location - Surface whose upgrade action was selected.
 * @return A callback that records the upgrade-click event and starts checkout.
 */
export function useVideoPressUpgrade( location: UpgradeLocation ): () => void {
	const state = getInitialState();
 
	const { run } = useVideoPressCheckout( {
		productSlug: state?.product?.slug ?? '',
		redirectUrl: VIDEOPRESS_ADMIN_PAGE,
		useBlogIdSuffix: true,
		from: 'jetpack-videopress',
	} );
 
	return useCallback( () => {
		// Record the click, then defer the checkout redirect by a microtask so
		// the Tracks pixel is dispatched before navigation can cancel it.
		// Mirrors the legacy dashboard's `recordEvent( … ).then( run )`.
		recordVideoPressUpgradeClick( location );
		void Promise.resolve().then( () => run() );
	}, [ run, location ] );
}