import React, { useState, useEffect, useRef } from "react"; import { withRouter, Link } from "react-router-dom"; import PropTypes from "prop-types"; import * as util from "../util"; const Header = (props) => { const { avatar, avatarImg, checkedAuth, signedIn, myChannel, handleSignIn } = props; const [showAvatarOptions, setShowAvatarOptions] = useState(false); const menuRef = React.createRef(); useEffect(() => { document.addEventListener("mousedown", handleClickOutside); return () => { document.removeEventListener("mousedown", handleClickOutside); }; }, []); const handleSignOut = (e, redirectUrl) => { e.preventDefault(); util.removeSession("ugc"); location.reload(); }; const handleClickOutside = (e) => { if (menuRef.current) { const clickOutside = menuRef && !menuRef.current.contains(e.target); if (clickOutside) { toggleUserMenu(); } } }; const toggleUserMenu = () => { setShowAvatarOptions(!showAvatarOptions); }; const basePath = util.getBasePath(); let actionItem =
; if (checkedAuth) { if (signedIn) { actionItem = ( <> {showAvatarOptions && (
My Channel Settings handleSignOut(e, `${basePath}`)} > Sign Out
)} ); } else { actionItem = ( ); } } return (
ACME Stream
{actionItem}
); }; Header.propTypes = { avatar: PropTypes.string, avatarImg: PropTypes.string, handleSignIn: PropTypes.func, signedIn: PropTypes.bool, myChannel: PropTypes.string, history: PropTypes.object, checkedAuth: PropTypes.bool, }; export default withRouter(Header);