Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(algolia): upgrade algoliasearch to v5 #10672

Merged
merged 8 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 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,17 @@
"copy:watch": "node ../../admin/scripts/copyUntypedFiles.js --watch"
},
"dependencies": {
"@docsearch/react": "^3.5.2",
"@algolia/client-search": "^5.13.0",
millotp marked this conversation as resolved.
Show resolved Hide resolved
"@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} 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: DocSearchModalProps['searchParameters'] = {
...props.searchParameters,
facetFilters,
indexName: props.indexName,
millotp marked this conversation as resolved.
Show resolved Hide resolved
};

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

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) => {
millotp marked this conversation as resolved.
Show resolved Hide resolved
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