Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

78 wiersze
2.6 KiB

  1. "use client";
  2. import IconButton from "@mui/material/IconButton";
  3. import Menu from "@mui/material/Menu";
  4. import MenuItem from "@mui/material/MenuItem";
  5. import Avatar from "@mui/material/Avatar";
  6. import React from "react";
  7. import { AppBarProps } from "./AppBar";
  8. import Divider from "@mui/material/Divider";
  9. import Typography from "@mui/material/Typography";
  10. import { useTranslation } from "react-i18next";
  11. import { signOut } from "next-auth/react";
  12. import { usePathname, useRouter, useSearchParams } from "next/navigation";
  13. type Props = Pick<AppBarProps, "avatarImageSrc" | "profileName">;
  14. const Profile: React.FC<Props> = ({ avatarImageSrc, profileName }) => {
  15. const [profileMenuAnchorEl, setProfileMenuAnchorEl] =
  16. React.useState<HTMLButtonElement>();
  17. const openProfileMenu: React.MouseEventHandler<HTMLButtonElement> = (
  18. event,
  19. ) => {
  20. setProfileMenuAnchorEl(event.currentTarget);
  21. };
  22. const closeProfileMenu = () => {
  23. setProfileMenuAnchorEl(undefined);
  24. };
  25. const { t, i18n: { language } } = useTranslation("login");
  26. const router = useRouter();
  27. const pathname = usePathname();
  28. const searchParams = useSearchParams()
  29. const onLangClick = React.useCallback((lang: string) => {
  30. const params = new URLSearchParams(searchParams.toString())
  31. params.set("lang", lang)
  32. router.replace(`${pathname}?${params.toString()}`);
  33. setTimeout(() => {
  34. window.location.reload();
  35. }, 80);
  36. }, [router, pathname, searchParams]);
  37. return (
  38. <>
  39. <IconButton aria-label="profile" onClick={openProfileMenu}>
  40. <Avatar src={avatarImageSrc} />
  41. </IconButton>
  42. <Menu
  43. id="profile-menu"
  44. anchorEl={profileMenuAnchorEl}
  45. anchorOrigin={{
  46. vertical: "bottom",
  47. horizontal: "right",
  48. }}
  49. keepMounted
  50. transformOrigin={{
  51. vertical: "top",
  52. horizontal: "right",
  53. }}
  54. open={Boolean(profileMenuAnchorEl)}
  55. onClose={closeProfileMenu}
  56. MenuListProps={{ dense: true, disablePadding: true }}
  57. >
  58. <Typography sx={{ mx: "1.5rem", my: "0.5rem" }} fontWeight="bold">
  59. {profileName}
  60. </Typography>
  61. <Divider />
  62. <MenuItem onClick={() => { router.replace("/changepassword") }}>{t("Change Password")}</MenuItem>
  63. {language === "zh" && <MenuItem onClick={() => { onLangClick("en") }}>{t("Change To English Version")}</MenuItem>}
  64. {language === "en" && <MenuItem onClick={() => { onLangClick("zh") }}>{t("Change To Chinese Version")}</MenuItem>}
  65. <MenuItem onClick={() => signOut()}>{t("Sign out")}</MenuItem>
  66. </Menu>
  67. </>
  68. );
  69. };
  70. export default Profile;