SidebarNav.jsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import { withUnstatedContainers } from '../UnstatedUtils';
  5. import AppContainer from '~/client/services/AppContainer';
  6. import NavigationContainer from '~/client/services/NavigationContainer';
  7. class SidebarNav extends React.Component {
  8. static propTypes = {
  9. onItemSelected: PropTypes.func,
  10. };
  11. state = {
  12. };
  13. itemSelectedHandler = (contentsId) => {
  14. const { navigationContainer, onItemSelected } = this.props;
  15. if (onItemSelected != null) {
  16. onItemSelected(contentsId);
  17. }
  18. navigationContainer.selectSidebarContents(contentsId);
  19. }
  20. PrimaryItem = ({ id, label, iconName }) => {
  21. const { sidebarContentsId } = this.props.navigationContainer.state;
  22. const isSelected = sidebarContentsId === id;
  23. return (
  24. <button
  25. type="button"
  26. className={`d-block btn btn-primary ${isSelected ? 'active' : ''}`}
  27. onClick={() => this.itemSelectedHandler(id)}
  28. >
  29. <i className="material-icons">{iconName}</i>
  30. </button>
  31. );
  32. }
  33. SecondaryItem({
  34. label, iconName, href, isBlank,
  35. }) {
  36. return (
  37. <a href={href} className="d-block btn btn-primary" target={`${isBlank ? '_blank' : ''}`}>
  38. <i className="material-icons">{iconName}</i>
  39. </a>
  40. );
  41. }
  42. generateIconFactory(classNames) {
  43. return () => <i className={classNames}></i>;
  44. }
  45. render() {
  46. const { isAdmin, currentUsername, isSharedUser } = this.props.appContainer;
  47. const isLoggedIn = currentUsername != null;
  48. const { PrimaryItem, SecondaryItem } = this;
  49. return (
  50. <div className="grw-sidebar-nav">
  51. <div className="grw-sidebar-nav-primary-container">
  52. {!isSharedUser && <PrimaryItem id="custom" label="Custom Sidebar" iconName="code" />}
  53. {!isSharedUser && <PrimaryItem id="recent" label="Recent Changes" iconName="update" />}
  54. {/* <PrimaryItem id="tag" label="Tags" iconName="icon-tag" /> */}
  55. {/* <PrimaryItem id="favorite" label="Favorite" iconName="icon-star" /> */}
  56. </div>
  57. <div className="grw-sidebar-nav-secondary-container">
  58. {isAdmin && <SecondaryItem label="Admin" iconName="settings" href="/admin" />}
  59. {isLoggedIn && <SecondaryItem label="Draft" iconName="file_copy" href="/me/drafts" />}
  60. <SecondaryItem label="Help" iconName="help" href="https://docs.growi.org" isBlank />
  61. <SecondaryItem label="Trash" iconName="delete" href="/trash" />
  62. </div>
  63. </div>
  64. );
  65. }
  66. }
  67. SidebarNav.propTypes = {
  68. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  69. navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
  70. };
  71. /**
  72. * Wrapper component for using unstated
  73. */
  74. const SidebarNavWrapper = withUnstatedContainers(SidebarNav, [AppContainer, NavigationContainer]);
  75. export default withTranslation()(SidebarNavWrapper);