RecentChanges.jsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import loggerFactory from '@alias/logger';
  5. import DevidedPagePath from '@commons/models/devided-page-path';
  6. import LinkedPagePath from '@commons/models/linked-page-path';
  7. import PagePathHierarchicalLink from '@commons/components/PagePathHierarchicalLink';
  8. import FootstampIcon from '../FootstampIcon';
  9. import { withUnstatedContainers } from '../UnstatedUtils';
  10. import AppContainer from '../../services/AppContainer';
  11. import { toastError } from '../../util/apiNotification';
  12. import FormattedDistanceDate from '../FormattedDistanceDate';
  13. import UserPicture from '../User/UserPicture';
  14. const logger = loggerFactory('growi:History');
  15. class RecentChanges extends React.Component {
  16. static propTypes = {
  17. t: PropTypes.func.isRequired, // i18next
  18. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  19. };
  20. constructor(props) {
  21. super(props);
  22. this.state = {
  23. // TODO: 7092 connect to state
  24. // eslint-disable-next-line react/no-unused-state
  25. isRecentChangesSidebarSmall: false,
  26. };
  27. this.reloadData = this.reloadData.bind(this);
  28. }
  29. componentWillMount() {
  30. this.retrieveSizePreferenceFromLocalStorage();
  31. }
  32. async componentDidMount() {
  33. this.reloadData();
  34. }
  35. async reloadData() {
  36. const { appContainer } = this.props;
  37. try {
  38. await appContainer.retrieveRecentlyUpdated();
  39. }
  40. catch (error) {
  41. logger.error('failed to save', error);
  42. toastError(error, 'Error occurred in updating History');
  43. }
  44. }
  45. retrieveSizePreferenceFromLocalStorage() {
  46. if (window.localStorage.isRecentChangesSidebarSmall === 'true') {
  47. this.setState({
  48. // TODO: 7092 connect to state
  49. // eslint-disable-next-line react/no-unused-state
  50. isRecentChangesSidebarSmall: true,
  51. });
  52. }
  53. }
  54. changeSizeHandler = (e) => {
  55. this.setState({
  56. // TODO: 7092 connect to state
  57. // eslint-disable-next-line react/no-unused-state
  58. isRecentChangesSidebarSmall: e.target.checked,
  59. });
  60. window.localStorage.setItem('isRecentChangesSidebarSmall', e.target.checked);
  61. }
  62. PageItem = ({ page }) => {
  63. const dPagePath = new DevidedPagePath(page.path, false, true);
  64. const linkedPagePathFormer = new LinkedPagePath(dPagePath.former);
  65. const linkedPagePathLatter = new LinkedPagePath(dPagePath.latter);
  66. const FormerLink = () => (
  67. <div className="grw-page-path-text-muted-container small">
  68. <PagePathHierarchicalLink linkedPagePath={linkedPagePathFormer} />
  69. </div>
  70. );
  71. let locked;
  72. if (page.grant !== 1) {
  73. locked = <span><i className="icon-lock ml-2" /></span>;
  74. }
  75. const tags = page.tags;
  76. const tagElements = tags.map((tag) => {
  77. return (
  78. <a key={tag} href={`/_search?q=tag:${tag.name}`} className="grw-tag-label badge badge-secondary mr-2 small">
  79. {tag.name}
  80. </a>
  81. );
  82. });
  83. return (
  84. <li className="list-group-item py-3 px-0">
  85. <div className="d-flex w-100">
  86. <UserPicture user={page.lastUpdateUser} size="md" noTooltip />
  87. <div className="flex-grow-1 ml-2">
  88. { !dPagePath.isRoot && <FormerLink /> }
  89. <h5 className="my-2">
  90. <PagePathHierarchicalLink linkedPagePath={linkedPagePathLatter} basePath={dPagePath.isRoot ? undefined : dPagePath.former} />
  91. {locked}
  92. </h5>
  93. <div className="mt-1 mb-2">
  94. { tagElements }
  95. </div>
  96. <div className="d-flex justify-content-between grw-recent-changes-item-lower pt-1">
  97. <div className="d-flex">
  98. <div className="footstamp-icon mr-1 d-inline-block"><FootstampIcon /></div>
  99. <div className="mr-2 grw-list-counts d-inline-block">{page.seenUsers.length}</div>
  100. <div className="icon-bubble mr-1 d-inline-block"></div>
  101. <div className="mr-2 grw-list-counts d-inline-block">{page.commentCount}</div>
  102. </div>
  103. <div className="grw-formatted-distance-date small mt-auto">
  104. <FormattedDistanceDate id={page.id} date={page.updatedAt} />
  105. </div>
  106. </div>
  107. </div>
  108. </div>
  109. </li>
  110. );
  111. }
  112. render() {
  113. const { PageItem } = this;
  114. const { t } = this.props;
  115. const { recentlyUpdatedPages } = this.props.appContainer.state;
  116. return (
  117. <>
  118. <div className="grw-sidebar-content-header p-3 d-flex">
  119. <h3 className="mb-0">{t('Recent Changes')}</h3>
  120. {/* <h3 className="mb-0">{t('Recent Created')}</h3> */} {/* TODO: impl switching */}
  121. <button type="button" className="btn btn-sm ml-auto grw-btn-reload-rc" onClick={this.reloadData}>
  122. <i className="icon icon-reload"></i>
  123. </button>
  124. <div className="grw-recent-changes-resize-button custom-control custom-switch ml-2">
  125. <input
  126. id="recentChangesResize"
  127. className="custom-control-input"
  128. type="checkbox"
  129. // checked={}
  130. // disabled={}
  131. // onChange={e => userPreferenceSwitchModifiedHandler(e.target.checked)}
  132. />
  133. <label className="custom-control-label" htmlFor="recentChangesResize">
  134. </label>
  135. </div>
  136. </div>
  137. <div className="grw-sidebar-content-body grw-recent-changes p-3">
  138. <ul className="list-group list-group-flush">
  139. { recentlyUpdatedPages.map(page => <PageItem key={page.id} page={page} />) }
  140. </ul>
  141. </div>
  142. </>
  143. );
  144. }
  145. }
  146. /**
  147. * Wrapper component for using unstated
  148. */
  149. const RecentChangesWrapper = withUnstatedContainers(RecentChanges, [AppContainer]);
  150. export default withTranslation()(RecentChangesWrapper);