diff --git a/.changeset/techdocs-war-on-war.md b/.changeset/techdocs-war-on-war.md
new file mode 100644
index 0000000000..28722feed3
--- /dev/null
+++ b/.changeset/techdocs-war-on-war.md
@@ -0,0 +1,5 @@
+---
+'@backstage/plugin-techdocs': patch
+---
+
+Fixed a bug that could cause searches in the in-context TechDocs search bar to show results from a different TechDocs site.
diff --git a/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx b/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx
index 5b3d714a04..b4233734aa 100644
--- a/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx
+++ b/plugins/techdocs/src/reader/components/TechDocsSearch.test.tsx
@@ -23,7 +23,7 @@ import {
waitFor,
within,
} from '@testing-library/react';
-import React from 'react';
+import React, { useState } from 'react';
import { TechDocsSearch } from './TechDocsSearch';
const entityId = {
@@ -70,6 +70,7 @@ describe('', () => {
expect(rendered.getByTestId('techdocs-search-bar')).toBeInTheDocument();
});
});
+
it('should trigger query when autocomplete input changed', async () => {
const query = () => singleResult;
const querySpy = jest.fn(query);
@@ -119,4 +120,61 @@ describe('', () => {
);
});
});
+
+ it('should update filter values when a new entityName is provided', async () => {
+ const query = () => singleResult;
+ const querySpy = jest.fn(query);
+ const searchApi = { query: querySpy };
+ const apiRegistry = TestApiRegistry.from([searchApiRef, searchApi]);
+ const newEntityId = {
+ name: 'test-diff',
+ namespace: 'testspace-diff',
+ kind: 'TestableDiff',
+ };
+
+ const WrappedSearchBar = () => {
+ const [entityName, setEntityName] = useState(entityId);
+ return wrapInTestApp(
+
+
+
+ ,
+ );
+ };
+
+ await act(async () => {
+ const rendered = render();
+
+ await singleResult;
+ expect(querySpy).toBeCalledWith({
+ filters: {
+ kind: 'Testable',
+ name: 'test',
+ namespace: 'testspace',
+ },
+ pageCursor: '',
+ term: '',
+ types: ['techdocs'],
+ });
+
+ const button = rendered.getByText('Update Entity');
+ button.click();
+
+ await singleResult;
+ await waitFor(() =>
+ expect(querySpy).toBeCalledWith({
+ filters: {
+ kind: 'TestableDiff',
+ name: 'test-diff',
+ namespace: 'testspace-diff',
+ },
+ pageCursor: '',
+ term: '',
+ types: ['techdocs'],
+ }),
+ );
+ });
+ });
});
diff --git a/plugins/techdocs/src/reader/components/TechDocsSearch.tsx b/plugins/techdocs/src/reader/components/TechDocsSearch.tsx
index 75cd3fcd6a..6c0be16f4f 100644
--- a/plugins/techdocs/src/reader/components/TechDocsSearch.tsx
+++ b/plugins/techdocs/src/reader/components/TechDocsSearch.tsx
@@ -64,6 +64,7 @@ const TechDocsSearchBar = ({
const {
term,
setTerm,
+ setFilters,
result: { loading, value: searchVal },
} = useSearch();
const classes = useStyles();
@@ -87,6 +88,20 @@ const TechDocsSearchBar = ({
useDebounce(() => setTerm(value), debounceTime, [value]);
+ // Update the filter context when the entityId changes, e.g. when the search
+ // bar continues to be rendered, navigating between different TechDocs sites.
+ const { kind, name, namespace } = entityId;
+ useEffect(() => {
+ setFilters(prevFilters => {
+ return {
+ ...prevFilters,
+ kind,
+ namespace,
+ name,
+ };
+ });
+ }, [kind, namespace, name, setFilters]);
+
const handleQuery = (e: ChangeEvent) => {
if (!open) {
setOpen(true);