RecentChanges.jsx 8.5 KB

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