Skip to content

Commit

Permalink
Perf: avoid fetching all reusable blocks on load in patterns preferen…
Browse files Browse the repository at this point in the history
…ce modal (WordPress#66621)

Avoid fetching block patterns when the site editor loads. Move useStartPatterns() hook to a wrapper and return early if the preferences modal is not active. Fix JS unit tests

---------

Co-authored-by: ellatrix <[email protected]>
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: andrewserong <[email protected]>
  • Loading branch information
4 people authored and karthick-murugan committed Nov 13, 2024
1 parent eaf0216 commit 5e4e303
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
45 changes: 26 additions & 19 deletions packages/editor/src/components/preferences-modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,40 @@ const {
} = unlock( preferencesPrivateApis );

export default function EditorPreferencesModal( { extraSections = {} } ) {
const isActive = useSelect( ( select ) => {
return select( interfaceStore ).isModalActive( 'editor/preferences' );
}, [] );
const { closeModal } = useDispatch( interfaceStore );

if ( ! isActive ) {
return null;
}

// Please wrap all contents inside PreferencesModalContents to prevent all
// hooks from executing when the modal is not open.
return (
<PreferencesModal closeModal={ closeModal }>
<PreferencesModalContents extraSections={ extraSections } />
</PreferencesModal>
);
}

function PreferencesModalContents( { extraSections = {} } ) {
const isLargeViewport = useViewportMatch( 'medium' );
const { isActive, showBlockBreadcrumbsOption } = useSelect(
const showBlockBreadcrumbsOption = useSelect(
( select ) => {
const { getEditorSettings } = select( editorStore );
const { get } = select( preferencesStore );
const { isModalActive } = select( interfaceStore );
const isRichEditingEnabled = getEditorSettings().richEditingEnabled;
const isDistractionFreeEnabled = get( 'core', 'distractionFree' );
return {
showBlockBreadcrumbsOption:
! isDistractionFreeEnabled &&
isLargeViewport &&
isRichEditingEnabled,
isActive: isModalActive( 'editor/preferences' ),
};
return (
! isDistractionFreeEnabled &&
isLargeViewport &&
isRichEditingEnabled
);
},
[ isLargeViewport ]
);
const { closeModal } = useDispatch( interfaceStore );
const { setIsListViewOpened, setIsInserterOpened } =
useDispatch( editorStore );
const { set: setPreference } = useDispatch( preferencesStore );
Expand Down Expand Up @@ -330,13 +345,5 @@ export default function EditorPreferencesModal( { extraSections = {} } ) {
]
);

if ( ! isActive ) {
return null;
}

return (
<PreferencesModal closeModal={ closeModal }>
<PreferencesModalTabs sections={ sections } />
</PreferencesModal>
);
return <PreferencesModalTabs sections={ sections } />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jest.mock( '@wordpress/compose/src/hooks/use-viewport-match', () => jest.fn() );

describe( 'EditPostPreferencesModal', () => {
it( 'should not render when the modal is not active', () => {
useSelect.mockImplementation( () => [ false, false, false ] );
useSelect.mockImplementation( () => false );
render( <EditPostPreferencesModal /> );
expect(
screen.queryByRole( 'dialog', { name: 'Preferences' } )
Expand Down

0 comments on commit 5e4e303

Please sign in to comment.