RecentChanges.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import React, {
  2. FC,
  3. useCallback, useEffect, useState,
  4. } from 'react';
  5. import { DevidedPagePath } from '@growi/core';
  6. import { UserPicture, FootstampIcon } from '@growi/ui';
  7. import PropTypes from 'prop-types';
  8. import { useTranslation } from 'react-i18next';
  9. import PagePathHierarchicalLink from '~/components/PagePathHierarchicalLink';
  10. import LinkedPagePath from '~/models/linked-page-path';
  11. import { useSWRInifinitexRecentlyUpdated } from '~/stores/page';
  12. import loggerFactory from '~/utils/logger';
  13. import FormattedDistanceDate from '../FormattedDistanceDate';
  14. import InfiniteScroll from './InfiniteScroll';
  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" data-hide-in-vrt>
  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 text-truncate">
  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 = (): JSX.Element => {
  110. const PER_PAGE = 20;
  111. const { t } = useTranslation();
  112. const swr = useSWRInifinitexRecentlyUpdated();
  113. const [isRecentChangesSidebarSmall, setIsRecentChangesSidebarSmall] = useState(false);
  114. const isEmpty = swr.data?.[0].length === 0;
  115. const isReachingEnd = isEmpty || (swr.data && swr.data[swr.data.length - 1]?.length < PER_PAGE);
  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. <div data-testid="grw-recent-changes">
  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={() => swr.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-recent-changes p-3">
  151. <ul className="list-group list-group-flush">
  152. <InfiniteScroll
  153. swrInifiniteResponse={swr}
  154. isReachingEnd={isReachingEnd}
  155. >
  156. {pages => pages.map(page => (
  157. isRecentChangesSidebarSmall
  158. ? <SmallPageItem key={page._id} page={page} />
  159. : <LargePageItem key={page._id} page={page} />
  160. ))
  161. }
  162. </InfiniteScroll>
  163. </ul>
  164. </div>
  165. </div>
  166. );
  167. };
  168. export default RecentChanges;