import { useQuery } from '@apollo/react-hooks' import ClickAwayListener from '@material-ui/core/ClickAwayListener' import Popper from '@material-ui/core/Popper' import { makeStyles } from '@material-ui/core/styles' import classnames from 'classnames' import gql from 'graphql-tag' import * as R from 'ramda' import React, { memo, useState, useEffect, useRef } from 'react' import { NavLink, useHistory } from 'react-router-dom' import NotificationCenter from 'src/components/NotificationCenter' import ActionButton from 'src/components/buttons/ActionButton' import { H4 } from 'src/components/typography' import AddMachine from 'src/pages/AddMachine' import { ReactComponent as AddIconReverse } from 'src/styling/icons/button/add/white.svg' import { ReactComponent as AddIcon } from 'src/styling/icons/button/add/zodiac.svg' import { ReactComponent as Logo } from 'src/styling/icons/menu/logo.svg' import { ReactComponent as NotificationIcon } from 'src/styling/icons/menu/notification.svg' import styles from './Header.styles' const useStyles = makeStyles(styles) const HAS_UNREAD = gql` query getUnread { hasUnreadNotifications } ` const Subheader = ({ item, classes }) => { const [prev, setPrev] = useState(null) return (
) } const notNil = R.compose(R.not, R.isNil) const Header = memo(({ tree, user }) => { const [open, setOpen] = useState(false) const [anchorEl, setAnchorEl] = useState(null) const [notifButtonCoords, setNotifButtonCoords] = useState({ x: 0, y: 0 }) const [active, setActive] = useState() const [hasUnread, setHasUnread] = useState(false) const { data, refetch } = useQuery(HAS_UNREAD, { pollInterval: 60000 }) const notifCenterButtonRef = useRef() const popperRef = useRef() const history = useHistory() const classes = useStyles() useEffect(() => { if (data?.hasUnreadNotifications) return setHasUnread(true) // if not true, make sure it's false and not undefined if (notNil(data?.hasUnreadNotifications)) return setHasUnread(false) }, [data]) const onPaired = machine => { setOpen(false) history.push('/maintenance/machine-status', { id: machine.deviceId }) } // these inline styles prevent scroll bubbling: when the user reaches the bottom of the notifications list and keeps scrolling, // the body scrolls, stealing the focus from the notification center, preventing the admin from scrolling the notifications back up // on the first scroll, needing to move the mouse to recapture the focus on the notification center // it also disables the scrollbars caused by the notification center's background to the right of the page, but keeps the scrolling on the body enabled const onClickAway = () => { setAnchorEl(null) document.querySelector('#root').classList.remove('root-notifcenter-open') document.querySelector('body').classList.remove('body-notifcenter-open') } const handleClick = event => { const coords = notifCenterButtonRef.current.getBoundingClientRect() setNotifButtonCoords({ x: coords.x, y: coords.y }) setAnchorEl(anchorEl ? null : event.currentTarget) document.querySelector('#root').classList.add('root-notifcenter-open') document.querySelector('body').classList.add('body-notifcenter-open') } const popperOpen = Boolean(anchorEl) const id = popperOpen ? 'notifications-popper' : undefined return (
{ setActive(false) history.push('/dashboard') }} className={classnames(classes.logo, classes.logoLink)}>

Lamassu Admin

setOpen(true)}> Add machine
{active && active.children && ( )} {open && setOpen(false)} onPaired={onPaired} />}
) }) export default Header