PersonalDropdown.jsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import React, { useRef } from 'react';
  2. import { UserPicture } from '@growi/ui';
  3. import { useTranslation } from 'next-i18next';
  4. import Link from 'next/link';
  5. import { useRipple } from 'react-use-ripple';
  6. import { toastError } from '~/client/util/apiNotification';
  7. import { apiv3Post } from '~/client/util/apiv3-client';
  8. import { useCurrentUser } from '~/stores/context';
  9. const PersonalDropdown = () => {
  10. const { t } = useTranslation('commons');
  11. const { data: currentUser } = useCurrentUser();
  12. // ripple
  13. const buttonRef = useRef(null);
  14. useRipple(buttonRef, { rippleColor: 'rgba(255, 255, 255, 0.3)' });
  15. const user = currentUser || {};
  16. const logoutHandler = async() => {
  17. try {
  18. await apiv3Post('/logout');
  19. window.location.reload();
  20. }
  21. catch (err) {
  22. toastError(err);
  23. }
  24. };
  25. return (
  26. <>
  27. {/* Button */}
  28. {/* remove .dropdown-toggle for hide caret */}
  29. {/* See https://stackoverflow.com/a/44577512/13183572 */}
  30. <button className="bg-transparent border-0 nav-link" type="button" ref={buttonRef} data-toggle="dropdown" data-testid="personal-dropdown-button">
  31. <UserPicture user={user} noLink noTooltip /><span className="ml-1 d-none d-lg-inline-block">&nbsp;{user.name}</span>
  32. </button>
  33. {/* Menu */}
  34. <div className="dropdown-menu dropdown-menu-right" data-testid="personal-dropdown-menu">
  35. <div className="px-4 pt-3 pb-2 text-center">
  36. <UserPicture user={user} size="lg" noLink noTooltip />
  37. <h5 className="mt-2">
  38. {user.name}
  39. </h5>
  40. <div className="my-2">
  41. <i className="icon-user icon-fw"></i>{user.username}<br />
  42. <i className="icon-envelope icon-fw"></i><span className="grw-email-sm">{user.email}</span>
  43. </div>
  44. <div className="btn-group btn-block mt-2" role="group">
  45. <Link
  46. href={`/user/${user.username}`}
  47. className="btn btn-sm btn-outline-secondary col"
  48. data-testid="grw-personal-dropdown-menu-user-home"
  49. >
  50. <i className="icon-fw icon-home"></i>{t('personal_dropdown.home')}
  51. </Link>
  52. <Link
  53. href="/me"
  54. className="btn btn-sm btn-outline-secondary col"
  55. data-testid="grw-personal-dropdown-menu-user-settings"
  56. >
  57. <i className="icon-fw icon-wrench"></i>{t('personal_dropdown.settings')}
  58. </Link>
  59. </div>
  60. </div>
  61. <div className="dropdown-divider"></div>
  62. <button type="button" className="dropdown-item" onClick={logoutHandler}><i className="icon-fw icon-power"></i>{t('Sign out')}</button>
  63. </div>
  64. </>
  65. );
  66. };
  67. export default PersonalDropdown;