SearchResultContent.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import React, {
  2. FC, useCallback, useEffect, useRef, useState,
  3. } from 'react';
  4. import { getIdForRef } from '@growi/core';
  5. import type { IPageToDeleteWithMeta, IPageToRenameWithMeta } from '@growi/core';
  6. import { useTranslation } from 'next-i18next';
  7. import dynamic from 'next/dynamic';
  8. import { animateScroll } from 'react-scroll';
  9. import { DropdownItem } from 'reactstrap';
  10. import { debounce } from 'throttle-debounce';
  11. import { useLayoutFluidClassName } from '~/client/services/layout';
  12. import { exportAsMarkdown, updateContentWidth } from '~/client/services/page-operation';
  13. import { toastSuccess } from '~/client/util/toastr';
  14. import type { IPageWithSearchMeta } from '~/interfaces/search';
  15. import type { OnDuplicatedFunction, OnRenamedFunction, OnDeletedFunction } from '~/interfaces/ui';
  16. import { useCurrentUser, useIsContainerFluid } from '~/stores/context';
  17. import {
  18. usePageDuplicateModal, usePageRenameModal, usePageDeleteModal,
  19. } from '~/stores/modal';
  20. import { mutatePageList, mutatePageTree } from '~/stores/page-listing';
  21. import { useSearchResultOptions } from '~/stores/renderer';
  22. import { mutateSearching } from '~/stores/search';
  23. import type { AdditionalMenuItemsRendererProps, ForceHideMenuItems } from '../Common/Dropdown/PageItemControl';
  24. import { PagePathNav } from '../Common/PagePathNav';
  25. import { type RevisionLoaderProps } from '../Page/RevisionLoader';
  26. import { type PageCommentProps } from '../PageComment';
  27. import type { PageContentFooterProps } from '../PageContentFooter';
  28. import styles from './SearchResultContent.module.scss';
  29. const SubNavButtons = dynamic(() => import('../PageControls').then(mod => mod.PageControls), { ssr: false });
  30. const RevisionLoader = dynamic<RevisionLoaderProps>(() => import('../Page/RevisionLoader').then(mod => mod.RevisionLoader), { ssr: false });
  31. const PageComment = dynamic<PageCommentProps>(() => import('../PageComment').then(mod => mod.PageComment), { ssr: false });
  32. const PageContentFooter = dynamic<PageContentFooterProps>(() => import('../PageContentFooter').then(mod => mod.PageContentFooter), { ssr: false });
  33. type AdditionalMenuItemsProps = AdditionalMenuItemsRendererProps & {
  34. pageId: string,
  35. revisionId: string,
  36. }
  37. const AdditionalMenuItems = (props: AdditionalMenuItemsProps): JSX.Element => {
  38. const { t } = useTranslation();
  39. const { pageId, revisionId } = props;
  40. return (
  41. // Export markdown
  42. <DropdownItem
  43. onClick={() => exportAsMarkdown(pageId, revisionId, 'md')}
  44. className="grw-page-control-dropdown-item"
  45. >
  46. <i className="icon-fw icon-cloud-download grw-page-control-dropdown-icon"></i>
  47. {t('export_bulk.export_page_markdown')}
  48. </DropdownItem>
  49. );
  50. };
  51. const SCROLL_OFFSET_TOP = 30;
  52. const MUTATION_OBSERVER_CONFIG = { childList: true, subtree: true }; // omit 'subtree: true'
  53. type Props ={
  54. pageWithMeta : IPageWithSearchMeta,
  55. highlightKeywords?: string[],
  56. showPageControlDropdown?: boolean,
  57. forceHideMenuItems?: ForceHideMenuItems,
  58. }
  59. const scrollToFirstHighlightedKeyword = (scrollElement: HTMLElement): void => {
  60. // use querySelector to intentionally get the first element found
  61. const toElem = scrollElement.querySelector('.highlighted-keyword') as HTMLElement | null;
  62. if (toElem == null) {
  63. return;
  64. }
  65. const distance = toElem.getBoundingClientRect().top - scrollElement.getBoundingClientRect().top - SCROLL_OFFSET_TOP;
  66. animateScroll.scrollMore(distance, {
  67. containerId: scrollElement.id,
  68. duration: 200,
  69. });
  70. };
  71. const scrollToFirstHighlightedKeywordDebounced = debounce(500, scrollToFirstHighlightedKeyword);
  72. export const SearchResultContent: FC<Props> = (props: Props) => {
  73. const scrollElementRef = useRef<HTMLDivElement|null>(null);
  74. // *************************** Auto Scroll ***************************
  75. useEffect(() => {
  76. const scrollElement = scrollElementRef.current;
  77. if (scrollElement == null) return;
  78. const observer = new MutationObserver(() => {
  79. scrollToFirstHighlightedKeywordDebounced(scrollElement);
  80. });
  81. observer.observe(scrollElement, MUTATION_OBSERVER_CONFIG);
  82. // no cleanup function -- 2023.07.31 Yuki Takei
  83. // see: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe
  84. // > You can call observe() multiple times on the same MutationObserver
  85. // > to watch for changes to different parts of the DOM tree and/or different types of changes.
  86. });
  87. // ******************************* end *******************************
  88. const {
  89. pageWithMeta,
  90. highlightKeywords,
  91. showPageControlDropdown,
  92. forceHideMenuItems,
  93. } = props;
  94. const { t } = useTranslation();
  95. const page = pageWithMeta?.data;
  96. const { open: openDuplicateModal } = usePageDuplicateModal();
  97. const { open: openRenameModal } = usePageRenameModal();
  98. const { open: openDeleteModal } = usePageDeleteModal();
  99. const { data: rendererOptions } = useSearchResultOptions(pageWithMeta.data.path, highlightKeywords);
  100. const { data: currentUser } = useCurrentUser();
  101. const { data: isContainerFluid } = useIsContainerFluid();
  102. const [isExpandContentWidth, setIsExpandContentWidth] = useState(page.expandContentWidth);
  103. // TODO: determine className by the 'expandContentWidth' from the updated page
  104. const growiLayoutFluidClass = useLayoutFluidClassName(isExpandContentWidth);
  105. const duplicateItemClickedHandler = useCallback(async(pageToDuplicate) => {
  106. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  107. const duplicatedHandler: OnDuplicatedFunction = (fromPath, toPath) => {
  108. toastSuccess(t('duplicated_pages', { fromPath }));
  109. mutatePageTree();
  110. mutateSearching();
  111. mutatePageList();
  112. };
  113. openDuplicateModal(pageToDuplicate, { onDuplicated: duplicatedHandler });
  114. }, [openDuplicateModal, t]);
  115. const renameItemClickedHandler = useCallback((pageToRename: IPageToRenameWithMeta) => {
  116. const renamedHandler: OnRenamedFunction = (path) => {
  117. toastSuccess(t('renamed_pages', { path }));
  118. mutatePageTree();
  119. mutateSearching();
  120. mutatePageList();
  121. };
  122. openRenameModal(pageToRename, { onRenamed: renamedHandler });
  123. }, [openRenameModal, t]);
  124. const onDeletedHandler: OnDeletedFunction = useCallback((pathOrPathsToDelete, isRecursively, isCompletely) => {
  125. if (typeof pathOrPathsToDelete !== 'string') {
  126. return;
  127. }
  128. const path = pathOrPathsToDelete;
  129. if (isCompletely) {
  130. toastSuccess(t('deleted_pages_completely', { path }));
  131. }
  132. else {
  133. toastSuccess(t('deleted_pages', { path }));
  134. }
  135. mutatePageTree();
  136. mutateSearching();
  137. mutatePageList();
  138. }, [t]);
  139. const deleteItemClickedHandler = useCallback((pageToDelete: IPageToDeleteWithMeta) => {
  140. openDeleteModal([pageToDelete], { onDeleted: onDeletedHandler });
  141. }, [onDeletedHandler, openDeleteModal]);
  142. const switchContentWidthHandler = useCallback(async(pageId: string, value: boolean) => {
  143. await updateContentWidth(pageId, value);
  144. setIsExpandContentWidth(value);
  145. }, []);
  146. const RightComponent = useCallback(() => {
  147. if (page == null) {
  148. return <></>;
  149. }
  150. const revisionId = getIdForRef(page.revision);
  151. return (
  152. <div className="d-flex flex-column align-items-end justify-content-center px-2 py-1">
  153. <SubNavButtons
  154. pageId={page._id}
  155. revisionId={revisionId}
  156. path={page.path}
  157. expandContentWidth={isExpandContentWidth ?? isContainerFluid}
  158. showPageControlDropdown={showPageControlDropdown}
  159. forceHideMenuItems={forceHideMenuItems}
  160. additionalMenuItemRenderer={props => <AdditionalMenuItems {...props} pageId={page._id} revisionId={revisionId} />}
  161. onClickDuplicateMenuItem={duplicateItemClickedHandler}
  162. onClickRenameMenuItem={renameItemClickedHandler}
  163. onClickDeleteMenuItem={deleteItemClickedHandler}
  164. onClickSwitchContentWidth={switchContentWidthHandler}
  165. />
  166. </div>
  167. );
  168. }, [page, isExpandContentWidth, showPageControlDropdown, forceHideMenuItems, isContainerFluid,
  169. duplicateItemClickedHandler, renameItemClickedHandler, deleteItemClickedHandler, switchContentWidthHandler]);
  170. const isRenderable = page != null && rendererOptions != null;
  171. return (
  172. <div
  173. key={page._id}
  174. data-testid="search-result-content"
  175. className={`dynamic-layout-root ${growiLayoutFluidClass} search-result-content ${styles['search-result-content']}`}
  176. >
  177. <RightComponent />
  178. { isRenderable && (
  179. <div className="container-lg grw-container-convertible pt-2 pb-2">
  180. <PagePathNav pageId={page._id} pagePath={page.path} formerLinkClassName="small" latterLinkClassName="fs-3" />
  181. </div>
  182. ) }
  183. <div
  184. id="search-result-content-body-container"
  185. ref={scrollElementRef}
  186. className="search-result-content-body-container container-lg grw-container-convertible overflow-y-scroll"
  187. >
  188. { isRenderable && (
  189. <RevisionLoader
  190. rendererOptions={rendererOptions}
  191. pageId={page._id}
  192. revisionId={page.revision}
  193. />
  194. )}
  195. { isRenderable && (
  196. <PageComment
  197. rendererOptions={rendererOptions}
  198. pageId={page._id}
  199. pagePath={page.path}
  200. revision={page.revision}
  201. currentUser={currentUser}
  202. isReadOnly
  203. />
  204. )}
  205. { isRenderable && (
  206. <PageContentFooter
  207. page={page}
  208. />
  209. )}
  210. </div>
  211. </div>
  212. );
  213. };