GlobalSearch.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import React, {
  2. useState, useCallback, useRef, useEffect,
  3. } from 'react';
  4. import assert from 'assert';
  5. import { pathUtils } from '@growi/core/dist/utils';
  6. import { useTranslation } from 'next-i18next';
  7. import { useRouter } from 'next/router';
  8. import { IFocusable } from '~/client/interfaces/focusable';
  9. import { IPageWithSearchMeta } from '~/interfaces/search';
  10. import {
  11. useIsSearchScopeChildrenAsDefault, useIsSearchServiceReachable,
  12. } from '~/stores/context';
  13. import { useCurrentPagePath } from '~/stores/page';
  14. import { useGlobalSearchFormRef } from '~/stores/ui';
  15. import SearchForm from '../SearchForm';
  16. import styles from './GlobalSearch.module.scss';
  17. export type GlobalSearchProps = {
  18. dropup?: boolean,
  19. }
  20. export const GlobalSearch = (props: GlobalSearchProps): JSX.Element => {
  21. const { t } = useTranslation('commons');
  22. const { dropup } = props;
  23. const { returnPathForURL } = pathUtils;
  24. const router = useRouter();
  25. const globalSearchFormRef = useRef<IFocusable>(null);
  26. useGlobalSearchFormRef(globalSearchFormRef);
  27. const { data: isSearchServiceReachable } = useIsSearchServiceReachable();
  28. const { data: isSearchScopeChildrenAsDefault } = useIsSearchScopeChildrenAsDefault();
  29. const { data: currentPagePath } = useCurrentPagePath();
  30. const [text, setText] = useState('');
  31. const [isScopeChildren, setScopeChildren] = useState<boolean|undefined>(isSearchScopeChildrenAsDefault ?? false);
  32. const [isFocused, setFocused] = useState<boolean>(false);
  33. useEffect(() => {
  34. setScopeChildren(isSearchScopeChildrenAsDefault);
  35. }, [isSearchScopeChildrenAsDefault]);
  36. const gotoPage = useCallback((data: IPageWithSearchMeta[]) => {
  37. assert(data.length > 0);
  38. const page = data[0].data; // should be single page selected
  39. // navigate to page
  40. if (page != null) {
  41. router.push(returnPathForURL(page.path, page._id));
  42. }
  43. }, [returnPathForURL, router]);
  44. const search = useCallback(() => {
  45. const url = new URL(window.location.href);
  46. url.pathname = '/_search';
  47. // construct search query
  48. let q = text;
  49. if (isScopeChildren) {
  50. q += ` prefix:${currentPagePath ?? window.location.pathname}`;
  51. }
  52. url.searchParams.append('q', q);
  53. router.push(url.href);
  54. }, [currentPagePath, isScopeChildren, router, text]);
  55. const scopeLabel = isScopeChildren
  56. ? t('header_search_box.label.This tree')
  57. : t('header_search_box.label.All pages');
  58. const isIndicatorShown = !isFocused && (text.length === 0);
  59. if (isScopeChildren == null || isSearchServiceReachable == null) {
  60. return <></>;
  61. }
  62. return (
  63. <div className={`grw-global-search ${styles['grw-global-search']} mb-0 d-print-none ${isSearchServiceReachable ? '' : 'has-error'}`}>
  64. <div className="input-group flex-nowrap">
  65. <div className={` ${dropup ? 'dropup' : ''}`}>
  66. <button
  67. className="btn btn-secondary dropdown-toggle py-0"
  68. type="button"
  69. data-bs-toggle="dropdown"
  70. aria-haspopup="true"
  71. data-testid="select-search-scope"
  72. >
  73. {scopeLabel}
  74. </button>
  75. <div className="dropdown-menu">
  76. <button
  77. className="dropdown-item"
  78. type="button"
  79. onClick={() => {
  80. setScopeChildren(false);
  81. globalSearchFormRef.current?.focus();
  82. }}
  83. >
  84. { t('header_search_box.item_label.All pages') }
  85. </button>
  86. <button
  87. data-tesid="search-current-tree"
  88. className="dropdown-item"
  89. type="button"
  90. onClick={() => {
  91. setScopeChildren(true);
  92. globalSearchFormRef.current?.focus();
  93. }}
  94. >
  95. { t('header_search_box.item_label.This tree') }
  96. </button>
  97. </div>
  98. </div>
  99. <SearchForm
  100. ref={globalSearchFormRef}
  101. isSearchServiceReachable={isSearchServiceReachable || false}
  102. dropup={dropup}
  103. onChange={gotoPage}
  104. onBlur={() => setFocused(false)}
  105. onFocus={() => setFocused(true)}
  106. onInputChange={text => setText(text)}
  107. onSubmit={search}
  108. />
  109. { isIndicatorShown && (
  110. <span className="grw-shortcut-key-indicator">
  111. <code className="bg-transparent text-muted">/</code>
  112. </span>
  113. ) }
  114. </div>
  115. </div>
  116. );
  117. };