RecentChanges.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import React, {
  2. memo, useCallback, useEffect, useState,
  3. } from 'react';
  4. import { isPopulated, type IPageHasId } from '@growi/core';
  5. import { DevidedPagePath } from '@growi/core/dist/models';
  6. import { UserPicture, FootstampIcon } from '@growi/ui/dist/components';
  7. import { useTranslation } from 'next-i18next';
  8. import { useKeywordManager } from '~/client/services/search-operation';
  9. import PagePathHierarchicalLink from '~/components/PagePathHierarchicalLink';
  10. import LinkedPagePath from '~/models/linked-page-path';
  11. import { useSWRINFxRecentlyUpdated } from '~/stores/page-listing';
  12. import loggerFactory from '~/utils/logger';
  13. import FormattedDistanceDate from '../FormattedDistanceDate';
  14. import InfiniteScroll from '../InfiniteScroll';
  15. import { SidebarHeaderReloadButton } from './SidebarHeaderReloadButton';
  16. import RecentChangesContentSkeleton from './Skeleton/RecentChangesContentSkeleton';
  17. import styles from './RecentChanges.module.scss';
  18. const logger = loggerFactory('growi:History');
  19. type PageItemLowerProps = {
  20. page: IPageHasId,
  21. }
  22. type PageItemProps = PageItemLowerProps & {
  23. isSmall: boolean
  24. onClickTag?: (tagName: string) => void,
  25. }
  26. const PageItemLower = memo(({ page }: PageItemLowerProps): JSX.Element => {
  27. return (
  28. <div className="d-flex justify-content-between grw-recent-changes-item-lower pt-1">
  29. <div className="d-flex">
  30. <div className="footstamp-icon mr-1 d-inline-block"><FootstampIcon /></div>
  31. <div className="mr-2 grw-list-counts d-inline-block">{page.seenUsers.length}</div>
  32. <div className="icon-bubble mr-1 d-inline-block"></div>
  33. <div className="mr-2 grw-list-counts d-inline-block">{page.commentCount}</div>
  34. </div>
  35. <div className="grw-formatted-distance-date small mt-auto" data-vrt-blackout-datetime>
  36. <FormattedDistanceDate id={page._id} date={page.updatedAt} />
  37. </div>
  38. </div>
  39. );
  40. });
  41. PageItemLower.displayName = 'PageItemLower';
  42. const PageItem = memo(({ page, isSmall, onClickTag }: PageItemProps): JSX.Element => {
  43. const dPagePath = new DevidedPagePath(page.path, false, true);
  44. const linkedPagePathFormer = new LinkedPagePath(dPagePath.former);
  45. const linkedPagePathLatter = new LinkedPagePath(dPagePath.latter);
  46. const FormerLink = () => (
  47. <div className="grw-page-path-text-muted-container small">
  48. <PagePathHierarchicalLink linkedPagePath={linkedPagePathFormer} />
  49. </div>
  50. );
  51. let locked;
  52. if (page.grant !== 1) {
  53. locked = <span><i className="icon-lock ml-2" /></span>;
  54. }
  55. const tags = page.tags;
  56. const tagElements = tags.map((tag) => {
  57. if (!isPopulated(tag)) {
  58. return <></>;
  59. }
  60. return (
  61. <a
  62. key={tag.name}
  63. type="button"
  64. className="grw-tag-label badge badge-secondary mr-2 small"
  65. onClick={() => onClickTag?.(tag.name)}
  66. >
  67. {tag.name}
  68. </a>
  69. );
  70. });
  71. return (
  72. <li className={`list-group-item ${styles['list-group-item']} ${isSmall ? 'py-2' : 'py-3'} px-0`}>
  73. <div className="d-flex w-100">
  74. <UserPicture user={page.lastUpdateUser} size="md" noTooltip />
  75. <div className="flex-grow-1 ml-2">
  76. { !dPagePath.isRoot && <FormerLink /> }
  77. <h5 className={isSmall ? 'my-0 text-truncate' : 'my-2'}>
  78. <PagePathHierarchicalLink linkedPagePath={linkedPagePathLatter} basePath={dPagePath.isRoot ? undefined : dPagePath.former} />
  79. {locked}
  80. </h5>
  81. {!isSmall && (
  82. <div className="grw-tag-labels mt-1 mb-2">
  83. { tagElements }
  84. </div>
  85. )}
  86. <PageItemLower page={page} />
  87. </div>
  88. </div>
  89. </li>
  90. );
  91. });
  92. PageItem.displayName = 'PageItem';
  93. const RecentChanges = (): JSX.Element => {
  94. const PER_PAGE = 20;
  95. const { t } = useTranslation();
  96. const swrInifinitexRecentlyUpdated = useSWRINFxRecentlyUpdated(PER_PAGE);
  97. const {
  98. data, mutate, isLoading,
  99. } = swrInifinitexRecentlyUpdated;
  100. const { pushState } = useKeywordManager();
  101. const [isRecentChangesSidebarSmall, setIsRecentChangesSidebarSmall] = useState(false);
  102. const isEmpty = data?.[0]?.pages.length === 0;
  103. const isReachingEnd = isEmpty || (data != null && data[data.length - 1]?.pages.length < PER_PAGE);
  104. const retrieveSizePreferenceFromLocalStorage = useCallback(() => {
  105. if (window.localStorage.isRecentChangesSidebarSmall === 'true') {
  106. setIsRecentChangesSidebarSmall(true);
  107. }
  108. }, []);
  109. const changeSizeHandler = useCallback((e) => {
  110. setIsRecentChangesSidebarSmall(e.target.checked);
  111. window.localStorage.setItem('isRecentChangesSidebarSmall', e.target.checked);
  112. }, []);
  113. // componentDidMount
  114. useEffect(() => {
  115. retrieveSizePreferenceFromLocalStorage();
  116. }, [retrieveSizePreferenceFromLocalStorage]);
  117. return (
  118. <div className="px-3" data-testid="grw-recent-changes">
  119. <div className="grw-sidebar-content-header py-3 d-flex">
  120. <h3 className="mb-0 text-nowrap">{t('Recent Changes')}</h3>
  121. <SidebarHeaderReloadButton onClick={() => mutate()} />
  122. <div className="d-flex align-items-center">
  123. <div className={`grw-recent-changes-resize-button ${styles['grw-recent-changes-resize-button']} custom-control custom-switch ml-1`}>
  124. <input
  125. id="recentChangesResize"
  126. className="custom-control-input"
  127. type="checkbox"
  128. checked={isRecentChangesSidebarSmall}
  129. onChange={changeSizeHandler}
  130. />
  131. <label className="custom-control-label" htmlFor="recentChangesResize">
  132. </label>
  133. </div>
  134. </div>
  135. </div>
  136. {
  137. isLoading ? <RecentChangesContentSkeleton /> : (
  138. <div className="grw-recent-changes py-3">
  139. <ul className="list-group list-group-flush">
  140. <InfiniteScroll
  141. swrInifiniteResponse={swrInifinitexRecentlyUpdated}
  142. isReachingEnd={isReachingEnd}
  143. >
  144. { data != null && data.map(apiResult => apiResult.pages).flat()
  145. .map(page => (
  146. <PageItem key={page._id} page={page} isSmall={isRecentChangesSidebarSmall} onClickTag={tagName => pushState(`tag:${tagName}`)} />
  147. ))
  148. }
  149. </InfiniteScroll>
  150. </ul>
  151. </div>
  152. )
  153. }
  154. </div>
  155. );
  156. };
  157. export default RecentChanges;