umami/components/settings/UserButton.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-09-21 06:31:53 +02:00
import React from 'react';
2020-09-07 10:22:16 +02:00
import { FormattedMessage } from 'react-intl';
2020-08-06 08:03:07 +02:00
import { useRouter } from 'next/router';
2022-08-29 05:20:54 +02:00
import { removeItem } from 'next-basics';
2020-09-21 06:31:53 +02:00
import MenuButton from 'components/common/MenuButton';
import Icon from 'components/common/Icon';
2020-08-06 08:03:07 +02:00
import User from 'assets/user.svg';
import styles from './UserButton.module.css';
import { AUTH_TOKEN } from 'lib/constants';
import useUser from 'hooks/useUser';
2020-08-06 08:03:07 +02:00
2022-10-13 01:29:44 +02:00
export default function UserButton() {
const { user } = useUser();
2020-08-06 08:03:07 +02:00
const router = useRouter();
const menuOptions = [
{
label: (
2020-09-07 10:22:16 +02:00
<FormattedMessage
id="label.logged-in-as"
defaultMessage="Logged in as {username}"
values={{ username: <b>{user.username}</b> }}
/>
2020-08-06 08:03:07 +02:00
),
value: 'username',
className: styles.username,
},
2022-10-13 00:35:33 +02:00
{
label: <FormattedMessage id="label.profile" defaultMessage="Profile" />,
value: 'profile',
2022-10-27 21:14:34 +02:00
hidden: process.env.isAdminDisabled,
2022-10-13 00:35:33 +02:00
},
2020-09-07 10:22:16 +02:00
{ label: <FormattedMessage id="label.logout" defaultMessage="Logout" />, value: 'logout' },
2020-08-06 08:03:07 +02:00
];
function handleSelect(value) {
2020-08-09 08:48:43 +02:00
if (value === 'logout') {
removeItem(AUTH_TOKEN);
router.push('/login');
} else if (value === 'profile') {
router.push('/settings/profile');
2020-08-06 08:03:07 +02:00
}
}
return (
2020-09-21 06:31:53 +02:00
<MenuButton
icon={<Icon icon={<User />} size="large" />}
2022-03-04 04:45:49 +01:00
buttonVariant="light"
2020-09-21 06:31:53 +02:00
options={menuOptions}
onSelect={handleSelect}
2022-03-12 01:04:05 +01:00
hideLabel
2020-09-21 06:31:53 +02:00
/>
2020-08-06 08:03:07 +02:00
);
}