SearchPageBase.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import type { ForwardRefRenderFunction } from 'react';
  2. import React, {
  3. forwardRef, useEffect, useImperativeHandle, useRef, useState,
  4. } from 'react';
  5. import { LoadingSpinner } from '@growi/ui/dist/components';
  6. import { useTranslation } from 'next-i18next';
  7. import dynamic from 'next/dynamic';
  8. import type { ISelectableAll } from '~/client/interfaces/selectable-all';
  9. import { toastSuccess } from '~/client/util/toastr';
  10. import type { IFormattedSearchResult, IPageWithSearchMeta } from '~/interfaces/search';
  11. import type { OnDeletedFunction } from '~/interfaces/ui';
  12. import {
  13. useIsGuestUser, useIsReadOnlyUser, useIsSearchServiceConfigured, useIsSearchServiceReachable,
  14. } from '~/stores-universal/context';
  15. import { usePageDeleteModal } from '~/stores/modal';
  16. import { mutatePageTree, mutateRecentlyUpdated } from '~/stores/page-listing';
  17. import type { ForceHideMenuItems } from '../Common/Dropdown/PageItemControl';
  18. // Do not import with next/dynamic
  19. // see: https://github.com/weseek/growi/pull/7923
  20. import { SearchResultList } from './SearchResultList';
  21. import styles from './SearchPageBase.module.scss';
  22. // https://regex101.com/r/brrkBu/1
  23. const highlightKeywordsSplitter = new RegExp('"[^"]+"|[^\u{20}\u{3000}]+', 'ug');
  24. export interface IReturnSelectedPageIds {
  25. getSelectedPageIds?: () => Set<string>,
  26. }
  27. type Props = {
  28. className?: string,
  29. pages?: IPageWithSearchMeta[],
  30. searchingKeyword?: string,
  31. forceHideMenuItems?: ForceHideMenuItems,
  32. onSelectedPagesByCheckboxesChanged?: (selectedCount: number, totalCount: number) => void,
  33. searchControl: React.ReactNode,
  34. searchResultListHead: React.ReactElement,
  35. searchPager: React.ReactNode,
  36. }
  37. const SearchResultContent = dynamic(() => import('./SearchResultContent').then(mod => mod.SearchResultContent), {
  38. ssr: false,
  39. loading: () => <></>,
  40. });
  41. const SearchPageBaseSubstance: ForwardRefRenderFunction<ISelectableAll & IReturnSelectedPageIds, Props> = (props:Props, ref) => {
  42. const {
  43. className,
  44. pages,
  45. searchingKeyword,
  46. forceHideMenuItems,
  47. onSelectedPagesByCheckboxesChanged,
  48. searchControl, searchResultListHead, searchPager,
  49. } = props;
  50. const searchResultListRef = useRef<ISelectableAll|null>(null);
  51. const { data: isGuestUser } = useIsGuestUser();
  52. const { data: isReadOnlyUser } = useIsReadOnlyUser();
  53. const { data: isSearchServiceConfigured } = useIsSearchServiceConfigured();
  54. const { data: isSearchServiceReachable } = useIsSearchServiceReachable();
  55. const [selectedPageIdsByCheckboxes] = useState<Set<string>>(new Set());
  56. // const [allPageIds] = useState<Set<string>>(new Set());
  57. const [selectedPageWithMeta, setSelectedPageWithMeta] = useState<IPageWithSearchMeta | undefined>();
  58. // publish selectAll()
  59. useImperativeHandle(ref, () => ({
  60. selectAll: () => {
  61. const instance = searchResultListRef.current;
  62. if (instance != null) {
  63. instance.selectAll();
  64. }
  65. if (pages != null) {
  66. pages.forEach(page => selectedPageIdsByCheckboxes.add(page.data._id));
  67. }
  68. },
  69. deselectAll: () => {
  70. const instance = searchResultListRef.current;
  71. if (instance != null) {
  72. instance.deselectAll();
  73. }
  74. selectedPageIdsByCheckboxes.clear();
  75. },
  76. getSelectedPageIds: () => {
  77. return selectedPageIdsByCheckboxes;
  78. },
  79. }));
  80. const checkboxChangedHandler = (isChecked: boolean, pageId: string) => {
  81. if (pages == null || pages.length === 0) {
  82. return;
  83. }
  84. if (isChecked) {
  85. selectedPageIdsByCheckboxes.add(pageId);
  86. }
  87. else {
  88. selectedPageIdsByCheckboxes.delete(pageId);
  89. }
  90. if (onSelectedPagesByCheckboxesChanged != null) {
  91. onSelectedPagesByCheckboxesChanged(selectedPageIdsByCheckboxes.size, pages.length);
  92. }
  93. };
  94. // select first item on load
  95. useEffect(() => {
  96. if ((pages == null || pages.length === 0)) {
  97. setSelectedPageWithMeta(undefined);
  98. }
  99. else if ((pages != null && pages.length > 0)) {
  100. setSelectedPageWithMeta(pages[0]);
  101. }
  102. }, [pages, setSelectedPageWithMeta]);
  103. // reset selectedPageIdsByCheckboxes
  104. useEffect(() => {
  105. if (pages == null) {
  106. return;
  107. }
  108. if (pages.length > 0) {
  109. selectedPageIdsByCheckboxes.clear();
  110. }
  111. if (onSelectedPagesByCheckboxesChanged != null) {
  112. onSelectedPagesByCheckboxesChanged(selectedPageIdsByCheckboxes.size, pages.length);
  113. }
  114. }, [onSelectedPagesByCheckboxesChanged, pages, selectedPageIdsByCheckboxes]);
  115. if (!isSearchServiceConfigured) {
  116. return (
  117. <div className="container-lg grw-container-convertible">
  118. <div className="row mt-5">
  119. <div className="col text-muted">
  120. <h1>Search service is not configured in this system.</h1>
  121. </div>
  122. </div>
  123. </div>
  124. );
  125. }
  126. if (!isSearchServiceReachable) {
  127. return (
  128. <div className="container-lg grw-container-convertible">
  129. <div className="row mt-5">
  130. <div className="col text-muted">
  131. <h1>Search service occures errors. Please contact to administrators of this system.</h1>
  132. </div>
  133. </div>
  134. </div>
  135. );
  136. }
  137. const highlightKeywords = searchingKeyword != null
  138. // Remove double quotation marks before and after a keyword if present
  139. // https://regex101.com/r/4QKBwg/1
  140. ? searchingKeyword.match(highlightKeywordsSplitter)?.map(keyword => keyword.replace(/^"(.*)"$/, '$1')) ?? undefined
  141. : undefined;
  142. return (
  143. <div className={`${className ?? ''} search-result-base flex-grow-1 d-flex flex-expand-vh-100`} data-testid="search-result-base">
  144. <div className="flex-expand-vert border boder-gray search-result-list" id="search-result-list">
  145. {searchControl}
  146. <div className="overflow-y-scroll">
  147. {/* Loading */}
  148. { pages == null && (
  149. <div className="mw-0 flex-grow-1 flex-basis-0 m-5 text-muted text-center">
  150. <LoadingSpinner className="me-1 fs-3" />
  151. </div>
  152. ) }
  153. {/* Loaded */}
  154. { pages != null && (
  155. <>
  156. <div className="my-3 px-md-4 px-3">
  157. {searchResultListHead}
  158. </div>
  159. { pages.length > 0 && (
  160. <div className={`page-list ${styles['page-list']} px-md-4`}>
  161. <SearchResultList
  162. ref={searchResultListRef}
  163. pages={pages}
  164. selectedPageId={selectedPageWithMeta?.data._id}
  165. forceHideMenuItems={forceHideMenuItems}
  166. onPageSelected={page => (setSelectedPageWithMeta(page))}
  167. onCheckboxChanged={checkboxChangedHandler}
  168. />
  169. </div>
  170. ) }
  171. <div className="my-4 d-flex justify-content-center">
  172. {searchPager}
  173. </div>
  174. </>
  175. ) }
  176. </div>
  177. </div>
  178. <div className={`${styles['search-result-content']} flex-expand-vert d-none d-lg-flex`}>
  179. {pages != null && pages.length !== 0 && selectedPageWithMeta != null && (
  180. <SearchResultContent
  181. pageWithMeta={selectedPageWithMeta}
  182. highlightKeywords={highlightKeywords}
  183. showPageControlDropdown={!(isGuestUser || isReadOnlyUser)}
  184. forceHideMenuItems={forceHideMenuItems}
  185. />
  186. )}
  187. </div>
  188. </div>
  189. );
  190. };
  191. type VoidFunction = () => void;
  192. export const usePageDeleteModalForBulkDeletion = (
  193. data: IFormattedSearchResult | undefined,
  194. ref: React.MutableRefObject<(ISelectableAll & IReturnSelectedPageIds) | null>,
  195. onDeleted?: OnDeletedFunction,
  196. ): VoidFunction => {
  197. const { t } = useTranslation();
  198. const { open: openDeleteModal } = usePageDeleteModal();
  199. return () => {
  200. if (data == null) {
  201. return;
  202. }
  203. const instance = ref.current;
  204. if (instance == null || instance.getSelectedPageIds == null) {
  205. return;
  206. }
  207. const selectedPageIds = instance.getSelectedPageIds();
  208. if (selectedPageIds.size === 0) {
  209. return;
  210. }
  211. const selectedPages = data.data
  212. .filter(pageWithMeta => selectedPageIds.has(pageWithMeta.data._id));
  213. openDeleteModal(selectedPages, {
  214. onDeleted: (...args) => {
  215. const path = args[0];
  216. const isCompletely = args[2];
  217. if (path == null || isCompletely == null) {
  218. toastSuccess(t('deleted_page'));
  219. }
  220. else {
  221. toastSuccess(t('deleted_pages_completely', { path }));
  222. }
  223. mutatePageTree();
  224. mutateRecentlyUpdated();
  225. if (onDeleted != null) {
  226. onDeleted(...args);
  227. }
  228. },
  229. });
  230. };
  231. };
  232. export const SearchPageBase = forwardRef(SearchPageBaseSubstance);