src/app/legacy/containers/Navigation/index.jsx
import React, { useContext } from 'react';
import { NavigationUl, NavigationLi } from '#psammead/psammead-navigation/src';
import {
DropdownUl,
DropdownLi,
} from '#psammead/psammead-navigation/src/DropdownNavigation';
import useClickTrackerHandler from '#app/hooks/useClickTrackerHandler';
import useViewTracker from '#app/hooks/useViewTracker';
import { RequestContext } from '#contexts/RequestContext';
import { ServiceContext } from '../../../contexts/ServiceContext';
import Canonical from './index.canonical';
import Amp from './index.amp';
const renderListItems = (
Li,
navigation,
script,
currentPage,
service,
dir,
activeIndex,
clickTrackerHandler,
viewRef,
) =>
navigation.map((item, index) => {
const { title, url } = item;
const active = index === activeIndex;
return (
<Li
key={title}
url={url}
script={script}
active={active}
currentPageText={currentPage}
service={service}
dir={dir}
clickTrackerHandler={clickTrackerHandler}
viewRef={viewRef}
>
{title}
</Li>
);
});
const NavigationContainer = () => {
const { isAmp } = useContext(RequestContext);
const { script, translations, navigation, service, dir } =
useContext(ServiceContext);
const { canonicalLink, origin } = useContext(RequestContext);
const { currentPage, navMenuText } = translations;
const scrollableNavEventTrackingData = {
componentName: `scrollable-navigation`,
};
const dropdownNavEventTrackingData = {
componentName: `dropdown-navigation`,
};
const scrollableNavClickTrackerHandler = useClickTrackerHandler(
scrollableNavEventTrackingData,
);
const dropdownNavClickTrackerHandler = useClickTrackerHandler(
dropdownNavEventTrackingData,
);
const scrollableNavViewRef = useViewTracker(scrollableNavEventTrackingData);
const dropdownNavViewRef = useViewTracker(dropdownNavEventTrackingData);
if (!navigation || navigation.length === 0) {
return null;
}
const activeIndex = navigation.findIndex(
link => `${origin}${link.url}` === canonicalLink,
);
const scrollableListItems = (
<NavigationUl>
{renderListItems(
NavigationLi,
navigation,
script,
currentPage,
service,
dir,
activeIndex,
scrollableNavClickTrackerHandler,
scrollableNavViewRef,
)}
</NavigationUl>
);
const dropdownListItems = (
<DropdownUl>
{renderListItems(
DropdownLi,
navigation,
script,
currentPage,
service,
dir,
activeIndex,
dropdownNavClickTrackerHandler,
dropdownNavViewRef,
)}
</DropdownUl>
);
const Navigation = isAmp ? Amp : Canonical;
return (
<Navigation
scrollableListItems={scrollableListItems}
dropdownListItems={dropdownListItems}
menuAnnouncedText={navMenuText}
dir={dir}
script={script}
service={service}
/>
);
};
export default NavigationContainer;