import React, { SyntheticEvent, Component, Fragment, ReactElement } from 'react'; import { Link } from 'react-router-dom'; import { css } from 'emotion'; import Button from '@material-ui/core/Button'; import IconButton from '@material-ui/core/IconButton'; import MenuItem from '@material-ui/core/MenuItem'; import Menu from '@material-ui/core/Menu'; import Info from '@material-ui/icons/Info'; import Help from '@material-ui/icons/Help'; import Tooltip from '@material-ui/core/Tooltip'; import AccountCircle from '@material-ui/icons/AccountCircle'; import { default as IconSearch } from '@material-ui/icons/Search'; import { getRegistryURL } from '../../utils/url'; import ExternalLink from '../Link'; import Logo from '../Logo'; import RegistryInfoDialog from '../RegistryInfoDialog/RegistryInfoDialog'; import Label from '../Label/Label'; import Search from '../Search/Search'; import RegistryInfoContent from '../RegistryInfoContent/RegistryInfoContent'; import { Greetings, NavBar, InnerNavBar, MobileNavBar, InnerMobileNavBar, LeftSide, RightSide, IconSearchButton, SearchWrapper } from './styles'; interface Props { logo?: string; username?: string; onLogout: () => void; onToggleLoginModal: () => void; scope: string; withoutSearch?: boolean; } interface State { anchorEl?: null | Element | ((element: Element) => Element); openInfoDialog: boolean; registryUrl: string; showMobileNavBar: boolean; } type ToolTipType = 'search' | 'help' | 'info'; class Header extends Component { constructor(props: Props) { super(props); this.state = { openInfoDialog: false, registryUrl: getRegistryURL(), showMobileNavBar: false, }; } public render(): ReactElement { const { showMobileNavBar } = this.state; const { withoutSearch = false } = this.props; return (
{this.renderLeftSide()} {this.renderRightSide()} {this.renderInfoDialog()} {showMobileNavBar && !withoutSearch && ( )}
); } /** * opens popover menu for logged in user. */ public handleLoggedInMenu = (event: SyntheticEvent) => { this.setState({ anchorEl: event.currentTarget, }); }; /** * closes popover menu for logged in user */ public handleLoggedInMenuClose = () => { this.setState({ anchorEl: null, }); }; /** * opens registry information dialog. */ public handleOpenRegistryInfoDialog = () => { this.setState({ openInfoDialog: true, }); }; /** * closes registry information dialog. */ public handleCloseRegistryInfoDialog = () => { this.setState({ openInfoDialog: false, }); }; /** * close/open popover menu for logged in users. */ public handleToggleLogin = () => { const { onToggleLoginModal } = this.props; this.setState( { anchorEl: null, }, onToggleLoginModal ); }; public handleToggleMNav = () => { const { showMobileNavBar } = this.state; this.setState({ showMobileNavBar: !showMobileNavBar, }); }; public handleDismissMNav = () => { this.setState({ showMobileNavBar: false, }); }; public renderLeftSide = () => { const { withoutSearch = false } = this.props; return ( {this.renderLogo()} {!withoutSearch && ( )} ); }; public renderLogo = () => { const { logo } = this.props; if (logo) { return logo; } else { return ; } }; public renderToolTipIcon = (title: string, type: ToolTipType) => { let content; switch (type) { case 'help': content = ( // @ts-ignore ); break; case 'info': content = ( ); break; case 'search': content = ( ); break; } return ( {content} ); }; public renderRightSide = () => { const { username = '', withoutSearch = false } = this.props; return ( {!withoutSearch && this.renderToolTipIcon('Search packages', 'search')} {this.renderToolTipIcon('Documentation', 'help')} {this.renderToolTipIcon('Registry Information', 'info')} {username ? ( this.renderMenu() ) : ( )} ); }; private renderGreetings = () => { const { username = '' } = this.props; return ( {'Hi,'} ); }; /** * render popover menu */ private renderMenu = () => { const { onLogout } = this.props; const { anchorEl } = this.state; const open = Boolean(anchorEl); return ( <> {this.renderGreetings()} {'Logout'} ); }; private renderInfoDialog = () => { const { scope } = this.props; const { openInfoDialog, registryUrl } = this.state; return ( ); }; } export default Header;