import User from 'assets/user.svg'; import useConfig from 'hooks/useConfig'; import useUser from 'hooks/useUser'; import { AUTH_TOKEN } from 'lib/constants'; import { removeItem } from 'next-basics'; import { useRouter } from 'next/router'; import { useRef, useState } from 'react'; import { Button, Icon, Item, Menu, Popup, Text } from 'react-basics'; import { FormattedMessage } from 'react-intl'; import styles from './UserButton.module.css'; import useDocumentClick from '../../hooks/useDocumentClick'; export default function UserButton() { const [show, setShow] = useState(false); const ref = useRef(); const user = useUser(); const router = useRouter(); const { adminDisabled } = useConfig(); const menuOptions = [ { label: ( {user.username} }} /> ), value: 'username', className: styles.username, }, { label: , value: 'profile', hidden: adminDisabled, divider: true, }, { label: , value: 'logout' }, ]; function handleClick() { setShow(state => !state); } function handleSelect(value) { if (value === 'logout') { removeItem(AUTH_TOKEN); router.push('/login'); } else if (value === 'profile') { router.push('/profile'); } } useDocumentClick(e => { if (!ref.current?.contains(e.target)) { setShow(false); } }); return (
{show && ( {({ label, value }) => ( {label} )} )}
); }