diff --git a/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx b/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx index 564a8098a5..c6f017e92d 100644 --- a/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx +++ b/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx @@ -14,7 +14,7 @@ * limitations under the License. */ import React from 'react'; -import { TechDocsSearch } from './TechDocsSearch'; +import { buildInitialFilters, TechDocsSearch } from './TechDocsSearch'; import { act, fireEvent, @@ -89,7 +89,7 @@ describe('', () => { await singleResult; expect(querySpy).toBeCalledWith({ filters: { - kind: 'Testable', + kind: 'testable', name: 'test', namespace: 'testspace', }, @@ -108,7 +108,7 @@ describe('', () => { await waitFor(() => expect(querySpy).toBeCalledWith({ filters: { - kind: 'Testable', + kind: 'testable', name: 'test', namespace: 'testspace', }, @@ -120,3 +120,23 @@ describe('', () => { }); }); }); + +describe('buildInitialFilters', () => { + const filterEnt = { + name: 'Test', + kind: 'TestKind', + namespace: 'TeStNaMeSpAcE', + }; + it('should use filters as is when legacy path', () => { + const filters = buildInitialFilters(true, filterEnt); + expect(filters).toStrictEqual(filterEnt); + }); + it('should lowercase all filters for new approach', () => { + const filters = buildInitialFilters(false, filterEnt); + expect(filters).toStrictEqual({ + name: 'test', + kind: 'testkind', + namespace: 'testnamespace', + }); + }); +}); diff --git a/plugins/techdocs/src/reader/components/TechDocsSearch.tsx b/plugins/techdocs/src/reader/components/TechDocsSearch.tsx index 6f2df2cc55..2ec5559bb9 100644 --- a/plugins/techdocs/src/reader/components/TechDocsSearch.tsx +++ b/plugins/techdocs/src/reader/components/TechDocsSearch.tsx @@ -16,11 +16,11 @@ import React, { ChangeEvent, useEffect, useState } from 'react'; import { + CircularProgress, Grid, IconButton, InputAdornment, TextField, - CircularProgress, } from '@material-ui/core'; import Autocomplete from '@material-ui/lab/Autocomplete'; import { SearchContextProvider, useSearch } from '@backstage/plugin-search'; @@ -28,13 +28,15 @@ import { DocsResultListItem } from '../../components/DocsResultListItem'; import SearchIcon from '@material-ui/icons/Search'; import { useDebounce } from 'react-use'; import { useNavigate } from 'react-router'; +import { configApiRef, useApi } from '@backstage/core-plugin-api'; +type EntityId = { + name: string; + namespace: string; + kind: string; +}; type TechDocsSearchProps = { - entityId: { - name: string; - namespace: string; - kind: string; - }; + entityId: EntityId; debounceTime?: number; }; @@ -52,6 +54,17 @@ type TechDocsSearchResult = { document: TechDocsDoc; }; +export const buildInitialFilters = ( + legacyPaths: boolean, + entityId: EntityId, +) => { + return legacyPaths + ? entityId + : Object.entries(entityId).reduce((acc, [key, value]) => { + return { ...acc, [key]: value.toLowerCase() }; + }, {}); +}; + const TechDocsSearchBar = ({ entityId, debounceTime = 150, @@ -161,12 +174,17 @@ const TechDocsSearchBar = ({ ); }; + const TechDocsSearch = (props: TechDocsSearchProps) => { + const configApi = useApi(configApiRef); + const legacyPaths = configApi.getOptionalBoolean( + 'techdocs.legacyUseCaseSensitiveTripletPaths', + ); const initialState = { term: '', types: ['techdocs'], pageCursor: '', - filters: props.entityId, + filters: buildInitialFilters(legacyPaths || false, props.entityId), }; return (