/** * WordPress dependencies */ import { Button, Dashicon, PanelBody } from '@safe-wordpress/components'; import { useSelect } from '@safe-wordpress/data'; import { useState } from '@safe-wordpress/element'; import { _x } from '@safe-wordpress/i18n'; /** * External dependencies */ import { store as NAB_SEGMENTS } from '@nab/segmentation-rules'; import { isEmpty } from '@nab/utils'; import type { Dict, SegmentationRuleTypeName, SegmentationRule as SR, } from '@nab/types'; /** * Internal dependencies */ import './style.scss'; import { store as NAB_EDITOR } from '../../../store'; export type SegmentationRuleProps = { readonly readOnly?: boolean; readonly rule: SR; readonly setAttributes: ( attrs: Dict ) => void; readonly remove: () => void; }; export const SegmentationRule = ( { readOnly, rule, setAttributes, remove, }: SegmentationRuleProps ): JSX.Element => { const experimentId = useExperimentId(); const ruleType = useSegmentationRuleType( rule.type ); const View = ruleType?.view; const Edit = ruleType?.edit; const Icon = ruleType?.icon; const attributes = rule?.attributes || {}; const validate = ruleType?.validate ?? ( () => ( {} ) ); const errors = validate( attributes ); const hasErrors = ! isEmpty( errors ); const [ opened, onToggle ] = useState( hasErrors ); if ( readOnly ) { return (