import React, { useContext, useState, useRef } from 'react';
import PropTypes from 'prop-types';
import browser from 'webextension-polyfill';
import { useDispatch, useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import {
MetaMetricsEventCategory,
MetaMetricsEventName,
} from '../../../../shared/constants/metametrics';
import {
CONNECTED_ACCOUNTS_ROUTE,
DEFAULT_ROUTE,
} from '../../../helpers/constants/routes';
import {
AlignItems,
BackgroundColor,
BLOCK_SIZES,
DISPLAY,
JustifyContent,
Size,
} from '../../../helpers/constants/design-system';
import {
AvatarNetwork,
Button,
ButtonIcon,
IconName,
PickerNetwork,
} from '../../component-library';
import {
getCurrentNetwork,
getOriginOfCurrentTab,
getSelectedIdentity,
} from '../../../selectors';
import { GlobalMenu, AccountPicker } from '..';
import Box from '../../ui/box/box';
import { toggleAccountMenu, toggleNetworkMenu } from '../../../store/actions';
import MetafoxLogo from '../../ui/metafox-logo';
import { getEnvironmentType } from '../../../../app/scripts/lib/util';
import { ENVIRONMENT_TYPE_POPUP } from '../../../../shared/constants/app';
import ConnectedStatusIndicator from '../../app/connected-status-indicator';
export const AppHeader = ({ onClick }) => {
const trackEvent = useContext(MetaMetricsContext);
const [accountOptionsMenuOpen, setAccountOptionsMenuOpen] = useState(false);
const menuRef = useRef(false);
const origin = useSelector(getOriginOfCurrentTab);
const history = useHistory();
const isUnlocked = useSelector((state) => state.metamask.isUnlocked);
// Used for account picker
const identity = useSelector(getSelectedIdentity);
const dispatch = useDispatch();
// Used for network icon / dropdown
const currentNetwork = useSelector(getCurrentNetwork);
// used to get the environment and connection status
const popupStatus = getEnvironmentType() === ENVIRONMENT_TYPE_POPUP;
const showStatus =
getEnvironmentType() === ENVIRONMENT_TYPE_POPUP &&
origin &&
origin !== browser.runtime.id;
return (
<>
{isUnlocked && !popupStatus ? (
{
if (onClick) {
await onClick();
}
history.push(DEFAULT_ROUTE);
}}
/>
) : null}
<>
{isUnlocked ? (
{popupStatus ? (
) : (
dispatch(toggleNetworkMenu())}
/>
)}
dispatch(toggleAccountMenu())}
/>
{showStatus ? (
history.push(CONNECTED_ACCOUNTS_ROUTE)}
/>
) : null}
{
trackEvent({
event: MetaMetricsEventName.NavAccountMenuOpened,
category: MetaMetricsEventCategory.Navigation,
properties: {
location: 'Home',
},
});
setAccountOptionsMenuOpen(true);
}}
/>
{accountOptionsMenuOpen ? (
setAccountOptionsMenuOpen(false)}
/>
) : null}
) : (
dispatch(toggleNetworkMenu())}
/>
{
if (onClick) {
await onClick();
}
history.push(DEFAULT_ROUTE);
}}
/>
)}
>
>
);
};
AppHeader.propTypes = {
/**
* The onClick handler to be passed to the MetaMask Logo in the App Header
*/
onClick: PropTypes.func,
};