SignInAvatar.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { useIntl } from "react-intl";
  2. import { useEffect, useState } from "react";
  3. import { Link, useNavigate } from "react-router-dom";
  4. import { Tooltip, Typography } from "antd";
  5. import { Avatar } from "antd";
  6. import { Popover } from "antd";
  7. import { ProCard } from "@ant-design/pro-components";
  8. import {
  9. UserOutlined,
  10. HomeOutlined,
  11. LogoutOutlined,
  12. SettingOutlined,
  13. } from "@ant-design/icons";
  14. import { useAppSelector } from "../../hooks";
  15. import { currentUser as _currentUser } from "../../reducers/current-user";
  16. import { TooltipPlacement } from "antd/lib/tooltip";
  17. import SettingModal from "./setting/SettingModal";
  18. import { AdminIcon } from "../../assets/icon";
  19. const { Title, Paragraph } = Typography;
  20. interface IWidget {
  21. placement?: TooltipPlacement;
  22. style?: React.CSSProperties;
  23. }
  24. const SignInAvatarWidget = ({ style, placement = "bottomRight" }: IWidget) => {
  25. const intl = useIntl();
  26. const navigate = useNavigate();
  27. const [userName, setUserName] = useState<string>();
  28. const [nickName, setNickName] = useState<string>();
  29. const user = useAppSelector(_currentUser);
  30. console.debug("user", user);
  31. useEffect(() => {
  32. setUserName(user?.realName);
  33. setNickName(user?.nickName);
  34. }, [user]);
  35. if (typeof user === "undefined") {
  36. return (
  37. <Link to="/anonymous/users/sign-in">
  38. {intl.formatMessage({
  39. id: "nut.users.sign-in-up.title",
  40. })}
  41. </Link>
  42. );
  43. } else {
  44. return (
  45. <>
  46. <Popover
  47. content={
  48. <ProCard
  49. style={{ maxWidth: 500, minWidth: 300 }}
  50. actions={[
  51. <Tooltip
  52. title={intl.formatMessage({
  53. id: "buttons.setting",
  54. })}
  55. >
  56. <SettingModal trigger={<SettingOutlined key="setting" />} />
  57. </Tooltip>,
  58. user.roles?.includes("root") ||
  59. user.roles?.includes("administrator") ? (
  60. <Tooltip
  61. title={intl.formatMessage({
  62. id: "buttons.admin",
  63. })}
  64. >
  65. <Link to={`/admin`}>
  66. <AdminIcon />
  67. </Link>
  68. </Tooltip>
  69. ) : (
  70. <></>
  71. ),
  72. <Tooltip
  73. title={intl.formatMessage({
  74. id: "columns.library.blog.label",
  75. })}
  76. >
  77. <Link to={`/blog/${userName}/overview`}>
  78. <HomeOutlined key="home" />
  79. </Link>
  80. </Tooltip>,
  81. <Tooltip
  82. title={intl.formatMessage({
  83. id: "buttons.sign-out",
  84. })}
  85. >
  86. <LogoutOutlined
  87. key="logout"
  88. onClick={() => {
  89. sessionStorage.removeItem("token");
  90. localStorage.removeItem("token");
  91. navigate("/anonymous/users/sign-in");
  92. }}
  93. />
  94. </Tooltip>,
  95. ]}
  96. >
  97. <Paragraph>
  98. <Title level={3}>{nickName}</Title>
  99. <Paragraph style={{ textAlign: "right" }}>
  100. {intl.formatMessage({
  101. id: "buttons.welcome",
  102. })}
  103. </Paragraph>
  104. </Paragraph>
  105. </ProCard>
  106. }
  107. placement={placement}
  108. >
  109. <span style={style}>
  110. <Avatar
  111. style={{ backgroundColor: "#87d068" }}
  112. icon={<UserOutlined />}
  113. src={user?.avatar}
  114. size="small"
  115. >
  116. {nickName?.slice(0, 2)}
  117. </Avatar>
  118. </span>
  119. </Popover>
  120. </>
  121. );
  122. }
  123. };
  124. export default SignInAvatarWidget;