Skip to content

Commit

Permalink
Minimize hook for meta boxes initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed Nov 25, 2024
1 parent 0b4af6f commit db46c07
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
3 changes: 2 additions & 1 deletion packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ function Layout( {
settings.supportsTemplateMode,
]
);
useMetaBoxInitialization( showMetaBoxes );
useMetaBoxInitialization( hasActiveMetaboxes );

const [ paddingAppenderRef, paddingStyle ] = usePaddingAppender(
enablePaddingAppender
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,22 @@ import { useEffect } from '@wordpress/element';
import { store as editPostStore } from '../../store';

/**
* Initializes postboxes (wp core script) and meta box saving for all meta box locations.
* Initializes WordPress `postboxes` script and the logic for saving meta boxes.
*
* @param { boolean } enabled
*/
export default ( enabled ) => {
const { areInitialized, isEditorReady, hasAny } = useSelect(
( select ) => {
if ( ! enabled ) {
return {};
}
const { __unstableIsEditorReady } = select( editorStore );
const { areMetaBoxesInitialized, getMetaBoxesPerLocation } =
select( editPostStore );
return {
areInitialized: areMetaBoxesInitialized(),
isEditorReady: __unstableIsEditorReady(),
hasAny:
getMetaBoxesPerLocation( 'normal' ).length > 0 ||
getMetaBoxesPerLocation( 'advanced' ).length > 0 ||
getMetaBoxesPerLocation( 'side' ).length > 0,
};
},
const isEnabledAndEditorReady = useSelect(
( select ) =>
enabled && select( editorStore ).__unstableIsEditorReady(),
[ enabled ]
);
const { initializeMetaBoxes } = useDispatch( editPostStore );
// The effect has to rerun when the editor is ready because initializeMetaBoxes
// will noop until then.
useEffect( () => {
if ( isEditorReady && hasAny && ! areInitialized ) {
if ( isEnabledAndEditorReady ) {
initializeMetaBoxes();
}
}, [ isEditorReady, hasAny, areInitialized, initializeMetaBoxes ] );
}, [ isEnabledAndEditorReady, initializeMetaBoxes ] );
};

0 comments on commit db46c07

Please sign in to comment.