PersonalDropdown.jsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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();
  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">
  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">
  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 href={`/user/${user.username}`}>
  46. <a className="btn btn-sm btn-outline-secondary col">
  47. <i className="icon-fw icon-home"></i>{ t('personal_dropdown.home') }
  48. </a>
  49. </Link>
  50. <Link href="/me">
  51. <a className="btn btn-sm btn-outline-secondary col">
  52. <i className="icon-fw icon-wrench"></i>{ t('personal_dropdown.settings') }
  53. </a>
  54. </Link>
  55. </div>
  56. </div>
  57. <div className="dropdown-divider"></div>
  58. <button type="button" className="dropdown-item" onClick={logoutHandler}><i className="icon-fw icon-power"></i>{ t('Sign out') }</button>
  59. </div>
  60. </>
  61. );
  62. };
  63. export default PersonalDropdown;