/** * WordPress dependencies */ import { useSelect, useDispatch } from '@safe-wordpress/data'; import { createInterpolateElement } from '@safe-wordpress/element'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import type { Dict, SegmentationRuleId } from '@nab/types'; /** * Internal dependencies */ import './style.scss'; import { SegmentationRule } from '../segmentation-rule'; import { store as NAB_EDITOR } from '../../../store'; export const SegmentationRuleList = (): JSX.Element => { const segmentationRules = useActiveSegmentRules(); const setRuleAttributes = useSetRuleAttributes(); const removeRule = useRemoveRule(); return (
{ !! segmentationRules.length && (

{ _x( 'A visitor participates in the test every time all of the following segmentation rules match:', 'text', 'nelio-ab-testing' ) }

) } { segmentationRules.map( ( rule ) => ( setRuleAttributes( rule.id, attrs ) } remove={ () => removeRule( rule.id ) } /> ) ) } { ! segmentationRules.length && [

{ _x( 'Split tests are usually applied to all visitors. But you can narrow your audience using segmentation rules to target only a subset of your visitors.', 'text', 'nelio-ab-testing' ) }

,

{ createInterpolateElement( _x( 'Use the buttons below to add the specific segmentation rules for this segment.', 'user', 'nelio-ab-testing' ), { strong: , } ) }

, ] }
); }; // ===== // HOOKS // ===== const useActiveSegment = () => useSelect( ( select ) => select( NAB_EDITOR ).getActiveSegment(), [] ); const useActiveSegmentRules = () => { const activeSegment = useActiveSegment(); return useSelect( ( select ) => { const { getSegmentationRules } = select( NAB_EDITOR ); return activeSegment?.id ? getSegmentationRules( activeSegment.id ) || [] : []; }, [ activeSegment ] ); }; const useSetRuleAttributes = (): ( ( ruleId: SegmentationRuleId, attrs: Dict ) => void ) => { const segmentId = useActiveSegment()?.id; const { updateSegmentationRule } = useDispatch( NAB_EDITOR ); return ( ruleId, attrs ) => segmentId ? updateSegmentationRule( segmentId, ruleId, attrs ) : noop; }; const useRemoveRule = (): ( ( ruleId: SegmentationRuleId ) => void ) => { const segmentId = useActiveSegment()?.id; const { removeSegmentationRulesFromSegment } = useDispatch( NAB_EDITOR ); return ( ruleId ) => segmentId ? removeSegmentationRulesFromSegment( segmentId, ruleId ) : noop; }; const noop = () => void null;