blob: 9221a05614b2370902ba78096b30c5ea43529dbc [file] [log] [blame]
/*
* MIT License
* Copyright (c) 2019 Alipay.inc
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React, { useCallback } from 'react';
import { SettingOutlined, UserOutlined, SettingFilled, LogoutOutlined } from '@ant-design/icons';
import { Avatar, Menu, Spin } from 'antd';
import { history, useModel, useIntl } from 'umi';
import { stringify } from 'querystring';
import HeaderDropdown from '../HeaderDropdown';
import styles from './index.less';
export type GlobalHeaderRightProps = {
menu?: boolean;
};
const settings = async () => {
history.replace({
pathname: '/settings',
search: stringify({
redirect: window.location.href,
}),
});
};
const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu }) => {
const { formatMessage } = useIntl();
const { initialState, setInitialState } = useModel('@@initialState');
const onMenuClick = useCallback((event) => {
const { key } = event;
if (key === 'settings') {
settings();
return;
}
if (key === 'logout') {
setInitialState({ ...initialState, currentUser: undefined });
history.replace({
pathname: '/user/logout',
search: stringify({
redirect: window.location.pathname,
}),
});
return;
}
history.push(`/account/${key}`);
}, []);
const loading = (
<span className={`${styles.action} ${styles.account}`}>
<Spin
size="small"
style={{
marginLeft: 8,
marginRight: 8,
}}
/>
</span>
);
if (!initialState) {
return loading;
}
const { currentUser } = initialState;
if (!currentUser || !currentUser.name) {
return loading;
}
const menuHeaderDropdown = (
<Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick}>
{menu && (
<Menu.Item key="center">
<UserOutlined />
{formatMessage({ id: 'menu.account.center' })}
</Menu.Item>
)}
{menu && (
<Menu.Item key="settings">
<SettingOutlined />
{formatMessage({ id: 'menu.setting' })}
</Menu.Item>
)}
{menu && <Menu.Divider />}
<Menu.Item key="settings">
<SettingFilled />
{formatMessage({ id: 'menu.setting' })}
</Menu.Item>
<Menu.Divider />
<Menu.Item key="logout">
<LogoutOutlined />
{formatMessage({ id: 'menu.account.logout' })}
</Menu.Item>
</Menu>
);
return (
<HeaderDropdown overlay={menuHeaderDropdown}>
<span className={`${styles.action} ${styles.account}`}>
<Avatar size="small" className={styles.avatar} src={currentUser.avatar} alt="avatar" />
<span className={`${styles.name} anticon`}>{currentUser.name}</span>
</span>
</HeaderDropdown>
);
};
export default AvatarDropdown;