PersonalDropdown.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { createSubscribedElement } from '../UnstatedUtils';
  5. import AppContainer from '../../services/AppContainer';
  6. import UserPicture from '../User/UserPicture';
  7. const PersonalDropdown = (props) => {
  8. const { t, appContainer } = props;
  9. const user = appContainer.currentUser || {};
  10. const logoutHandler = () => {
  11. const { interceptorManager } = appContainer;
  12. const context = {
  13. user,
  14. currentPagePath: decodeURIComponent(window.location.pathname),
  15. };
  16. interceptorManager.process('logout', context);
  17. window.location.href = '/logout';
  18. };
  19. const followOsCheckboxModifiedHandler = (bool) => {
  20. // reset user preference
  21. if (bool) {
  22. appContainer.setColorSchemePreference(null);
  23. }
  24. // set preferDarkModeByMediaQuery as users preference
  25. else {
  26. appContainer.setColorSchemePreference(appContainer.state.preferDarkModeByMediaQuery);
  27. }
  28. };
  29. const userPreferenceSwitchModifiedHandler = (bool) => {
  30. appContainer.setColorSchemePreference(bool);
  31. };
  32. /*
  33. * render
  34. */
  35. const { preferDarkModeByMediaQuery, preferDarkModeByUser } = appContainer.state;
  36. const isUserPreferenceExists = preferDarkModeByUser != null;
  37. const isDarkMode = () => {
  38. if (isUserPreferenceExists) {
  39. return preferDarkModeByUser;
  40. }
  41. return preferDarkModeByMediaQuery;
  42. };
  43. return (
  44. <>
  45. {/* Button */}
  46. {/* remove .dropdown-toggle for hide caret */}
  47. {/* See https://stackoverflow.com/a/44577512/13183572 */}
  48. <a className="nav-link waves-effect waves-light" data-toggle="dropdown">
  49. <UserPicture user={user} withoutLink /><span className="d-none d-sm-inline-block">&nbsp;{user.name}</span>
  50. </a>
  51. {/* Menu */}
  52. <div className="dropdown-menu dropdown-menu-right">
  53. <a className="dropdown-item" href={`/user/${user.username}`}><i className="icon-fw icon-user"></i>{ t('User\'s Home') }</a>
  54. <a className="dropdown-item" href="/me"><i className="icon-fw icon-wrench"></i>{ t('User Settings') }</a>
  55. <div className="dropdown-divider"></div>
  56. <a className="dropdown-item" href={`/user/${user.username}#user-draft-list`}><i className="icon-fw icon-docs"></i>{ t('List Drafts') }</a>
  57. <a className="dropdown-item" href="/trash"><i className="icon-fw icon-trash"></i>{ t('Deleted Pages') }</a>
  58. <div className="dropdown-divider"></div>
  59. <h6 className="dropdown-header">Color Scheme</h6>
  60. <form className="px-4">
  61. <div className="form-row align-items-center">
  62. <div className="form-group col-auto">
  63. <div className="custom-control custom-checkbox">
  64. <input
  65. id="cbFollowOs"
  66. className="custom-control-input"
  67. type="checkbox"
  68. checked={!isUserPreferenceExists}
  69. onChange={e => followOsCheckboxModifiedHandler(e.target.checked)}
  70. />
  71. <label className="custom-control-label text-nowrap" htmlFor="cbFollowOs">Use OS Setting</label>
  72. </div>
  73. </div>
  74. </div>
  75. <div className="form-row align-items-center">
  76. <div className="form-group col-auto mb-0 d-flex">
  77. <span className={isUserPreferenceExists ? '' : 'text-muted'}>Light</span>
  78. <div className="custom-control custom-switch custom-checkbox-secondary ml-2">
  79. <input
  80. id="swUserPreference"
  81. className="custom-control-input"
  82. type="checkbox"
  83. checked={isDarkMode()}
  84. disabled={!isUserPreferenceExists}
  85. onChange={e => userPreferenceSwitchModifiedHandler(e.target.checked)}
  86. />
  87. <label className="custom-control-label" htmlFor="swUserPreference"></label>
  88. </div>
  89. <span className={isUserPreferenceExists ? '' : 'text-muted'}>Dark</span>
  90. </div>
  91. </div>
  92. </form>
  93. <div className="dropdown-divider"></div>
  94. <a className="dropdown-item" type="button" onClick={logoutHandler}><i className="icon-fw icon-power"></i>{ t('Sign out') }</a>
  95. </div>
  96. </>
  97. );
  98. };
  99. /**
  100. * Wrapper component for using unstated
  101. */
  102. const PersonalDropdownWrapper = (props) => {
  103. return createSubscribedElement(PersonalDropdown, props, [AppContainer]);
  104. };
  105. PersonalDropdown.propTypes = {
  106. t: PropTypes.func.isRequired, // i18next
  107. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  108. };
  109. export default withTranslation()(PersonalDropdownWrapper);