Back

100% Statements 6/6
50% Branches 1/2
100% Functions 2/2
100% Lines 6/6

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                    1x                                 42x       42x   2x 2x         42x    
/**
 * External dependencies
 */
import { useStagedSearch } from '@jetpack-premium-analytics/routing';
import { useCallback } from '@wordpress/element';
/**
 * Internal dependencies
 */
import { route } from '../package.json';
 
const ROUTE_FROM = route.path;
 
type SectionSearch = {
	section?: string;
};
 
/**
 * Read and update the active section via the `?section=` search param, the dashboard's
 * deep-linkable model. Switching stages and commits at once: one history entry per change,
 * unless `replace` is set to normalize a deep link.
 *
 * @return The section the URL names, and a setter.
 */
export function useActiveSection(): [
	string | undefined,
	( slug: string, options?: { replace?: boolean } ) => void,
] {
	const { effective, stage, commit } = useStagedSearch< SectionSearch, typeof ROUTE_FROM >( {
		from: ROUTE_FROM,
	} );
 
	const setActiveSection = useCallback(
		( slug: string, options?: { replace?: boolean } ) => {
			stage( { section: slug } );
			commit( { replace: options?.replace ?? false } );
		},
		[ stage, commit ]
	);
 
	return [ effective.section, setActiveSection ];
}