Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preference modal: avoid fetching all reusable blocks when the site editor loads #66621

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading