From 0c9963b52b59eb1d563b10a3f1db5f011ddc740f Mon Sep 17 00:00:00 2001 From: scruffian Date: Tue, 27 Jun 2023 13:36:29 +0100 Subject: [PATCH 1/4] Navigation: Load navigations using so that we only need to preload one request --- .../block-library/src/navigation/constants.js | 14 +++--- .../src/navigation/use-navigation-menu.js | 49 ++++++++----------- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/packages/block-library/src/navigation/constants.js b/packages/block-library/src/navigation/constants.js index 5e6b26ae40b0f4..ff6ac3de9b45e9 100644 --- a/packages/block-library/src/navigation/constants.js +++ b/packages/block-library/src/navigation/constants.js @@ -20,13 +20,15 @@ export const PRIORITIZED_INSERTER_BLOCKS = [ 'core/navigation-link', ]; +export const PRELOADED_NAVIGATION_MENUS_QUERY = { + per_page: 100, + status: [ 'publish', 'draft' ], + order: 'desc', + orderby: 'date', +}; + export const SELECT_NAVIGATION_MENUS_ARGS = [ 'postType', 'wp_navigation', - { - per_page: 100, - status: [ 'publish', 'draft' ], - order: 'desc', - orderby: 'date', - }, + PRELOADED_NAVIGATION_MENUS_QUERY, ]; diff --git a/packages/block-library/src/navigation/use-navigation-menu.js b/packages/block-library/src/navigation/use-navigation-menu.js index 30e5cdd99c0924..26f5bae0fd4e16 100644 --- a/packages/block-library/src/navigation/use-navigation-menu.js +++ b/packages/block-library/src/navigation/use-navigation-menu.js @@ -4,18 +4,19 @@ import { store as coreStore, useResourcePermissions, + useEntityRecords, } from '@wordpress/core-data'; import { useSelect } from '@wordpress/data'; /** * Internal dependencies */ -import { SELECT_NAVIGATION_MENUS_ARGS } from './constants'; +import { PRELOADED_NAVIGATION_MENUS_QUERY } from './constants'; export default function useNavigationMenu( ref ) { const permissions = useResourcePermissions( 'navigation', ref ); - return useSelect( + const useSelectResult = useSelect( ( select ) => { const { canCreate, @@ -25,12 +26,6 @@ export default function useNavigationMenu( ref ) { hasResolved, } = permissions; - const { - navigationMenus, - isResolvingNavigationMenus, - hasResolvedNavigationMenus, - } = selectNavigationMenus( select ); - const { navigationMenu, isNavigationMenuResolved, @@ -38,18 +33,10 @@ export default function useNavigationMenu( ref ) { } = selectExistingMenu( select, ref ); return { - navigationMenus, - isResolvingNavigationMenus, - hasResolvedNavigationMenus, - navigationMenu, isNavigationMenuResolved, isNavigationMenuMissing, - canSwitchNavigationMenu: ref - ? navigationMenus?.length > 1 - : navigationMenus?.length > 0, - canUserCreateNavigationMenu: canCreate, isResolvingCanUserCreateNavigationMenu: isResolving, hasResolvedCanUserCreateNavigationMenu: hasResolved, @@ -67,22 +54,26 @@ export default function useNavigationMenu( ref ) { }, [ ref, permissions ] ); -} -function selectNavigationMenus( select ) { - const { getEntityRecords, hasFinishedResolution, isResolving } = - select( coreStore ); + const { + records: navigationMenus, + isResolving: isResolvingNavigationMenus, + hasResolved: hasResolvedNavigationMenus, + canSwitchNavigationMenu = ref + ? navigationMenus?.length > 1 + : navigationMenus?.length > 0, + } = useEntityRecords( + 'postType', + `wp_navigation`, + PRELOADED_NAVIGATION_MENUS_QUERY + ); return { - navigationMenus: getEntityRecords( ...SELECT_NAVIGATION_MENUS_ARGS ), - isResolvingNavigationMenus: isResolving( - 'getEntityRecords', - SELECT_NAVIGATION_MENUS_ARGS - ), - hasResolvedNavigationMenus: hasFinishedResolution( - 'getEntityRecords', - SELECT_NAVIGATION_MENUS_ARGS - ), + ...useSelectResult, + navigationMenus, + isResolvingNavigationMenus, + hasResolvedNavigationMenus, + canSwitchNavigationMenu, }; } From e7ef6dad10df7d1bf876ca0b4baa5801831ea9ba Mon Sep 17 00:00:00 2001 From: scruffian Date: Tue, 4 Jul 2023 13:47:14 +0100 Subject: [PATCH 2/4] remove permissions from the useSelect --- .../src/navigation/use-navigation-menu.js | 41 +++++++------------ 1 file changed, 15 insertions(+), 26 deletions(-) diff --git a/packages/block-library/src/navigation/use-navigation-menu.js b/packages/block-library/src/navigation/use-navigation-menu.js index 26f5bae0fd4e16..9ee8ac2e9072aa 100644 --- a/packages/block-library/src/navigation/use-navigation-menu.js +++ b/packages/block-library/src/navigation/use-navigation-menu.js @@ -18,14 +18,6 @@ export default function useNavigationMenu( ref ) { const useSelectResult = useSelect( ( select ) => { - const { - canCreate, - canUpdate, - canDelete, - isResolving, - hasResolved, - } = permissions; - const { navigationMenu, isNavigationMenuResolved, @@ -36,44 +28,41 @@ export default function useNavigationMenu( ref ) { navigationMenu, isNavigationMenuResolved, isNavigationMenuMissing, - - canUserCreateNavigationMenu: canCreate, - isResolvingCanUserCreateNavigationMenu: isResolving, - hasResolvedCanUserCreateNavigationMenu: hasResolved, - - canUserUpdateNavigationMenu: canUpdate, - hasResolvedCanUserUpdateNavigationMenu: ref - ? hasResolved - : undefined, - - canUserDeleteNavigationMenu: canDelete, - hasResolvedCanUserDeleteNavigationMenu: ref - ? hasResolved - : undefined, }; }, - [ ref, permissions ] + [ ref ] ); + const { canCreate, canUpdate, canDelete, isResolving, hasResolved } = + permissions; + const { records: navigationMenus, isResolving: isResolvingNavigationMenus, hasResolved: hasResolvedNavigationMenus, - canSwitchNavigationMenu = ref - ? navigationMenus?.length > 1 - : navigationMenus?.length > 0, } = useEntityRecords( 'postType', `wp_navigation`, PRELOADED_NAVIGATION_MENUS_QUERY ); + const canSwitchNavigationMenu = ref + ? navigationMenus?.length > 1 + : navigationMenus?.length > 0; + return { ...useSelectResult, navigationMenus, isResolvingNavigationMenus, hasResolvedNavigationMenus, canSwitchNavigationMenu, + canUserCreateNavigationMenu: canCreate, + isResolvingCanUserCreateNavigationMenu: isResolving, + hasResolvedCanUserCreateNavigationMenu: hasResolved, + canUserUpdateNavigationMenu: canUpdate, + hasResolvedCanUserUpdateNavigationMenu: ref ? hasResolved : undefined, + canUserDeleteNavigationMenu: canDelete, + hasResolvedCanUserDeleteNavigationMenu: ref ? hasResolved : undefined, }; } From 65fd4d2fea49124e567f2c7f5b3518c4236d29cd Mon Sep 17 00:00:00 2001 From: scruffian Date: Thu, 13 Jul 2023 15:08:56 +0100 Subject: [PATCH 3/4] Add comments about what constants to keep in sync with each other --- packages/block-library/src/navigation/constants.js | 4 ++++ .../sidebar-navigation-screen-navigation-menus/constants.js | 2 ++ 2 files changed, 6 insertions(+) diff --git a/packages/block-library/src/navigation/constants.js b/packages/block-library/src/navigation/constants.js index ff6ac3de9b45e9..07d71d50dd98ce 100644 --- a/packages/block-library/src/navigation/constants.js +++ b/packages/block-library/src/navigation/constants.js @@ -20,6 +20,10 @@ export const PRIORITIZED_INSERTER_BLOCKS = [ 'core/navigation-link', ]; +// These parameters must be kept aligned with those in +// lib/compat/wordpress-6.3/navigation-block-preloading.php +// and +// edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js export const PRELOADED_NAVIGATION_MENUS_QUERY = { per_page: 100, status: [ 'publish', 'draft' ], diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js index ab2e6e9cefa333..643a452901b541 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/constants.js @@ -3,6 +3,8 @@ // on apiFetch this query is limited to 100. // These parameters must be kept aligned with those in // lib/compat/wordpress-6.3/navigation-block-preloading.php +// and +// block-library/src/navigation/constants.js export const PRELOADED_NAVIGATION_MENUS_QUERY = { per_page: 100, status: [ 'publish', 'draft' ], From 66927ec63c41a95c3a6783d7f5f3872c97d4f74e Mon Sep 17 00:00:00 2001 From: scruffian Date: Thu, 13 Jul 2023 17:01:16 +0100 Subject: [PATCH 4/4] extract vars --- .../src/navigation/use-navigation-menu.js | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/packages/block-library/src/navigation/use-navigation-menu.js b/packages/block-library/src/navigation/use-navigation-menu.js index 9ee8ac2e9072aa..02df0ba2831dcc 100644 --- a/packages/block-library/src/navigation/use-navigation-menu.js +++ b/packages/block-library/src/navigation/use-navigation-menu.js @@ -16,19 +16,13 @@ import { PRELOADED_NAVIGATION_MENUS_QUERY } from './constants'; export default function useNavigationMenu( ref ) { const permissions = useResourcePermissions( 'navigation', ref ); - const useSelectResult = useSelect( + const { + navigationMenu, + isNavigationMenuResolved, + isNavigationMenuMissing, + } = useSelect( ( select ) => { - const { - navigationMenu, - isNavigationMenuResolved, - isNavigationMenuMissing, - } = selectExistingMenu( select, ref ); - - return { - navigationMenu, - isNavigationMenuResolved, - isNavigationMenuMissing, - }; + return selectExistingMenu( select, ref ); }, [ ref ] ); @@ -51,7 +45,9 @@ export default function useNavigationMenu( ref ) { : navigationMenus?.length > 0; return { - ...useSelectResult, + navigationMenu, + isNavigationMenuResolved, + isNavigationMenuMissing, navigationMenus, isResolvingNavigationMenus, hasResolvedNavigationMenus,