RecentChanges.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.reloadData = this.reloadData.bind(this);
  23. }
  24. async componentDidMount() {
  25. this.reloadData();
  26. }
  27. async reloadData() {
  28. const { appContainer } = this.props;
  29. try {
  30. await appContainer.retrieveRecentlyUpdated();
  31. }
  32. catch (error) {
  33. logger.error('failed to save', error);
  34. toastError(error, 'Error occurred in updating History');
  35. }
  36. }
  37. PageItem = ({ page }) => {
  38. const dPagePath = new DevidedPagePath(page.path, false, true);
  39. const linkedPagePathFormer = new LinkedPagePath(dPagePath.former);
  40. const linkedPagePathLatter = new LinkedPagePath(dPagePath.latter);
  41. const FormerLink = () => (
  42. <div className="grw-page-path-text-muted-container small">
  43. <PagePathHierarchicalLink linkedPagePath={linkedPagePathFormer} />
  44. </div>
  45. );
  46. let locked;
  47. if (page.grant !== 1) {
  48. locked = <span><i className="icon-lock ml-2" /></span>;
  49. }
  50. const tags = page.tags;
  51. const tagElements = tags.map((tag) => {
  52. return (
  53. <a key={tag} href={`/_search?q=tag:${tag.name}`} className="grw-tag-label badge badge-secondary mr-2 small">
  54. {tag.name}
  55. </a>
  56. );
  57. });
  58. return (
  59. <li className="list-group-item p-2">
  60. <div className="d-flex w-100">
  61. <UserPicture user={page.lastUpdateUser} size="md" noTooltip />
  62. <div className="flex-grow-1 ml-2">
  63. { !dPagePath.isRoot && <FormerLink /> }
  64. <h5 className="mb-1">
  65. <PagePathHierarchicalLink linkedPagePath={linkedPagePathLatter} basePath={dPagePath.isRoot ? undefined : dPagePath.former} />
  66. {locked}
  67. </h5>
  68. <div className="my-2">
  69. { tagElements }
  70. </div>
  71. <div className="pull-left">
  72. <span className="footstamp-icon mr-1"><FootstampIcon /></span>
  73. <span className="mr-2 grw-list-counts">{page.seenUsers.length}</span>
  74. <i className="icon-bubble mr-1"></i>
  75. <span className="mr-2 grw-list-counts">{page.commentCount}</span>
  76. </div>
  77. <div className="pull-right small">
  78. <FormattedDistanceDate id={page.id} date={page.updatedAt} />
  79. </div>
  80. </div>
  81. </div>
  82. </li>
  83. );
  84. }
  85. render() {
  86. const { PageItem } = this;
  87. const { t } = this.props;
  88. const { recentlyUpdatedPages } = this.props.appContainer.state;
  89. return (
  90. <>
  91. <div className="grw-sidebar-content-header p-3 d-flex">
  92. <h3 className="mb-0">{t('Recent Changes')}</h3>
  93. {/* <h3 className="mb-0">{t('Recent Created')}</h3> */} {/* TODO: impl switching */}
  94. <button type="button" className="btn btn-sm btn-outline-secondary ml-auto" onClick={this.reloadData}>
  95. <i className="icon icon-reload"></i>
  96. </button>
  97. </div>
  98. <div className="grw-sidebar-content-body p-3">
  99. <ul className="list-group list-group-flush">
  100. { recentlyUpdatedPages.map(page => <PageItem key={page.id} page={page} />) }
  101. </ul>
  102. </div>
  103. </>
  104. );
  105. }
  106. }
  107. /**
  108. * Wrapper component for using unstated
  109. */
  110. const RecentChangesWrapper = withUnstatedContainers(RecentChanges, [AppContainer]);
  111. export default withTranslation()(RecentChangesWrapper);