From 1049294ba60f294fb6f68022ed3ecd8fda7b0287 Mon Sep 17 00:00:00 2001 From: Nick Coughlin Date: Thu, 30 May 2024 05:36:37 -0700 Subject: [PATCH] fix(theme): SearchPage should respect `contextualSearch: false` setting (#10178) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Sébastien Lorber Co-authored-by: sebastien --- .../src/theme/SearchPage/index.tsx | 36 +++++++++++-------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/packages/docusaurus-theme-search-algolia/src/theme/SearchPage/index.tsx b/packages/docusaurus-theme-search-algolia/src/theme/SearchPage/index.tsx index 323fb4f4586a..2f95bb284285 100644 --- a/packages/docusaurus-theme-search-algolia/src/theme/SearchPage/index.tsx +++ b/packages/docusaurus-theme-search-algolia/src/theme/SearchPage/index.tsx @@ -159,8 +159,9 @@ function SearchPageContent(): JSX.Element { i18n: {currentLocale}, } = useDocusaurusContext(); const { - algolia: {appId, apiKey, indexName}, + algolia: {appId, apiKey, indexName, contextualSearch}, } = useAlgoliaThemeConfig(); + const processSearchResultUrl = useSearchResultUrlProcessor(); const documentsFoundPlural = useDocumentsFoundPlural(); @@ -213,11 +214,16 @@ function SearchPageContent(): JSX.Element { initialSearchResultState, ); + // respect settings from the theme config for facets + const disjunctiveFacets = contextualSearch + ? ['language', 'docusaurus_tag'] + : []; + const algoliaClient = algoliaSearch(appId, apiKey); const algoliaHelper = algoliaSearchHelper(algoliaClient, indexName, { hitsPerPage: 15, advancedSyntax: true, - disjunctiveFacets: ['language', 'docusaurus_tag'], + disjunctiveFacets, }); algoliaHelper.on( @@ -313,17 +319,19 @@ function SearchPageContent(): JSX.Element { }); const makeSearch = useEvent((page: number = 0) => { - algoliaHelper.addDisjunctiveFacetRefinement('docusaurus_tag', 'default'); - algoliaHelper.addDisjunctiveFacetRefinement('language', currentLocale); - - Object.entries(docsSearchVersionsHelpers.searchVersions).forEach( - ([pluginId, searchVersion]) => { - algoliaHelper.addDisjunctiveFacetRefinement( - 'docusaurus_tag', - `docs-${pluginId}-${searchVersion}`, - ); - }, - ); + if (contextualSearch) { + algoliaHelper.addDisjunctiveFacetRefinement('docusaurus_tag', 'default'); + algoliaHelper.addDisjunctiveFacetRefinement('language', currentLocale); + + Object.entries(docsSearchVersionsHelpers.searchVersions).forEach( + ([pluginId, searchVersion]) => { + algoliaHelper.addDisjunctiveFacetRefinement( + 'docusaurus_tag', + `docs-${pluginId}-${searchVersion}`, + ); + }, + ); + } algoliaHelper.setQuery(searchQuery).setPage(page).search(); }); @@ -401,7 +409,7 @@ function SearchPageContent(): JSX.Element { /> - {docsSearchVersionsHelpers.versioningEnabled && ( + {contextualSearch && docsSearchVersionsHelpers.versioningEnabled && (