umami/src/components/input/ProfileButton.tsx

60 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-02-03 13:06:55 +08:00
import { Key } from 'react';
2023-03-22 12:28:36 +08:00
import { Icon, Button, PopupTrigger, Popup, Menu, Item, Text } from 'react-basics';
2023-09-29 20:29:22 +08:00
import { useRouter } from 'next/navigation';
2023-03-22 12:28:36 +08:00
import Icons from 'components/icons';
2024-02-05 15:55:14 +08:00
import { useMessages, useLogin, useLocale } from 'components/hooks';
2023-09-05 04:42:39 +08:00
import { CURRENT_VERSION } from 'lib/constants';
2024-02-04 15:19:29 +08:00
import styles from './ProfileButton.module.css';
2023-03-22 12:28:36 +08:00
2023-04-21 23:00:42 +08:00
export function ProfileButton() {
2023-03-22 12:28:36 +08:00
const { formatMessage, labels } = useMessages();
const { user } = useLogin();
2023-03-22 12:28:36 +08:00
const router = useRouter();
const { dir } = useLocale();
const cloudMode = !!process.env.cloudMode;
2023-03-22 12:28:36 +08:00
2024-02-03 13:06:55 +08:00
const handleSelect = (key: Key, close: () => void) => {
2023-03-22 12:28:36 +08:00
if (key === 'profile') {
2024-02-05 15:55:14 +08:00
router.push('/profile');
2023-03-22 12:28:36 +08:00
}
if (key === 'logout') {
router.push('/logout');
}
2024-02-03 13:06:55 +08:00
close();
2023-03-22 12:28:36 +08:00
};
return (
<PopupTrigger>
<Button data-test="button-profile" variant="quiet">
2023-03-22 12:28:36 +08:00
<Icon>
<Icons.Profile />
</Icon>
</Button>
<Popup position="bottom" alignment={dir === 'rtl' ? 'start' : 'end'}>
2024-02-03 13:06:55 +08:00
{(close: () => void) => (
<Menu onSelect={key => handleSelect(key, close)} className={styles.menu}>
2024-02-06 07:32:36 +08:00
<Text className={styles.name}>{user.username}</Text>
2024-02-03 13:06:55 +08:00
<Item key="profile" className={styles.item} divider={true}>
2023-04-07 11:34:02 +08:00
<Icon>
2024-02-03 13:06:55 +08:00
<Icons.User />
2023-04-07 11:34:02 +08:00
</Icon>
2024-02-03 13:06:55 +08:00
<Text>{formatMessage(labels.profile)}</Text>
2023-04-07 11:34:02 +08:00
</Item>
2024-02-03 13:06:55 +08:00
{!cloudMode && (
<Item data-test="item-logout" key="logout" className={styles.item}>
2024-02-03 13:06:55 +08:00
<Icon>
<Icons.Logout />
</Icon>
<Text>{formatMessage(labels.logout)}</Text>
</Item>
)}
<div className={styles.version}>{`v${CURRENT_VERSION}`}</div>
</Menu>
)}
2023-03-22 12:28:36 +08:00
</Popup>
</PopupTrigger>
);
}
2023-04-21 23:00:42 +08:00
export default ProfileButton;