Skip to content

Commit

Permalink
chore(algolia): upgrade to the new major
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Nov 12, 2024
1 parent 0c86355 commit 068da51
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 196 deletions.
6 changes: 3 additions & 3 deletions packages/docusaurus-theme-search-algolia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
"copy:watch": "node ../../admin/scripts/copyUntypedFiles.js --watch"
},
"dependencies": {
"@docsearch/react": "^3.5.2",
"@docsearch/react": "^3.8.0",
"@docusaurus/core": "3.6.1",
"@docusaurus/logger": "3.6.1",
"@docusaurus/plugin-content-docs": "3.6.1",
"@docusaurus/theme-common": "3.6.1",
"@docusaurus/theme-translations": "3.6.1",
"@docusaurus/utils": "3.6.1",
"@docusaurus/utils-validation": "3.6.1",
"algoliasearch": "^4.18.0",
"algoliasearch-helper": "^3.13.3",
"algoliasearch": "^5.13.0",
"algoliasearch-helper": "^3.22.5",
"clsx": "^2.0.0",
"eta": "^2.2.0",
"fs-extra": "^11.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ import {
import Translate from '@docusaurus/Translate';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import translations from '@theme/SearchTranslations';

import type {AutocompleteState} from '@algolia/autocomplete-core';
import type {
InternalDocSearchHit,
DocSearchModal as DocSearchModalType,
DocSearchModalProps,
} from '@docsearch/react';
import type {
InternalDocSearchHit,
StoredDocSearchHit,
} from '@docsearch/react/dist/esm/types';
import type {SearchClient} from 'algoliasearch/lite';
} from '@docsearch/react';

import type {AutocompleteState} from '@algolia/autocomplete-core';
import type {FacetFilters, SearchForHits} from 'algoliasearch/lite';

type DocSearchProps = Omit<
DocSearchModalProps,
Expand Down Expand Up @@ -74,16 +72,10 @@ function ResultsFooter({state, onClose}: ResultsFooterProps) {
);
}

type FacetFilters = Required<
Required<DocSearchProps>['searchParameters']
>['facetFilters'];

function mergeFacetFilters(f1: FacetFilters, f2: FacetFilters): FacetFilters {
const normalize = (
f: FacetFilters,
): readonly string[] | readonly (string | readonly string[])[] =>
const normalize = (f: FacetFilters): FacetFilters =>
typeof f === 'string' ? [f] : f;
return [...normalize(f1), ...normalize(f2)] as FacetFilters;
return [...normalize(f1), ...normalize(f2)];
}

function DocSearch({
Expand All @@ -98,7 +90,9 @@ function DocSearch({
useAlgoliaContextualFacetFilters() as FacetFilters;

const configFacetFilters: FacetFilters =
props.searchParameters?.facetFilters ?? [];
('facetFilters' in props.searchParameters!
? props.searchParameters?.facetFilters
: []) ?? [];

const facetFilters: FacetFilters = contextualSearch
? // Merge contextual search filters with config filters
Expand All @@ -107,9 +101,10 @@ function DocSearch({
configFacetFilters;

// We let user override default searchParameters if she wants to
const searchParameters: DocSearchProps['searchParameters'] = {
const searchParameters: SearchForHits = {
...props.searchParameters,
facetFilters,
indexName: props.indexName,
};

const history = useHistory();
Expand Down Expand Up @@ -152,6 +147,7 @@ function DocSearch({
const closeModal = useCallback(() => {
setIsOpen(false);
searchButtonRef.current?.focus();
setInitialQuery(undefined);
}, []);

const handleInput = useCallback(
Expand Down Expand Up @@ -201,8 +197,10 @@ function DocSearch({
[closeModal],
);

const transformSearchClient = useCallback(
(searchClient: SearchClient) => {
const transformSearchClient = useCallback<
NonNullable<DocSearchModalProps['transformSearchClient']>
>(
(searchClient) => {
searchClient.addAlgoliaAgent(
'docusaurus',
siteMetadata.docusaurusVersion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import React, {useEffect, useReducer, useRef, useState} from 'react';
import clsx from 'clsx';

import algoliaSearchHelper from 'algoliasearch-helper';
import algoliaSearch from 'algoliasearch/lite';
import {liteClient} from 'algoliasearch/lite';

import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import Head from '@docusaurus/Head';
Expand Down Expand Up @@ -219,7 +219,7 @@ function SearchPageContent(): JSX.Element {
? ['language', 'docusaurus_tag']
: [];

const algoliaClient = algoliaSearch(appId, apiKey);
const algoliaClient = liteClient(appId, apiKey);
const algoliaHelper = algoliaSearchHelper(algoliaClient, indexName, {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: why errors happens after upgrading to TS 5.5 ?
Expand Down
Loading

0 comments on commit 068da51

Please sign in to comment.