2
0

RecentChanges.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import React, {
  2. FC,
  3. useCallback, useEffect, useState,
  4. } from 'react';
  5. import PropTypes from 'prop-types';
  6. import { useTranslation } from 'react-i18next';
  7. import { UserPicture } from '@growi/ui';
  8. import { DevidedPagePath } from '@growi/core';
  9. import PagePathHierarchicalLink from '~/components/PagePathHierarchicalLink';
  10. import { useSWRxRecentlyUpdated } from '~/stores/page';
  11. import loggerFactory from '~/utils/logger';
  12. import LinkedPagePath from '~/models/linked-page-path';
  13. import FootstampIcon from '../FootstampIcon';
  14. import FormattedDistanceDate from '../FormattedDistanceDate';
  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">
  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. // when tag document is deleted from database directly tags includes null
  49. const tagElements = tags.includes(null)
  50. ? <></>
  51. : tags.map((tag) => {
  52. return (
  53. <a key={tag.name} href={`/_search?q=tag:${tag.name}`} className="grw-tag-label badge badge-secondary mr-2 small">
  54. {tag.name}
  55. </a>
  56. );
  57. });
  58. return (
  59. <li className="list-group-item py-3 px-0">
  60. <div className="d-flex w-100">
  61. <UserPicture user={page.lastUpdateUser} size="md" noTooltip />
  62. <div className="flex-grow-1 ml-2">
  63. { !dPagePath.isRoot && <FormerLink /> }
  64. <h5 className="my-2">
  65. <PagePathHierarchicalLink linkedPagePath={linkedPagePathLatter} basePath={dPagePath.isRoot ? undefined : dPagePath.former} />
  66. {locked}
  67. </h5>
  68. <div className="grw-tag-labels mt-1 mb-2">
  69. { tagElements }
  70. </div>
  71. <PageItemLower page={page} />
  72. </div>
  73. </div>
  74. </li>
  75. );
  76. }
  77. LargePageItem.propTypes = {
  78. page: PropTypes.any,
  79. };
  80. function SmallPageItem({ page }) {
  81. const dPagePath = new DevidedPagePath(page.path, false, true);
  82. const linkedPagePathFormer = new LinkedPagePath(dPagePath.former);
  83. const linkedPagePathLatter = new LinkedPagePath(dPagePath.latter);
  84. const FormerLink = () => (
  85. <div className="grw-page-path-text-muted-container small">
  86. <PagePathHierarchicalLink linkedPagePath={linkedPagePathFormer} />
  87. </div>
  88. );
  89. let locked;
  90. if (page.grant !== 1) {
  91. locked = <span><i className="icon-lock ml-2" /></span>;
  92. }
  93. return (
  94. <li className="list-group-item py-2 px-0">
  95. <div className="d-flex w-100">
  96. <UserPicture user={page.lastUpdateUser} size="md" noTooltip />
  97. <div className="flex-grow-1 ml-2">
  98. { !dPagePath.isRoot && <FormerLink /> }
  99. <h5 className="my-0">
  100. <PagePathHierarchicalLink linkedPagePath={linkedPagePathLatter} basePath={dPagePath.isRoot ? undefined : dPagePath.former} />
  101. {locked}
  102. </h5>
  103. <PageItemLower page={page} />
  104. </div>
  105. </div>
  106. </li>
  107. );
  108. }
  109. SmallPageItem.propTypes = {
  110. page: PropTypes.any,
  111. };
  112. const RecentChanges: FC<void> = () => {
  113. const { t } = useTranslation();
  114. const { data: pages, mutate } = useSWRxRecentlyUpdated();
  115. const [isRecentChangesSidebarSmall, setIsRecentChangesSidebarSmall] = useState(false);
  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. <>
  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={() => 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-sidebar-content-body grw-recent-changes p-3">
  151. <ul className="list-group list-group-flush">
  152. {(pages || []).map(page => (isRecentChangesSidebarSmall
  153. ? <SmallPageItem key={page._id} page={page} />
  154. : <LargePageItem key={page._id} page={page} />))}
  155. </ul>
  156. </div>
  157. </>
  158. );
  159. };
  160. export default RecentChanges;