/** * WordPress dependencies */ import { Button, Dashicon } from '@safe-wordpress/components'; import { useSelect, useDispatch } from '@safe-wordpress/data'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import { SubscribeWithCouponButton } from '@nab/components'; import { store as NAB_DATA, usePageAttribute } from '@nab/data'; import { store as NAB_EXPERIMENTS } from '@nab/experiments'; /** * Internal dependencies */ import './style.scss'; import { useExperimentAttribute } from '../hooks'; import { HelpButton } from '../help-button'; export const Header = (): JSX.Element | null => { const Icon = useExperimentIcon(); const name = useExperimentAttribute( 'name' ); const status = useExperimentAttribute( 'status' ); if ( ! Icon ) { return null; } return (

{ name }

{ status === 'running' && ( <> ) }
); }; // ======= // HELPERS // ======= const PauseButton = () => { const { pauseExperiment } = useDispatch( NAB_DATA ); const { canUserPause } = useCapabilities(); const [ isStopping ] = usePageAttribute( 'editor/isExperimentBeingStopped', false ); const [ isPausing ] = usePageAttribute( 'editor/isExperimentBeingPaused', false ); if ( ! canUserPause ) { return null; } if ( isStopping || isPausing ) { return ( { isPausing ? _x( 'Pausing…', 'text', 'nelio-ab-testing' ) : _x( 'Stopping…', 'text', 'nelio-ab-testing' ) } ); } return ( ); }; const StopButton = () => { const { stopExperiment } = useDispatch( NAB_DATA ); const { canUserStop } = useCapabilities(); const [ isStopping ] = usePageAttribute( 'editor/isExperimentBeingStopped', false ); const [ isPausing ] = usePageAttribute( 'editor/isExperimentBeingPaused', false ); if ( ! canUserStop ) { return null; } return ( ); }; // ===== // HOOKS // ===== const useExperimentIcon = () => { const type = useExperimentAttribute( 'type' ); const experimentType = useSelect( ( select ) => select( NAB_EXPERIMENTS ).getExperimentTypes()[ type ?? '' ], [ type ] ); return experimentType?.icon ?? ( () => <> ); }; const useCapabilities = () => useSelect( ( select ) => ( { canUserPause: select( NAB_DATA ).hasCapability( 'pause_nab_experiments' ), canUserStop: select( NAB_DATA ).hasCapability( 'stop_nab_experiments' ), } ), [] );