RecentChanges.tsx 5.8 KB

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