RecentChanges.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import React, {
  2. memo, useCallback, useEffect, useState,
  3. } from 'react';
  4. import { DevidedPagePath, isPopulated } from '@growi/core';
  5. import { UserPicture, FootstampIcon } from '@growi/ui';
  6. import { useTranslation } from 'next-i18next';
  7. import Link from 'next/link';
  8. import PagePathHierarchicalLink from '~/components/PagePathHierarchicalLink';
  9. import { IPageHasId } from '~/interfaces/page';
  10. import LinkedPagePath from '~/models/linked-page-path';
  11. import { useSWRInifinitexRecentlyUpdated } from '~/stores/page-listing';
  12. import loggerFactory from '~/utils/logger';
  13. import FormattedDistanceDate from '../FormattedDistanceDate';
  14. import InfiniteScroll from './InfiniteScroll';
  15. import TagLabelsStyles from '../Page/TagLabels.module.scss';
  16. import styles from './RecentChanges.module.scss';
  17. const logger = loggerFactory('growi:History');
  18. type PageItemProps = {
  19. page: IPageHasId,
  20. }
  21. const PageItemLower = memo(({ page }: PageItemProps): JSX.Element => {
  22. return (
  23. <div className="d-flex justify-content-between grw-recent-changes-item-lower pt-1">
  24. <div className="d-flex">
  25. <div className="footstamp-icon mr-1 d-inline-block"><FootstampIcon /></div>
  26. <div className="mr-2 grw-list-counts d-inline-block">{page.seenUsers.length}</div>
  27. <div className="icon-bubble mr-1 d-inline-block"></div>
  28. <div className="mr-2 grw-list-counts d-inline-block">{page.commentCount}</div>
  29. </div>
  30. <div className="grw-formatted-distance-date small mt-auto">
  31. <FormattedDistanceDate id={page._id} date={page.updatedAt} />
  32. </div>
  33. </div>
  34. );
  35. });
  36. PageItemLower.displayName = 'PageItemLower';
  37. const LargePageItem = memo(({ page }: PageItemProps): JSX.Element => {
  38. const dPagePath = new DevidedPagePath(page.path, false, true);
  39. const linkedPagePathFormer = new LinkedPagePath(dPagePath.former);
  40. const linkedPagePathLatter = new LinkedPagePath(dPagePath.latter);
  41. const FormerLink = () => (
  42. <div className="grw-page-path-text-muted-container small">
  43. <PagePathHierarchicalLink linkedPagePath={linkedPagePathFormer} />
  44. </div>
  45. );
  46. let locked;
  47. if (page.grant !== 1) {
  48. locked = <span><i className="icon-lock ml-2" /></span>;
  49. }
  50. const tags = page.tags;
  51. const tagElements = tags.map((tag) => {
  52. if (!isPopulated(tag)) {
  53. return <></>;
  54. }
  55. return (
  56. <Link key={tag.name} href={`/_search?q=tag:${tag.name}`} prefetch={false}>
  57. <a className="grw-tag-label badge badge-secondary mr-2 small">
  58. {tag.name}
  59. </a>
  60. </Link>
  61. );
  62. });
  63. return (
  64. <li className={`list-group-item ${styles['list-group-item']} py-3 px-0`}>
  65. <div className="d-flex w-100">
  66. <UserPicture user={page.lastUpdateUser} size="md" noTooltip />
  67. <div className="flex-grow-1 ml-2">
  68. { !dPagePath.isRoot && <FormerLink /> }
  69. <h5 className="my-2">
  70. <PagePathHierarchicalLink linkedPagePath={linkedPagePathLatter} basePath={dPagePath.isRoot ? undefined : dPagePath.former} />
  71. {locked}
  72. </h5>
  73. <div className="grw-tag-labels mt-1 mb-2">
  74. { tagElements }
  75. </div>
  76. <PageItemLower page={page} />
  77. </div>
  78. </div>
  79. </li>
  80. );
  81. });
  82. LargePageItem.displayName = 'LargePageItem';
  83. const SmallPageItem = memo(({ page }: PageItemProps): JSX.Element => {
  84. const dPagePath = new DevidedPagePath(page.path, false, true);
  85. const linkedPagePathFormer = new LinkedPagePath(dPagePath.former);
  86. const linkedPagePathLatter = new LinkedPagePath(dPagePath.latter);
  87. const FormerLink = () => (
  88. <div className="grw-page-path-text-muted-container small">
  89. <PagePathHierarchicalLink linkedPagePath={linkedPagePathFormer} />
  90. </div>
  91. );
  92. let locked;
  93. if (page.grant !== 1) {
  94. locked = <span><i className="icon-lock ml-2" /></span>;
  95. }
  96. return (
  97. <li className="list-group-item py-2 px-0">
  98. <div className="d-flex w-100">
  99. <UserPicture user={page.lastUpdateUser} size="md" noTooltip />
  100. <div className="flex-grow-1 ml-2">
  101. { !dPagePath.isRoot && <FormerLink /> }
  102. <h5 className="my-0">
  103. <PagePathHierarchicalLink linkedPagePath={linkedPagePathLatter} basePath={dPagePath.isRoot ? undefined : dPagePath.former} />
  104. {locked}
  105. </h5>
  106. <PageItemLower page={page} />
  107. </div>
  108. </div>
  109. </li>
  110. );
  111. });
  112. SmallPageItem.displayName = 'SmallPageItem';
  113. const RecentChanges = (): JSX.Element => {
  114. const PER_PAGE = 20;
  115. const { t } = useTranslation();
  116. const swr = useSWRInifinitexRecentlyUpdated();
  117. const [isRecentChangesSidebarSmall, setIsRecentChangesSidebarSmall] = useState(false);
  118. const isEmpty = swr.data?.[0].length === 0;
  119. const isReachingEnd = isEmpty || (swr.data && swr.data[swr.data.length - 1]?.length < PER_PAGE);
  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. <div data-testid="grw-recent-changes">
  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={() => swr.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 ${styles['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-recent-changes p-3">
  155. <ul className="list-group list-group-flush">
  156. <InfiniteScroll
  157. swrInifiniteResponse={swr}
  158. isReachingEnd={isReachingEnd}
  159. >
  160. {pages => pages.map(page => (
  161. isRecentChangesSidebarSmall
  162. ? <SmallPageItem key={page._id} page={page} />
  163. : <LargePageItem key={page._id} page={page} />
  164. ))
  165. }
  166. </InfiniteScroll>
  167. </ul>
  168. </div>
  169. </div>
  170. );
  171. };
  172. export default RecentChanges;