import {useEffect, useState, useCallback, useMemo, useRef, forwardRef} from '@wordpress/element'; import Section from './Section'; const storageKey = 'adpresso_modal_section_states'; // Type sections are always closed when an entity is edited; the assignment only once the ad is assigned somewhere. const TYPE_SECTIONS = ['adpresso_ad_type', 'adpresso_placement_type']; const ASSIGNMENT_SECTION = 'adpresso_ad_assignment'; // The open/closed state of the other sections, remembered per browser. const loadSectionState = () => { try { const raw = window.localStorage.getItem( storageKey ); return raw ? JSON.parse( raw ) || {} : {}; } catch { return {}; } }; const rememberSectionState = ( id, collapsed ) => { if ( TYPE_SECTIONS.includes( id ) || id === ASSIGNMENT_SECTION ) { return; } try { window.localStorage.setItem( storageKey, JSON.stringify( {...loadSectionState(), [id]: collapsed} ) ); } catch {} }; // Creating: only what a first-time user needs is open, the rest is a closed summary. const OPEN_ON_CREATE = [ 'adpresso_ad_type', 'adpresso_ad_content', 'adpresso_ad_assignment', 'adpresso_group_ads', 'adpresso_group_output', 'adpresso_group_banner_blindness', 'adpresso_placement_type', 'adpresso_placement_general', ]; const isAssigned = ( entityData ) => ( entityData?.used_in?.placements?.length || 0 ) + ( entityData?.used_in?.groups?.length || 0 ) > 0; /** * Creating: type, content and assignment sections open (see OPEN_ON_CREATE), the rest closed. Editing: type sections * closed, the assignment closed once the ad is assigned, every other section as it was left last time (open by default). * * @param {Array} sections * @param {Object} entityData * @returns {Object} Section id -> collapsed. */ export const initialCollapsedStates = ( sections, entityData ) => { const isUpdate = ( entityData?.id || 0 ) > 0; const remembered = isUpdate ? loadSectionState() : {}; const states = {}; ( sections || [] ).filter( Boolean ).forEach( ( sec ) => { if ( !isUpdate ) { states[sec.id] = !OPEN_ON_CREATE.includes( sec.id ); } else if ( TYPE_SECTIONS.includes( sec.id ) ) { states[sec.id] = true; } else if ( sec.id === ASSIGNMENT_SECTION ) { states[sec.id] = isAssigned( entityData ); } else { states[sec.id] = !!remembered[sec.id]; } } ); return states; }; const SectionRenderer = forwardRef( ( {sections, allMetaData, onDataChange, availableConditions, entityData, i18n, onSetDirty}, ref ) => { const [dependentFields, setDependentFields] = useState( [] ); const [compatibleConditions, setCompatibleConditions] = useState( null ); const [collapsedStates, setCollapsedStates] = useState( null ); const isUpdate = ( entityData?.id || 0 ) > 0; const previousId = useRef( undefined ); const onToggleSection = useCallback( ( id ) => { setCollapsedStates( prev => { const next = {...prev, [id]: !prev[id]}; // Toggles while creating don't change how edited entities open. if ( isUpdate ) { rememberSectionState( id, next[id] ); } return next; } ); }, [isUpdate] ); useEffect( () => { const id = entityData?.id || 0; // The first save of a new entity gives it an ID: the sections stay as they are. const justCreated = previousId.current === 0 && id > 0; previousId.current = id; if ( justCreated ) { return; } setCollapsedStates( initialCollapsedStates( sections, entityData ) ); }, [sections, entityData?.id] ); const handleAdTypeChange = useCallback( ( fieldId, newValue, metaKey, options ) => { onDataChange( fieldId, newValue, metaKey, options ); if ( options?.silent ) { return; } setCollapsedStates( prev => ( {...prev, adpresso_ad_content: false} ) ); }, [onDataChange] ); // Finds the controller field (e.g. Placement Type) across all sections const hiddenSectionIds = useMemo( () => { for ( const section of sections || [] ) { const controllerField = section.fields?.find( f => f.controls_dependencies === true ); if ( !controllerField ) { continue; } const selectedValue = entityData?.meta_data?.[controllerField.id]; const selectedOption = controllerField.options ? controllerField.options[selectedValue] : null; return selectedOption?.hidden_sections || []; } return []; }, [sections, entityData?.meta_data] ); const visibleSections = useMemo( () => (sections || []).filter( section => !hiddenSectionIds.includes( section.id ) ), [sections, hiddenSectionIds] ); if ( !sections || sections.length === 0 ) { return null; } if ( !collapsedStates ) { return null; } return (
{visibleSections.map( section => { const isAdTypeSection = section.fields?.some( f => f.type === 'image_group' ); const sectionProps = { entityData: entityData, key: section.id, section: section, allMetaData: entityData.meta_data, onDataChange: isAdTypeSection ? handleAdTypeChange : onDataChange, availableConditions: availableConditions, compatibleConditions: compatibleConditions, i18n: i18n, isCollapsed: collapsedStates[section.id] ?? true, onToggleSection: onToggleSection, sectionId: section.id, ref: ref, onSetDirty: onSetDirty }; if ( section.controls_dependencies === true ) { sectionProps.setDependentFields = setDependentFields; sectionProps.setCompatibleConditions = setCompatibleConditions; } if ( section.receives_dependencies === true ) { sectionProps.injectedFields = dependentFields; } return
; } )}
); } ); export default SectionRenderer;