* Finds the active nav item based on the current pathname * @returns the active nav item with `SingleNavItem` type or undefined if none is active
( navItems: NavItem[], pathname: string, )
| 513 | * @returns the active nav item with `SingleNavItem` type or undefined if none is active |
| 514 | */ |
| 515 | function findActiveNavItem( |
| 516 | navItems: NavItem[], |
| 517 | pathname: string, |
| 518 | ): SingleNavItem | undefined { |
| 519 | const found = navItems.find((item) => |
| 520 | item.isSingle !== false |
| 521 | ? // The current item is single, so check if the item url is active |
| 522 | isActiveRoute({ itemUrl: item.url, pathname }) |
| 523 | : // The current item is not single, so check if any of the sub items are active |
| 524 | item.items.some((item) => |
| 525 | isActiveRoute({ itemUrl: item.url, pathname }), |
| 526 | ), |
| 527 | ); |
| 528 | |
| 529 | if (found?.isSingle !== false) { |
| 530 | // The found item is single, so return it |
| 531 | return found; |
| 532 | } |
| 533 | |
| 534 | // The found item is not single, so find the active sub item |
| 535 | return found?.items.find((item) => |
| 536 | isActiveRoute({ itemUrl: item.url, pathname }), |
| 537 | ); |
| 538 | } |
| 539 | |
| 540 | interface Props { |
| 541 | children: React.ReactNode; |