SearchMethodMenuItem.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React from 'react';
  2. import { useTranslation } from 'next-i18next';
  3. import { useCurrentPagePath } from '~/stores/page';
  4. import type { GetItemProps } from '../interfaces/downshift';
  5. import { SearchMenuItem } from './SearchMenuItem';
  6. type Props = {
  7. activeIndex: number | null
  8. searchKeyword: string
  9. getItemProps: GetItemProps
  10. }
  11. export const SearchMethodMenuItem = (props: Props): JSX.Element => {
  12. const {
  13. activeIndex, searchKeyword, getItemProps,
  14. } = props;
  15. const { t } = useTranslation('commons');
  16. const { data: currentPagePath } = useCurrentPagePath();
  17. const shouldShowMenuItem = searchKeyword.trim().length > 0;
  18. return (
  19. <>
  20. { shouldShowMenuItem && (
  21. <SearchMenuItem
  22. index={0}
  23. isActive={activeIndex === 0}
  24. getItemProps={getItemProps}
  25. url={`/_search?q=${searchKeyword}`}
  26. >
  27. <span className="material-symbols-outlined fs-4 me-3">search</span>
  28. <span>{searchKeyword}</span>
  29. <div className="ms-auto">
  30. <span>{t('search_method_menu_item.search_in_all')}</span>
  31. </div>
  32. </SearchMenuItem>
  33. )}
  34. <SearchMenuItem
  35. index={shouldShowMenuItem ? 1 : 0}
  36. isActive={activeIndex === (shouldShowMenuItem ? 1 : 0)}
  37. getItemProps={getItemProps}
  38. url={`/_search?q=prefix:${currentPagePath} ${searchKeyword}`}
  39. >
  40. <span className="material-symbols-outlined fs-4 me-3">search</span>
  41. <code>prefix: {currentPagePath}</code>
  42. <span className="ms-2">{searchKeyword}</span>
  43. <div className="ms-auto">
  44. <span>{t('search_method_menu_item.only_children_of_this_tree')}</span>
  45. </div>
  46. </SearchMenuItem>
  47. { shouldShowMenuItem && (
  48. <SearchMenuItem
  49. index={2}
  50. isActive={activeIndex === 2}
  51. getItemProps={getItemProps}
  52. url={`/_search?q="${searchKeyword}"`}
  53. >
  54. <span className="material-symbols-outlined fs-4 me-3">search</span>
  55. <span>{`"${searchKeyword}"`}</span>
  56. <div className="ms-auto">
  57. <span>{t('search_method_menu_item.exact_mutch')}</span>
  58. </div>
  59. </SearchMenuItem>
  60. ) }
  61. </>
  62. );
  63. };