RecentChanges.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 styles from './RecentChanges.module.scss';
  16. const logger = loggerFactory('growi:History');
  17. type PageItemProps = {
  18. page: IPageHasId,
  19. }
  20. const PageItemLower = memo(({ page }: PageItemProps): JSX.Element => {
  21. return (
  22. <div className="d-flex justify-content-between grw-recent-changes-item-lower pt-1">
  23. <div className="d-flex">
  24. <div className="footstamp-icon mr-1 d-inline-block"><FootstampIcon /></div>
  25. <div className="mr-2 grw-list-counts d-inline-block">{page.seenUsers.length}</div>
  26. <div className="icon-bubble mr-1 d-inline-block"></div>
  27. <div className="mr-2 grw-list-counts d-inline-block">{page.commentCount}</div>
  28. </div>
  29. <div className="grw-formatted-distance-date small mt-auto">
  30. <FormattedDistanceDate id={page._id} date={page.updatedAt} />
  31. </div>
  32. </div>
  33. );
  34. });
  35. PageItemLower.displayName = 'PageItemLower';
  36. const LargePageItem = memo(({ page }: PageItemProps): JSX.Element => {
  37. const dPagePath = new DevidedPagePath(page.path, false, true);
  38. const linkedPagePathFormer = new LinkedPagePath(dPagePath.former);
  39. const linkedPagePathLatter = new LinkedPagePath(dPagePath.latter);
  40. const FormerLink = () => (
  41. <div className="grw-page-path-text-muted-container small">
  42. <PagePathHierarchicalLink linkedPagePath={linkedPagePathFormer} />
  43. </div>
  44. );
  45. let locked;
  46. if (page.grant !== 1) {
  47. locked = <span><i className="icon-lock ml-2" /></span>;
  48. }
  49. const tags = page.tags;
  50. const tagElements = tags.map((tag) => {
  51. if (!isPopulated(tag)) {
  52. return <></>;
  53. }
  54. return (
  55. <Link key={tag.name} href={`/_search?q=tag:${tag.name}`}>
  56. <a className="grw-tag-label badge badge-secondary mr-2 small">
  57. {tag.name}
  58. </a>
  59. </Link>
  60. );
  61. });
  62. return (
  63. <li className={`list-group-item ${styles['list-group-item']} py-3 px-0`}>
  64. <div className="d-flex w-100">
  65. <UserPicture user={page.lastUpdateUser} size="md" noTooltip />
  66. <div className="flex-grow-1 ml-2">
  67. { !dPagePath.isRoot && <FormerLink /> }
  68. <h5 className="my-2">
  69. <PagePathHierarchicalLink linkedPagePath={linkedPagePathLatter} basePath={dPagePath.isRoot ? undefined : dPagePath.former} />
  70. {locked}
  71. </h5>
  72. <div className="grw-tag-labels mt-1 mb-2">
  73. { tagElements }
  74. </div>
  75. <PageItemLower page={page} />
  76. </div>
  77. </div>
  78. </li>
  79. );
  80. });
  81. LargePageItem.displayName = 'LargePageItem';
  82. const SmallPageItem = memo(({ page }: PageItemProps): JSX.Element => {
  83. const dPagePath = new DevidedPagePath(page.path, false, true);
  84. const linkedPagePathFormer = new LinkedPagePath(dPagePath.former);
  85. const linkedPagePathLatter = new LinkedPagePath(dPagePath.latter);
  86. const FormerLink = () => (
  87. <div className="grw-page-path-text-muted-container small">
  88. <PagePathHierarchicalLink linkedPagePath={linkedPagePathFormer} />
  89. </div>
  90. );
  91. let locked;
  92. if (page.grant !== 1) {
  93. locked = <span><i className="icon-lock ml-2" /></span>;
  94. }
  95. return (
  96. <li className="list-group-item py-2 px-0">
  97. <div className="d-flex w-100">
  98. <UserPicture user={page.lastUpdateUser} size="md" noTooltip />
  99. <div className="flex-grow-1 ml-2">
  100. { !dPagePath.isRoot && <FormerLink /> }
  101. <h5 className="my-0">
  102. <PagePathHierarchicalLink linkedPagePath={linkedPagePathLatter} basePath={dPagePath.isRoot ? undefined : dPagePath.former} />
  103. {locked}
  104. </h5>
  105. <PageItemLower page={page} />
  106. </div>
  107. </div>
  108. </li>
  109. );
  110. });
  111. SmallPageItem.displayName = 'SmallPageItem';
  112. const RecentChanges = (): JSX.Element => {
  113. const PER_PAGE = 20;
  114. const { t } = useTranslation();
  115. const swr = useSWRInifinitexRecentlyUpdated();
  116. const [isRecentChangesSidebarSmall, setIsRecentChangesSidebarSmall] = useState(false);
  117. const isEmpty = swr.data?.[0].length === 0;
  118. const isReachingEnd = isEmpty || (swr.data && swr.data[swr.data.length - 1]?.length < PER_PAGE);
  119. const retrieveSizePreferenceFromLocalStorage = useCallback(() => {
  120. if (window.localStorage.isRecentChangesSidebarSmall === 'true') {
  121. setIsRecentChangesSidebarSmall(true);
  122. }
  123. }, []);
  124. const changeSizeHandler = useCallback((e) => {
  125. setIsRecentChangesSidebarSmall(e.target.checked);
  126. window.localStorage.setItem('isRecentChangesSidebarSmall', e.target.checked);
  127. }, []);
  128. // componentDidMount
  129. useEffect(() => {
  130. retrieveSizePreferenceFromLocalStorage();
  131. }, [retrieveSizePreferenceFromLocalStorage]);
  132. return (
  133. <>
  134. <div className="grw-sidebar-content-header p-3 d-flex">
  135. <h3 className="mb-0 text-nowrap">{t('Recent Changes')}</h3>
  136. <button type="button" className="btn btn-sm ml-auto grw-btn-reload" onClick={() => swr.mutate()}>
  137. <i className="icon icon-reload"></i>
  138. </button>
  139. <div className="d-flex align-items-center">
  140. <div className={`grw-recent-changes-resize-button ${styles['grw-recent-changes-resize-button']} custom-control custom-switch ml-1`}>
  141. <input
  142. id="recentChangesResize"
  143. className="custom-control-input"
  144. type="checkbox"
  145. checked={isRecentChangesSidebarSmall}
  146. onChange={changeSizeHandler}
  147. />
  148. <label className="custom-control-label" htmlFor="recentChangesResize">
  149. </label>
  150. </div>
  151. </div>
  152. </div>
  153. <div className="grw-recent-changes p-3">
  154. <ul className="list-group list-group-flush">
  155. <InfiniteScroll
  156. swrInifiniteResponse={swr}
  157. isReachingEnd={isReachingEnd}
  158. >
  159. {pages => pages.map(page => (
  160. isRecentChangesSidebarSmall
  161. ? <SmallPageItem key={page._id} page={page} />
  162. : <LargePageItem key={page._id} page={page} />
  163. ))
  164. }
  165. </InfiniteScroll>
  166. </ul>
  167. </div>
  168. </>
  169. );
  170. };
  171. export default RecentChanges;