SearchResultContent.tsx 9.7 KB

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