Back

42.85% Statements 3/7
0% Branches 0/6
50% Functions 1/2
42.85% Lines 3/7

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          2x                                             4x                     4x                      
import { useCallback } from '@wordpress/element';
import { Link } from '@wordpress/ui';
import type { MouseEvent, ReactNode } from 'react';
 
/** Matches `Automattic\Jetpack\Protect\Dashboard::MENU_SLUG`. */
export const PROTECT_PAGE_SLUG = 'jetpack-protect';
 
/**
 * A link to a dashboard tab that switches tabs in place, and still opens in a new tab when asked.
 *
 * @param props          - Component props.
 * @param props.tab      - The tab's value, such as "settings".
 * @param props.params   - More search params to set, such as the tab's filter.
 * @param props.onOpen   - Switches to the tab.
 * @param props.children - The link text.
 * @return The link.
 */
export default function TabLink( {
	tab,
	params,
	onOpen,
	children,
}: {
	tab: string;
	params?: Record< string, string >;
	onOpen: ( tab: string, params?: Record< string, string > ) => void;
	children: ReactNode;
} ) {
	const onClick = useCallback(
		( event: MouseEvent< HTMLAnchorElement > ) => {
			if ( event.metaKey || event.ctrlKey || event.shiftKey || event.button !== 0 ) {
				return;
			}
			event.preventDefault();
			onOpen( tab, params );
		},
		[ onOpen, tab, params ]
	);
 
	return (
		<Link
			href={ `admin.php?page=${ PROTECT_PAGE_SLUG }&p=${ encodeURIComponent(
				`/?${ new URLSearchParams( { ...params, tab } ) }`
			) }` }
			onClick={ onClick }
		>
			{ children }
		</Link>
	);
}