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 (#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 Oct 30, 2024
1 parent fb7b17a commit ed178c3
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

1 comment on commit ed178c3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in ed178c3.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11603418412
📝 Reported issues:

Please sign in to comment.