SidebarNav.jsx 2.7 KB

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