From 74232f718ec65747f4817b0fb70deca5f9440b8a Mon Sep 17 00:00:00 2001 From: Abdul Rahman Date: Thu, 9 Apr 2026 07:11:27 +0530 Subject: [PATCH] Refactor navigation menu item open state logic - Simplified the logic for determining the first non-link item in the navigation menu, improving readability and maintainability. - Updated the active navigation item checks to enhance clarity, replacing the previous method with a more straightforward function for matching navigation items to the active object. - Improved the handling of selected navigation menu item index to reflect the updated logic for active navigation items. These changes contribute to a clearer and more efficient navigation handling within the component. --- .../useNavigationMenuItemFolderOpenState.ts | 59 ++++++++++--------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/packages/twenty-front/src/modules/navigation-menu-item/display/folder/hooks/useNavigationMenuItemFolderOpenState.ts b/packages/twenty-front/src/modules/navigation-menu-item/display/folder/hooks/useNavigationMenuItemFolderOpenState.ts index 09e9d6ffb10..f35dbdba746 100644 --- a/packages/twenty-front/src/modules/navigation-menu-item/display/folder/hooks/useNavigationMenuItemFolderOpenState.ts +++ b/packages/twenty-front/src/modules/navigation-menu-item/display/folder/hooks/useNavigationMenuItemFolderOpenState.ts @@ -57,25 +57,18 @@ export const useNavigationMenuItemFolderOpenState = ({ } if (!isOpen) { - const firstNonLinkItem = navigationMenuItems.find((item) => { + for (const item of navigationMenuItems) { if (item.type === NavigationMenuItemType.LINK) { - return false; + continue; } - const computedLink = getNavigationMenuItemComputedLink( - item, - objectMetadataItems, - views, - ); - return isNonEmptyString(computedLink); - }); - if (isDefined(firstNonLinkItem)) { const link = getNavigationMenuItemComputedLink( - firstNonLinkItem, + item, objectMetadataItems, views, ); if (isNonEmptyString(link)) { navigate(link); + break; } } } @@ -89,23 +82,35 @@ export const useNavigationMenuItemFolderOpenState = ({ ) : -1; - const isAtomRelevantToFolder = - isDefined(activeNavigationItem) && - navigationMenuItems.some((item) => { - const objectMetadataItem = getObjectMetadataForNavigationMenuItem( - item, - objectMetadataItems, - views, - ); - return ( - isDefined(objectMetadataItem) && - objectMetadataItem.nameSingular === - activeNavigationItem.objectNameSingular - ); - }); + const activeNavigationObjectNameSingular = + activeNavigationItem?.objectNameSingular; + + const navigationMenuItemMatchesActiveObject = ( + navigationMenuItem: NavigationMenuItem, + ): boolean => { + const objectMetadataItem = getObjectMetadataForNavigationMenuItem( + navigationMenuItem, + objectMetadataItems, + views, + ); + + if (!isDefined(objectMetadataItem)) { + return false; + } + + return ( + objectMetadataItem.nameSingular === activeNavigationObjectNameSingular + ); + }; + + const isActiveNavigationItemObjectInFolder = + isDefined(activeNavigationObjectNameSingular) && + navigationMenuItems.some(navigationMenuItemMatchesActiveObject); const recordMatchIndex = navigationMenuItems.findIndex((item) => { - if (item.type !== NavigationMenuItemType.RECORD) return false; + if (item.type !== NavigationMenuItemType.RECORD) { + return false; + } const computedLink = getNavigationMenuItemComputedLink( item, objectMetadataItems, @@ -149,7 +154,7 @@ export const useNavigationMenuItemFolderOpenState = ({ return false; }); - const selectedNavigationMenuItemIndex = isAtomRelevantToFolder + const selectedNavigationMenuItemIndex = isActiveNavigationItemObjectInFolder ? explicitMatchIndex !== -1 ? explicitMatchIndex : recordMatchIndex