RecentChanges.jsx 8.7 KB

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