SidebarNav.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import {
  5. GlobalNav,
  6. } from '@atlaskit/navigation-next';
  7. import { createSubscribedElement } from '../UnstatedUtils';
  8. import AppContainer from '../../services/AppContainer';
  9. class SidebarNav extends React.Component {
  10. static propTypes = {
  11. currentContentsId: PropTypes.string,
  12. onItemSelected: PropTypes.func,
  13. };
  14. state = {
  15. };
  16. itemSelectedHandler = (contentsId) => {
  17. const { onItemSelected } = this.props;
  18. if (onItemSelected != null) {
  19. onItemSelected(contentsId);
  20. }
  21. }
  22. generatePrimaryItemObj(id, label, iconClassNames) {
  23. const isSelected = this.props.currentContentsId === id;
  24. return {
  25. id,
  26. component: ({ className }) => (
  27. <div className={`${className} grw-global-item-container ${isSelected ? 'active' : ''}`}>
  28. <button
  29. type="button"
  30. className="btn btn-primary btn-lg"
  31. onClick={() => this.itemSelectedHandler(id)}
  32. >
  33. <i className={iconClassNames}></i>
  34. </button>
  35. </div>
  36. ),
  37. };
  38. }
  39. generateSecondaryItemObj(id, label, iconClassNames, href, isBlank, isHiddenOnLargeDevice) {
  40. return {
  41. id,
  42. component: ({ className }) => (
  43. <div className={`${className} grw-global-item-container ${isHiddenOnLargeDevice ? 'd-block d-md-none' : ''}`}>
  44. <a href={href} className="btn btn-primary btn-lg" target={`${isBlank ? '_blank' : ''}`}>
  45. <i className={iconClassNames}></i>
  46. </a>
  47. </div>
  48. ),
  49. };
  50. }
  51. generateIconFactory(classNames) {
  52. return () => <i className={classNames}></i>;
  53. }
  54. render() {
  55. const { isAdmin, currentUsername } = this.props.appContainer;
  56. const primaryItems = [
  57. this.generatePrimaryItemObj('custom', 'Custom Sidebar', 'fa fa-code'),
  58. this.generatePrimaryItemObj('recent', 'Recent Changes', 'icon-clock'),
  59. // this.generatePrimaryItemObj('tag', 'Tags', 'icon-tag'),
  60. // this.generatePrimaryItemObj('favorite', 'Favorite', 'icon-star'),
  61. ];
  62. const secondaryItems = [
  63. this.generateSecondaryItemObj('draft', 'Draft', 'icon-docs', `/user/${currentUsername}#user-draft-list`),
  64. this.generateSecondaryItemObj('help', 'Help', 'icon-question', 'https://docs.growi.org', true),
  65. this.generateSecondaryItemObj('trash', 'Trash', 'icon-trash', '/trash'),
  66. ];
  67. if (isAdmin) {
  68. secondaryItems.unshift( // add to the beginning
  69. this.generateSecondaryItemObj('admin', 'Admin', 'icon-settings', '/admin', false, true),
  70. );
  71. }
  72. return (
  73. <GlobalNav
  74. primaryItems={primaryItems}
  75. secondaryItems={secondaryItems}
  76. />
  77. );
  78. }
  79. }
  80. SidebarNav.propTypes = {
  81. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  82. };
  83. /**
  84. * Wrapper component for using unstated
  85. */
  86. const SidebarNavWrapper = (props) => {
  87. return createSubscribedElement(SidebarNav, props, [AppContainer]);
  88. };
  89. export default withTranslation()(SidebarNavWrapper);