RecentChanges.jsx 8.6 KB

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