RecentChanges.jsx 6.7 KB

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