SignInAvatar.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. const { Title, Paragraph } = Typography;
  17. const SignInAvatarWidget = () => {
  18. const intl = useIntl();
  19. const navigate = useNavigate();
  20. const [userName, setUserName] = useState<string>();
  21. const [nickName, setNickName] = useState<string>();
  22. const user = useAppSelector(_currentUser);
  23. useEffect(() => {
  24. setUserName(user?.realName);
  25. setNickName(user?.nickName);
  26. }, [user]);
  27. const userCard = (
  28. <>
  29. <ProCard
  30. style={{ maxWidth: 500, minWidth: 300 }}
  31. actions={[
  32. <Tooltip
  33. title={intl.formatMessage({
  34. id: "buttons.setting",
  35. })}
  36. >
  37. <SettingOutlined key="setting" />
  38. </Tooltip>,
  39. <Tooltip
  40. title={intl.formatMessage({
  41. id: "columns.library.blog.label",
  42. })}
  43. >
  44. <Link to={`/blog/${userName}/overview`}>
  45. <HomeOutlined key="home" />
  46. </Link>
  47. </Tooltip>,
  48. <Tooltip
  49. title={intl.formatMessage({
  50. id: "buttons.sign-out",
  51. })}
  52. >
  53. <LogoutOutlined
  54. key="logout"
  55. onClick={() => {
  56. sessionStorage.removeItem("token");
  57. localStorage.removeItem("token");
  58. navigate("/anonymous/users/sign-in");
  59. }}
  60. />
  61. </Tooltip>,
  62. ]}
  63. >
  64. <Paragraph>
  65. <Title level={3}>{nickName}</Title>
  66. <Paragraph style={{ textAlign: "right" }}>
  67. {intl.formatMessage({
  68. id: "buttons.welcome",
  69. })}
  70. </Paragraph>
  71. </Paragraph>
  72. </ProCard>
  73. </>
  74. );
  75. if (typeof user === "undefined") {
  76. return <Link to="/anonymous/users/sign-in">登录</Link>;
  77. } else {
  78. return (
  79. <>
  80. <Popover content={userCard} placement="bottomRight">
  81. <Avatar
  82. style={{ backgroundColor: "#87d068" }}
  83. icon={<UserOutlined />}
  84. size="small"
  85. >
  86. {nickName?.slice(0, 1)}
  87. </Avatar>
  88. </Popover>
  89. </>
  90. );
  91. }
  92. };
  93. export default SignInAvatarWidget;