AvatarDropdown.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { fetchLogout } from '@/services/login';
  2. import { removeToken } from '@/utils/authUtils';
  3. import { toLoginPage } from '@/utils/routerUtils';
  4. import { LogoutOutlined, UserOutlined } from '@ant-design/icons';
  5. import { history, Icon, useIntl, useModel } from '@umijs/max';
  6. import { Spin } from 'antd';
  7. import { createStyles } from 'antd-style';
  8. import React, { useCallback } from 'react';
  9. import { flushSync } from 'react-dom';
  10. import HeaderDropdown from '../HeaderDropdown';
  11. export type GlobalHeaderRightProps = {
  12. menu?: boolean;
  13. children?: React.ReactNode;
  14. };
  15. export const AvatarName = () => {
  16. const { initialState } = useModel('@@initialState');
  17. const { currentUser } = initialState || {};
  18. return <span className="anticon leading-6">{currentUser?.username}</span>;
  19. };
  20. const useStyles = createStyles(({ token }) => {
  21. return {
  22. action: {
  23. display: 'flex',
  24. height: '48px',
  25. marginLeft: 'auto',
  26. overflow: 'hidden',
  27. alignItems: 'center',
  28. padding: '0 8px',
  29. cursor: 'pointer',
  30. borderRadius: token.borderRadius,
  31. '&:hover': {
  32. backgroundColor: token.colorBgTextHover,
  33. },
  34. },
  35. };
  36. });
  37. export const AvatarDropdown: React.FC<GlobalHeaderRightProps> = ({ menu, children }) => {
  38. const intl = useIntl();
  39. const { initialState, setInitialState } = useModel('@@initialState');
  40. const loginOut = async () => {
  41. await fetchLogout();
  42. toLoginPage();
  43. removeToken();
  44. };
  45. const { styles } = useStyles();
  46. const onMenuClick = useCallback(
  47. (event: any) => {
  48. event?.domEvent?.stopPropagation();
  49. const { key } = event;
  50. if (key === 'logout') {
  51. flushSync(() => {
  52. setInitialState((s) => ({ ...s, currentUser: undefined }));
  53. });
  54. loginOut();
  55. return;
  56. }
  57. history.push(`/sys/${key}`);
  58. },
  59. [setInitialState],
  60. );
  61. const loading = (
  62. <span className={styles.action}>
  63. <Spin
  64. size="small"
  65. style={{
  66. marginLeft: 8,
  67. marginRight: 8,
  68. }}
  69. />
  70. </span>
  71. );
  72. if (!initialState) {
  73. return loading;
  74. }
  75. const { currentUser } = initialState;
  76. if (!currentUser || !currentUser.username) {
  77. return loading;
  78. }
  79. const menuItems = [
  80. ...(menu
  81. ? [
  82. {
  83. key: 'userInfo',
  84. icon: <UserOutlined />,
  85. label: intl.formatMessage({ id: 'layout.user.userInfo' }),
  86. },
  87. {
  88. key: 'modifyPassword',
  89. icon: <Icon icon="ri:key-line" className="mt-2" />,
  90. label: intl.formatMessage({ id: 'layout.user.modifyPassword' }),
  91. },
  92. {
  93. type: 'divider' as const,
  94. },
  95. ]
  96. : []),
  97. {
  98. key: 'logout',
  99. icon: <LogoutOutlined />,
  100. label: intl.formatMessage({ id: 'layout.user.logout' }),
  101. },
  102. ];
  103. return (
  104. <HeaderDropdown
  105. menu={{
  106. selectedKeys: [],
  107. onClick: onMenuClick,
  108. items: menuItems,
  109. }}
  110. >
  111. {children}
  112. </HeaderDropdown>
  113. );
  114. };