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

Fix Nav block exhaustive deps warnings #48680

Merged
merged 17 commits into from
Mar 4, 2023
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
82 changes: 58 additions & 24 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ function Navigation( {

const ref = attributes.ref;

const setRef = ( postId ) => {
setAttributes( { ref: postId } );
};
const setRef = useCallback(
( postId ) => {
setAttributes( { ref: postId } );
},
[ setAttributes ]
);

const recursionId = `navigationMenu/${ ref }`;
const hasAlreadyRendered = useHasRecursion( recursionId );
Expand Down Expand Up @@ -210,6 +213,17 @@ function Navigation( {
[ navigationMenus ]
);

const handleUpdateMenu = useCallback(
( menuId, options = { focusNavigationBlock: false } ) => {
const { focusNavigationBlock } = options;
setRef( menuId );
if ( focusNavigationBlock ) {
selectBlock( clientId );
}
},
[ selectBlock, clientId, setRef ]
);

// This useEffect adds snackbar and speak status notices when menus are created.
// If there are no fallback navigation menus then we don't show these messages,
// because this means that we are creating the first, fallback navigation menu.
Expand All @@ -221,7 +235,7 @@ function Navigation( {
}

if ( createNavigationMenuIsSuccess ) {
handleUpdateMenu( createNavigationMenuPost.id, {
handleUpdateMenu( createNavigationMenuPost?.id, {
focusNavigationBlock: true,
} );

Expand All @@ -238,9 +252,13 @@ function Navigation( {
);
}
}, [
createNavigationMenuStatus,
createNavigationMenuError,
createNavigationMenuPost,
createNavigationMenuIsError,
createNavigationMenuIsSuccess,
handleUpdateMenu,
hideNavigationMenuStatusNotice,
isCreatingNavigationMenu,
showNavigationMenuStatusNotice,
createNavigationMenuPost?.id,
fallbackNavigationMenus,
] );

Expand Down Expand Up @@ -272,9 +290,11 @@ function Navigation( {
setRef( fallbackNavigationMenus[ 0 ].id );
}, [
ref,
setRef,
isCreatingNavigationMenu,
fallbackNavigationMenus,
hasUncontrolledInnerBlocks,
__unstableMarkNextChangeAsNotPersistent,
] );

const isEntityAvailable =
Expand Down Expand Up @@ -342,7 +362,16 @@ function Navigation( {
'publish'
);
}
}, [ hasResolvedNavigationMenus, hasUnsavedBlocks ] );
}, [
hasResolvedNavigationMenus,
hasUnsavedBlocks,
classicMenus,
convertClassicMenu,
createNavigationMenu,
fallbackNavigationMenus?.length,
isConvertingClassicMenu,
ref,
] );

const navRef = useRef();

Expand Down Expand Up @@ -413,17 +442,6 @@ function Navigation( {
] = useState();
const [ detectedOverlayColor, setDetectedOverlayColor ] = useState();

const handleUpdateMenu = useCallback(
( menuId, options = { focusNavigationBlock: false } ) => {
const { focusNavigationBlock } = options;
setRef( menuId );
if ( focusNavigationBlock ) {
selectBlock( clientId );
}
},
[ selectBlock, clientId ]
);

const onSelectClassicMenu = async ( classicMenu ) => {
const navMenu = await convertClassicMenu(
classicMenu.id,
Expand All @@ -449,7 +467,7 @@ function Navigation( {
}

if ( createNavigationMenuIsSuccess ) {
handleUpdateMenu( createNavigationMenuPost.id, {
handleUpdateMenu( createNavigationMenuPost?.id, {
focusNavigationBlock: true,
} );

Expand All @@ -466,7 +484,7 @@ function Navigation( {
}, [
createNavigationMenuStatus,
createNavigationMenuError,
createNavigationMenuPost,
createNavigationMenuPost?.id,
createNavigationMenuIsError,
createNavigationMenuIsSuccess,
isCreatingNavigationMenu,
Expand All @@ -492,7 +510,12 @@ function Navigation( {
__( 'Classic menu import failed.' )
);
}
}, [ classicMenuConversionStatus, classicMenuConversionError ] );
}, [
classicMenuConversionStatus,
classicMenuConversionError,
hideClassicMenuConversionNotice,
showClassicMenuConversionNotice,
] );

// Spacer block needs orientation from context. This is a patch until
// https://github.com/WordPress/gutenberg/issues/36197 is addressed.
Expand All @@ -501,7 +524,11 @@ function Navigation( {
__unstableMarkNextChangeAsNotPersistent();
setAttributes( { orientation } );
}
}, [ orientation ] );
}, [
orientation,
__unstableMarkNextChangeAsNotPersistent,
setAttributes,
] );

useEffect( () => {
if ( ! enableContrastChecking ) {
Expand Down Expand Up @@ -531,7 +558,11 @@ function Navigation( {
setDetectedOverlayBackgroundColor
);
}
} );
}, [
enableContrastChecking,
overlayTextColor.color,
overlayBackgroundColor.color,
] );

useEffect( () => {
if ( ! isSelected && ! isInnerBlockSelected ) {
Expand Down Expand Up @@ -572,6 +603,9 @@ function Navigation( {
canUserCreateNavigationMenu,
hasResolvedCanUserCreateNavigationMenu,
ref,
hideNavigationMenuPermissionsNotice,
showNavigationMenuPermissionsNotice,
navMenuResolvedButMissing,
] );

const hasManagePermissions =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ function NavigationMenuSelector( {
setIsCreatingMenu( false );
}
}, [
isCreatingMenu,
createNavigationMenuIsError,
hasResolvedNavigationMenus,
createNavigationMenuIsSuccess,
setIsCreatingMenu,
canUserCreateNavigationMenu,
createNavigationMenuIsError,
isCreatingMenu,
menuUnavailable,
noBlockMenus,
noMenuSelected,
] );

const NavigationMenuSelectorDropdown = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,40 +120,53 @@ export default function UnsavedInnerBlocks( {
const { hasResolvedNavigationMenus } = useNavigationMenu();

// Automatically save the uncontrolled blocks.
useEffect( () => {
// The block will be disabled when used in a BlockPreview.
// In this case avoid automatic creation of a wp_navigation post.
// Otherwise the user will be spammed with lots of menus!
//
// Also ensure other navigation menus have loaded so an
// accurate name can be created.
//
// Don't try saving when another save is already
// in progress.
//
// And finally only create the menu when the block is selected,
// which is an indication they want to start editing.
if (
isDisabled ||
isSaving ||
! hasResolvedDraftNavigationMenus ||
! hasResolvedNavigationMenus ||
! hasSelection ||
! innerBlocksAreDirty
) {
return;
}
useEffect(
() => {
// The block will be disabled when used in a BlockPreview.
// In this case avoid automatic creation of a wp_navigation post.
// Otherwise the user will be spammed with lots of menus!
//
// Also ensure other navigation menus have loaded so an
// accurate name can be created.
//
// Don't try saving when another save is already
// in progress.
//
// And finally only create the menu when the block is selected,
// which is an indication they want to start editing.
if (
isDisabled ||
isSaving ||
! hasResolvedDraftNavigationMenus ||
! hasResolvedNavigationMenus ||
! hasSelection ||
! innerBlocksAreDirty
) {
return;
}

createNavigationMenu( null, blocks );
}, [
createNavigationMenu,
isDisabled,
isSaving,
hasResolvedDraftNavigationMenus,
hasResolvedNavigationMenus,
innerBlocksAreDirty,
hasSelection,
] );
createNavigationMenu( null, blocks );
},
/* The dependency "blocks" is intentionally omitted here.
* This is because making blocks a dependency would cause
* createNavigationMenu to run on every block change whereas
* we only want it to run when the blocks are first detected
* as dirty.
* A better solution might be to add a hard saving lock using
* a ref to avoid having to disbale theses eslint rules.
*/
/* eslint-disable react-hooks/exhaustive-deps */
[
createNavigationMenu,
isDisabled,
isSaving,
hasResolvedDraftNavigationMenus,
hasResolvedNavigationMenus,
innerBlocksAreDirty,
hasSelection,
]
/* eslint-enable react-hooks/exhaustive-deps */
);

const Wrapper = isSaving ? Disabled : 'div';

Expand Down
Loading