RecentChanges.tsx 6.5 KB

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