import { useWizardStore } from '@/store/reports/useWizardStore'; import { __ } from '@wordpress/i18n'; import FieldWrapper from '@/components/Fields/FieldWrapper'; import { useFormContext } from 'react-hook-form'; import { memo, useEffect, useRef } from 'react'; import useLicenseData from '@/hooks/useLicenseData'; import ProBadge from '@/components/Common/ProBadge'; import { useReportConfigStore } from '@/store/reports/useReportConfigStore'; import { ContentBlockId, ContentItem } from '@/store/reports/types'; import Icon from '@/utils/Icon'; /** * Classic content selection component. * Displays a grid of content blocks with checkboxes for enabling/disabling blocks. */ const ClassicContentSelection = () => { const availableContent = useReportConfigStore( ( state ) => state.availableContent ); const content = useWizardStore( ( state ) => state.wizard.content ); const addContent = useWizardStore( ( state ) => state.addContent ); const removeContent = useWizardStore( ( state ) => state.removeContent ); const shouldLoadEcommerce = window.burst_settings?.shouldLoadEcommerce || false; const { isPro, isLicenseValid } = useLicenseData(); const isFirstRender = useRef( true ); const { register, setValue, formState: { errors } } = useFormContext(); useEffect( () => { register( 'content', { value: content, validate: ( value: string[]) => 0 < value.length || __( 'Please select at least one content item', 'burst-statistics' ) }); }, [ register, content ]); // eslint-disable-line react-hooks/exhaustive-deps useEffect( () => { if ( isFirstRender.current ) { isFirstRender.current = false; return; } setValue( 'content', content, { shouldValidate: !! errors.content }); }, [ content, setValue ]); // eslint-disable-line react-hooks/exhaustive-deps const isSelected = ( blockId:ContentBlockId ) => { return content.some( item => item.id === blockId ); }; const handleToggle = ( block: ContentItem ) => { if ( block.pro && ( ! isLicenseValid || ! isPro ) ) { return; } const index = content.findIndex( item => item.id === block.id ); if ( -1 === index ) { addContent( block.id ); } else { removeContent( index ); } }; return (
{ availableContent .filter( ( block ) => ! block.ecommerce || shouldLoadEcommerce ) .filter( ( block ) => ! block.component ) .map( ( block:ContentItem, index ) => { const isBlockSelected = isSelected( block.id ); const isBlockProDisabled = block.pro && ( ! isLicenseValid || ! isPro ); return (
handleToggle( block )} className={` flex items-center gap-3 p-4 rounded-lg border cursor-pointer transition-all ${isBlockSelected ? 'border-green bg-green-50' : 'border-gray-200 hover:border-gray-300 bg-white'} ${isBlockProDisabled ? 'opacity-50 cursor-not-allowed' : 'hover:bg-gray-50'} `} > {block.icon && (
)} { block.pro && ! isLicenseValid && (
) }
{ handleToggle( block ); }} onClick={( e ) => { e.stopPropagation(); }} className="h-4 w-4 text-green border-gray-300 rounded focus:ring-2 focus:ring-blue" disabled={ isBlockProDisabled } />
); }) }
); }; export default memo( ClassicContentSelection );