RecentChanges.jsx 3.7 KB

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