From 07145edf3a6afc476c44e1f79798e5f91099e89b Mon Sep 17 00:00:00 2001
From: Ella <4710635+ellatrix@users.noreply.github.com>
Date: Wed, 30 Oct 2024 20:15:02 +0100
Subject: [PATCH 1/2] Perf: avoid fetching all reusable blocks on load (again)
---
.../src/components/preferences-modal/index.js | 45 +++++++++++--------
1 file changed, 26 insertions(+), 19 deletions(-)
diff --git a/packages/editor/src/components/preferences-modal/index.js b/packages/editor/src/components/preferences-modal/index.js
index a8cfd8245522c..7ea7ea456ce28 100644
--- a/packages/editor/src/components/preferences-modal/index.js
+++ b/packages/editor/src/components/preferences-modal/index.js
@@ -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 (
+
+
+
+ );
+}
+
+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 );
@@ -330,13 +345,5 @@ export default function EditorPreferencesModal( { extraSections = {} } ) {
]
);
- if ( ! isActive ) {
- return null;
- }
-
- return (
-
-
-
- );
+ return ;
}
From 74571e77655984edf77cf65bf5aa3418f634629e Mon Sep 17 00:00:00 2001
From: ramon
Date: Thu, 31 Oct 2024 10:08:23 +1100
Subject: [PATCH 2/2] Fix JS unit test
---
packages/editor/src/components/preferences-modal/test/index.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/packages/editor/src/components/preferences-modal/test/index.js b/packages/editor/src/components/preferences-modal/test/index.js
index 01ac1a88fbe7d..70102eea40f2e 100644
--- a/packages/editor/src/components/preferences-modal/test/index.js
+++ b/packages/editor/src/components/preferences-modal/test/index.js
@@ -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( );
expect(
screen.queryByRole( 'dialog', { name: 'Preferences' } )