import { fetchLogout } from '@/services/login'; import { removeToken } from '@/utils/authUtils'; import { toLoginPage } from '@/utils/routerUtils'; import { LogoutOutlined, UserOutlined } from '@ant-design/icons'; import { history, Icon, useIntl, useModel } from '@umijs/max'; import { Spin } from 'antd'; import { createStyles } from 'antd-style'; import React, { useCallback } from 'react'; import { flushSync } from 'react-dom'; import HeaderDropdown from '../HeaderDropdown'; export type GlobalHeaderRightProps = { menu?: boolean; children?: React.ReactNode; }; export const AvatarName = () => { const { initialState } = useModel('@@initialState'); const { currentUser } = initialState || {}; return {currentUser?.username}; }; const useStyles = createStyles(({ token }) => { return { action: { display: 'flex', height: '48px', marginLeft: 'auto', overflow: 'hidden', alignItems: 'center', padding: '0 8px', cursor: 'pointer', borderRadius: token.borderRadius, '&:hover': { backgroundColor: token.colorBgTextHover, }, }, }; }); export const AvatarDropdown: React.FC = ({ menu, children }) => { const intl = useIntl(); const { initialState, setInitialState } = useModel('@@initialState'); const loginOut = async () => { await fetchLogout(); toLoginPage(); removeToken(); }; const { styles } = useStyles(); const onMenuClick = useCallback( (event: any) => { event?.domEvent?.stopPropagation(); const { key } = event; if (key === 'logout') { flushSync(() => { setInitialState((s) => ({ ...s, currentUser: undefined })); }); loginOut(); return; } history.push(`/sys/${key}`); }, [setInitialState], ); const loading = ( ); if (!initialState) { return loading; } const { currentUser } = initialState; if (!currentUser || !currentUser.username) { return loading; } const menuItems = [ ...(menu ? [ { key: 'userInfo', icon: , label: intl.formatMessage({ id: 'layout.user.userInfo' }), }, { key: 'modifyPassword', icon: , label: intl.formatMessage({ id: 'layout.user.modifyPassword' }), }, { type: 'divider' as const, }, ] : []), { key: 'logout', icon: , label: intl.formatMessage({ id: 'layout.user.logout' }), }, ]; return ( {children} ); };