SearchResultContent.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import type { FC } from 'react';
  2. import React, {
  3. useCallback, useEffect, useRef,
  4. } from 'react';
  5. import { getIdForRef } from '@growi/core';
  6. import type { IPageToDeleteWithMeta, IPageToRenameWithMeta } from '@growi/core';
  7. import { useTranslation } from 'next-i18next';
  8. import dynamic from 'next/dynamic';
  9. import { animateScroll } from 'react-scroll';
  10. import { DropdownItem } from 'reactstrap';
  11. import { debounce } from 'throttle-debounce';
  12. import { useShouldExpandContent } from '~/client/services/layout';
  13. import { exportAsMarkdown, updateContentWidth } from '~/client/services/page-operation';
  14. import { toastSuccess } from '~/client/util/toastr';
  15. import type { IPageWithSearchMeta } from '~/interfaces/search';
  16. import type { OnDuplicatedFunction, OnRenamedFunction, OnDeletedFunction } from '~/interfaces/ui';
  17. import { useCurrentUser } from '~/stores/context';
  18. import {
  19. usePageDuplicateModal, usePageRenameModal, usePageDeleteModal,
  20. } from '~/stores/modal';
  21. import { mutatePageList, mutatePageTree } from '~/stores/page-listing';
  22. import { useSearchResultOptions } from '~/stores/renderer';
  23. import { mutateSearching } from '~/stores/search';
  24. import type { AdditionalMenuItemsRendererProps, ForceHideMenuItems } from '../Common/Dropdown/PageItemControl';
  25. import { PagePathNav } from '../Common/PagePathNav';
  26. import { type RevisionLoaderProps } from '../Page/RevisionLoader';
  27. import type { PageContentFooterProps } from '../PageContentFooter';
  28. import styles from './SearchResultContent.module.scss';
  29. const moduleClass = styles['search-result-content'];
  30. const _fluidLayoutClass = styles['fluid-layout'];
  31. const PageControls = dynamic(() => import('../PageControls').then(mod => mod.PageControls), { ssr: false });
  32. const RevisionLoader = dynamic<RevisionLoaderProps>(() => import('../Page/RevisionLoader').then(mod => mod.RevisionLoader), { ssr: false });
  33. const PageComment = dynamic(() => 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. <span className="material-symbols-outlined me-1 grw-page-control-dropdown-icon">cloud_download</span>
  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 shouldExpandContent = useShouldExpandContent(page);
  104. const duplicateItemClickedHandler = useCallback(async(pageToDuplicate) => {
  105. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  106. const duplicatedHandler: OnDuplicatedFunction = (fromPath, toPath) => {
  107. toastSuccess(t('duplicated_pages', { fromPath }));
  108. mutatePageTree();
  109. mutateSearching();
  110. mutatePageList();
  111. };
  112. openDuplicateModal(pageToDuplicate, { onDuplicated: duplicatedHandler });
  113. }, [openDuplicateModal, t]);
  114. const renameItemClickedHandler = useCallback((pageToRename: IPageToRenameWithMeta) => {
  115. const renamedHandler: OnRenamedFunction = (path) => {
  116. toastSuccess(t('renamed_pages', { path }));
  117. mutatePageTree();
  118. mutateSearching();
  119. mutatePageList();
  120. };
  121. openRenameModal(pageToRename, { onRenamed: renamedHandler });
  122. }, [openRenameModal, t]);
  123. const onDeletedHandler: OnDeletedFunction = useCallback((pathOrPathsToDelete, isRecursively, isCompletely) => {
  124. if (typeof pathOrPathsToDelete !== 'string') {
  125. return;
  126. }
  127. const path = pathOrPathsToDelete;
  128. if (isCompletely) {
  129. toastSuccess(t('deleted_pages_completely', { path }));
  130. }
  131. else {
  132. toastSuccess(t('deleted_pages', { path }));
  133. }
  134. mutatePageTree();
  135. mutateSearching();
  136. mutatePageList();
  137. }, [t]);
  138. const deleteItemClickedHandler = useCallback((pageToDelete: IPageToDeleteWithMeta) => {
  139. openDeleteModal([pageToDelete], { onDeleted: onDeletedHandler });
  140. }, [onDeletedHandler, openDeleteModal]);
  141. const switchContentWidthHandler = useCallback(async(pageId: string, value: boolean) => {
  142. await updateContentWidth(pageId, value);
  143. // TODO: revalidate page data and update shouldExpandContent
  144. }, []);
  145. const RightComponent = useCallback(() => {
  146. if (page == null) {
  147. return <></>;
  148. }
  149. const revisionId = page.revision != null ? getIdForRef(page.revision) : null;
  150. const additionalMenuItemRenderer = revisionId != null
  151. ? props => <AdditionalMenuItems {...props} pageId={page._id} revisionId={revisionId} />
  152. : undefined;
  153. return (
  154. <div className="d-flex flex-column align-items-end justify-content-center px-2 py-1">
  155. <PageControls
  156. pageId={page._id}
  157. revisionId={revisionId}
  158. path={page.path}
  159. expandContentWidth={shouldExpandContent}
  160. showPageControlDropdown={showPageControlDropdown}
  161. forceHideMenuItems={forceHideMenuItems}
  162. additionalMenuItemRenderer={additionalMenuItemRenderer}
  163. onClickDuplicateMenuItem={duplicateItemClickedHandler}
  164. onClickRenameMenuItem={renameItemClickedHandler}
  165. onClickDeleteMenuItem={deleteItemClickedHandler}
  166. onClickSwitchContentWidth={switchContentWidthHandler}
  167. />
  168. </div>
  169. );
  170. }, [page, shouldExpandContent, showPageControlDropdown, forceHideMenuItems,
  171. duplicateItemClickedHandler, renameItemClickedHandler, deleteItemClickedHandler, switchContentWidthHandler]);
  172. const fluidLayoutClass = shouldExpandContent ? _fluidLayoutClass : '';
  173. return (
  174. <div
  175. key={page._id}
  176. data-testid="search-result-content"
  177. className={`dynamic-layout-root ${moduleClass} ${fluidLayoutClass}`}
  178. >
  179. <RightComponent />
  180. <div className="container-lg grw-container-convertible pt-2 pb-2">
  181. <PagePathNav pageId={page._id} pagePath={page.path} formerLinkClassName="small" latterLinkClassName="fs-3 text-truncate" />
  182. </div>
  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. { page.revision != null && rendererOptions != null && (
  189. <RevisionLoader
  190. rendererOptions={rendererOptions}
  191. pageId={page._id}
  192. revisionId={page.revision}
  193. />
  194. )}
  195. { page.revision != null && (
  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. <PageContentFooter
  206. page={page}
  207. />
  208. </div>
  209. </div>
  210. );
  211. };