/** * External dependencies */ import { __ } from '@wordpress/i18n'; import { useEffect, useMemo, useState, useRef } from '@wordpress/element'; import classnames from 'classnames'; import { compose } from '@wordpress/compose'; import { Navigation, NavigationMenu, NavigationGroup, } from '@woocommerce/experimental'; import { NAVIGATION_STORE_NAME } from '@woocommerce/data'; import { recordEvent } from '@woocommerce/tracks'; import { withSelect } from '@wordpress/data'; /** * Internal dependencies */ import { addHistoryListener, getMatchingItem } from '../../utils'; import CategoryTitle from '../category-title'; import Header from '../header'; import Item from '../../components/Item'; const Container = ( { menuItems } ) => { useEffect( () => { // Collapse the original WP Menu. document.documentElement.classList.remove( 'wp-toolbar' ); const adminMenu = document.getElementById( 'adminmenumain' ); if ( ! adminMenu ) { return; } adminMenu.classList.add( 'folded' ); }, [] ); const { rootBackLabel, rootBackUrl } = window.wcNavigation; const parentCategory = { capability: 'manage_woocommerce', id: 'woocommerce', isCategory: true, menuId: 'primary', migrate: true, order: 10, parent: '', title: 'WooCommerce', }; const categoriesMap = menuItems.reduce( ( acc, item ) => { if ( item.isCategory ) { return { ...acc, [ item.id ]: item }; } return acc; }, { woocommerce: parentCategory, } ); const categories = Object.values( categoriesMap ); const [ activeItem, setActiveItem ] = useState( 'woocommerce-home' ); const [ activeLevel, setActiveLevel ] = useState( 'woocommerce' ); useEffect( () => { const initialMatchedItem = getMatchingItem( menuItems ); if ( initialMatchedItem ) { setActiveItem( initialMatchedItem ); setActiveLevel( initialMatchedItem.parent ); } const removeListener = addHistoryListener( () => { setTimeout( () => { const matchedItem = getMatchingItem( menuItems ); if ( matchedItem ) { setActiveItem( matchedItem ); } }, 0 ); } ); return removeListener; }, [ menuItems ] ); const getMenuItemsByCategory = ( items ) => { return items.reduce( ( acc, item ) => { // Set up the category if it doesn't yet exist. if ( ! acc[ item.parent ] ) { acc[ item.parent ] = {}; } // Check if parent category is in the same menu. if ( item.parent !== 'woocommerce' && categoriesMap[ item.parent ] && categoriesMap[ item.parent ].menuId !== item.menuId ) { return acc; } // Create the menu object if it doesn't exist in this category. if ( ! acc[ item.parent ][ item.menuId ] ) { acc[ item.parent ][ item.menuId ] = []; } acc[ item.parent ][ item.menuId ].push( item ); return acc; }, {} ); }; const categorizedItems = useMemo( () => getMenuItemsByCategory( menuItems ), [ menuItems ] ); const navDomRef = useRef( null ); const trackBackClick = ( id ) => { recordEvent( 'navigation_back_click', { category: id, } ); }; const isRoot = activeLevel === 'woocommerce'; const isRootBackVisible = isRoot && rootBackUrl; const classes = classnames( 'woocommerce-navigation', { 'is-root': isRoot, } ); return (