RecentChanges.jsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 { withUnstatedContainers } from '../UnstatedUtils';
  10. import AppContainer from '~/client/services/AppContainer';
  11. import { toastError } from '~/client/util/apiNotification';
  12. import FormattedDistanceDate from '../FormattedDistanceDate';
  13. const logger = loggerFactory('growi:History');
  14. class RecentChanges extends React.Component {
  15. static propTypes = {
  16. t: PropTypes.func.isRequired, // i18next
  17. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  18. };
  19. constructor(props) {
  20. super(props);
  21. this.reloadData = this.reloadData.bind(this);
  22. }
  23. async componentDidMount() {
  24. this.reloadData();
  25. }
  26. async reloadData() {
  27. const { appContainer } = this.props;
  28. try {
  29. await appContainer.retrieveRecentlyUpdated();
  30. }
  31. catch (error) {
  32. logger.error('failed to save', error);
  33. toastError(error, 'Error occurred in updating History');
  34. }
  35. }
  36. PageItem = ({ page }) => {
  37. const dPagePath = new DevidedPagePath(page.path, false, true);
  38. const linkedPagePathFormer = new LinkedPagePath(dPagePath.former);
  39. const linkedPagePathLatter = new LinkedPagePath(dPagePath.latter);
  40. const FormerLink = () => (
  41. <div className="grw-page-path-text-muted-container small">
  42. <PagePathHierarchicalLink linkedPagePath={linkedPagePathFormer} />
  43. </div>
  44. );
  45. return (
  46. <li className="list-group-item p-2">
  47. <div className="d-flex w-100">
  48. <UserPicture user={page.lastUpdateUser} size="md" noTooltip />
  49. <div className="flex-grow-1 ml-2">
  50. { !dPagePath.isRoot && <FormerLink /> }
  51. <h5 className="mb-1">
  52. <PagePathHierarchicalLink linkedPagePath={linkedPagePathLatter} basePath={dPagePath.isRoot ? undefined : dPagePath.former} />
  53. </h5>
  54. <div className="text-right small">
  55. <FormattedDistanceDate id={page.id} date={page.updatedAt} />
  56. </div>
  57. </div>
  58. </div>
  59. </li>
  60. );
  61. }
  62. render() {
  63. const { PageItem } = this;
  64. const { t } = this.props;
  65. const { recentlyUpdatedPages } = this.props.appContainer.state;
  66. return (
  67. <>
  68. <div className="grw-sidebar-content-header p-3 d-flex">
  69. <h3 className="mb-0">{t('Recent Changes')}</h3>
  70. {/* <h3 className="mb-0">{t('Recent Created')}</h3> */} {/* TODO: impl switching */}
  71. <button type="button" className="btn btn-sm btn-outline-secondary ml-auto" onClick={this.reloadData}>
  72. <i className="icon icon-reload"></i>
  73. </button>
  74. </div>
  75. <div className="grw-sidebar-content-body p-3">
  76. <ul className="list-group list-group-flush">
  77. { recentlyUpdatedPages.map(page => <PageItem key={page.id} page={page} />) }
  78. </ul>
  79. </div>
  80. </>
  81. );
  82. }
  83. }
  84. /**
  85. * Wrapper component for using unstated
  86. */
  87. const RecentChangesWrapper = withUnstatedContainers(RecentChanges, [AppContainer]);
  88. export default withTranslation()(RecentChangesWrapper);