import React, {useEffect, useState} from 'react';
import {Link, useIntl} from 'gatsby-plugin-intl';
import {
Alert,
Header,
NavMenuButton,
PrimaryNav,
Grid,
NavDropDownButton,
Menu,
} from '@trussworks/react-uswds';
import BetaBanner from '../BetaBanner';
import J40MainGridContainer from '../J40MainGridContainer';
import GovernmentBanner from '../GovernmentBanner';
// import Language from '../Language';
import {useWindowSize} from 'react-use';
// @ts-ignore
import siteLogo from '../../images/j40-logo-v2.png';
import * as styles from './J40Header.module.scss';
import * as COMMON_COPY from '../../data/copy/common';
import {PAGES_ENDPOINTS, USWDS_BREAKPOINTS} from '../../data/constants';
interface IJ40Header {
location: Location
}
/**
* The J40Header component will control how the header looks for both mobile and desktop
*
* The Header is defined as
* 1. Two rows of Banners (ie, official gov website and beta site)
* 2. Logo and Nav Links Row
* 3. Any Alerts
*
* @param {Location} location
* @return {JSX.Element}
*/
const J40Header = ({location}:IJ40Header) => {
const intl = useIntl();
// grab last segment of location pathname
const [lastSegmentLocation] = location.pathname.split('/').slice(-1);
// Logo text
const logoLine1 = intl.formatMessage(COMMON_COPY.HEADER.TITLE_LINE_1);
/**
* State variable to control the toggling of mobile menu button
*/
const [mobileNavOpen, setMobileNavOpen] = useState(false);
const toggleMobileNav = (): void => {
setMobileNavOpen((prevOpen) => !prevOpen);
};
/**
* State variable to hold the open/close state of each nav dropdown. This will allow for two
* dropdown that are being used, each corresponding to an index in the state array:
*
* index 0 = Data & Methodology dropdown
* index 1 = About dropdown
*/
const [isOpen, setIsOpen] = useState([false, false]);
/**
* When transitioning between anything larger than desktop and anything less than desktop the nav menu
* changes from the usual row of nav links (dektop) to the MENU button(mobile). On desktop all nav drop
* dropdowns should be closed, while on mobile all nav links should be open.
*
* The useWindowSize provides the device width and the useEffect allows the side effect (opening/closing
* nav links) to occur anytime the device width changes.
*/
const {width} = useWindowSize();
useEffect( () => {
if (width < USWDS_BREAKPOINTS.DESKTOP) {
setIsOpen([true, true]);
} else {
setIsOpen([false, false]);
}
}, [width]);
/**
* This toggle function will handle both navigation toggle links (Meth and About).
*
* @param {number} index
*/
const onToggle = (index: number): void => {
// The setIsOpen is used to toggle the currently selected nav link
setIsOpen((prevIsOpen) => {
const newIsOpen = [...isOpen];
newIsOpen[index] = !prevIsOpen[index];
return newIsOpen;
});
/**
* When on desktop only, the dropdown nav links (Meth and About) should close if any of the other ones
* are still open. This next set of logic handles that.
*/
if (index === 0 && isOpen[1] === true && width > USWDS_BREAKPOINTS.DESKTOP) {
setIsOpen([isOpen[0], false]);
} else if (index === 1 && isOpen[0] === true && width > USWDS_BREAKPOINTS.DESKTOP) {
setIsOpen([false, isOpen[1]]);
}
};
/**
* On mobile, the Methodology & Data page should have two sub-nav links. This defines
* the array that will hold these links
*/
const methPageSubNavLinks = [
{intl.formatMessage(COMMON_COPY.HEADER.METHODOLOGY)}
,
{intl.formatMessage(COMMON_COPY.HEADER.DOWNLOADS)}
,
{intl.formatMessage(COMMON_COPY.HEADER.PREVIOUS_VERSIONS)}
,
];
/**
* On mobile, the About page should have 3 sub-nav links. This defines
* the array that will hold these links
*/
const aboutPageSubNavLinks = [
{intl.formatMessage(COMMON_COPY.HEADER.ABOUT)}
,
{intl.formatMessage(COMMON_COPY.HEADER.PUBLIC_ENG)}
,
{intl.formatMessage(COMMON_COPY.HEADER.FAQS)}
,
];
// Methodology & Data Nav component
const MethNav = () =>
<>
{/* Add a className of usa-current anytime this component renders when the location of the app is on
the Methodology page or the downloads page. This will style the nav link with a bottom border */}