From 11a46863dedb7adf0021494a1aae61bc55fbe2a0 Mon Sep 17 00:00:00 2001 From: Phil Kuang Date: Thu, 21 Apr 2022 23:53:02 -0400 Subject: [PATCH] fix(HomePageSearchBar): don't require SearchContext Signed-off-by: Phil Kuang --- .changeset/search-dry-wolves-join.md | 5 ++++ .changeset/search-heavy-llamas-worry.md | 5 ++++ plugins/search-react/api-report.md | 3 +++ .../src/context/SearchContext.test.tsx | 26 ++++++++++++++++++- .../src/context/SearchContext.tsx | 10 +++++++ plugins/search-react/src/context/index.tsx | 6 ++++- plugins/search-react/src/index.ts | 1 + .../src/components/SearchBar/SearchBar.tsx | 3 ++- 8 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 .changeset/search-dry-wolves-join.md create mode 100644 .changeset/search-heavy-llamas-worry.md diff --git a/.changeset/search-dry-wolves-join.md b/.changeset/search-dry-wolves-join.md new file mode 100644 index 0000000000..2eb613b642 --- /dev/null +++ b/.changeset/search-dry-wolves-join.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-search': patch +--- + +Fix issue with `HomePageSearchBar` requiring `SearchContext` diff --git a/.changeset/search-heavy-llamas-worry.md b/.changeset/search-heavy-llamas-worry.md new file mode 100644 index 0000000000..2d77d6c4d6 --- /dev/null +++ b/.changeset/search-heavy-llamas-worry.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-search-react': patch +--- + +Export `useSearchContextCheck` hook to check if the search context is available diff --git a/plugins/search-react/api-report.md b/plugins/search-react/api-report.md index 112d864add..969e4d8563 100644 --- a/plugins/search-react/api-report.md +++ b/plugins/search-react/api-report.md @@ -74,4 +74,7 @@ export type SearchContextValue = { // @public export const useSearch: () => SearchContextValue; + +// @public +export const useSearchContextCheck: () => boolean; ``` diff --git a/plugins/search-react/src/context/SearchContext.test.tsx b/plugins/search-react/src/context/SearchContext.test.tsx index b19953f6a5..0272176150 100644 --- a/plugins/search-react/src/context/SearchContext.test.tsx +++ b/plugins/search-react/src/context/SearchContext.test.tsx @@ -18,7 +18,11 @@ import { useApi } from '@backstage/core-plugin-api'; import { render, screen, waitFor } from '@testing-library/react'; import { act, renderHook } from '@testing-library/react-hooks'; import React from 'react'; -import { SearchContextProvider, useSearch } from './SearchContext'; +import { + SearchContextProvider, + useSearch, + useSearchContextCheck, +} from './SearchContext'; jest.mock('@backstage/core-plugin-api', () => ({ ...jest.requireActual('@backstage/core-plugin-api'), @@ -71,6 +75,26 @@ describe('SearchContext', () => { ); }); + it('Checks whether context is set', async () => { + const hook = renderHook(() => useSearchContextCheck()); + + expect(hook.result.current).toEqual(false); + + const { result, waitForNextUpdate } = renderHook( + () => useSearchContextCheck(), + { + wrapper, + initialProps: { + initialState, + }, + }, + ); + + await waitForNextUpdate(); + + expect(result.current).toEqual(true); + }); + it('Uses initial state values', async () => { const { result, waitForNextUpdate } = renderHook(() => useSearch(), { wrapper, diff --git a/plugins/search-react/src/context/SearchContext.tsx b/plugins/search-react/src/context/SearchContext.tsx index 9024fb5e9e..8a0f962121 100644 --- a/plugins/search-react/src/context/SearchContext.tsx +++ b/plugins/search-react/src/context/SearchContext.tsx @@ -79,6 +79,16 @@ export const useSearch = () => { return value; }; +/** + * @public + * + * React hook which checks for an existing search context + */ +export const useSearchContextCheck = () => { + const context = useContext(SearchContext); + return context !== undefined; +}; + /** * The initial state of `SearchContextProvider`. * diff --git a/plugins/search-react/src/context/index.tsx b/plugins/search-react/src/context/index.tsx index c805cacfc0..76e1142bea 100644 --- a/plugins/search-react/src/context/index.tsx +++ b/plugins/search-react/src/context/index.tsx @@ -14,7 +14,11 @@ * limitations under the License. */ -export { SearchContextProvider, useSearch } from './SearchContext'; +export { + SearchContextProvider, + useSearch, + useSearchContextCheck, +} from './SearchContext'; export type { SearchContextProviderProps, SearchContextState, diff --git a/plugins/search-react/src/index.ts b/plugins/search-react/src/index.ts index 8b250b6742..424c804472 100644 --- a/plugins/search-react/src/index.ts +++ b/plugins/search-react/src/index.ts @@ -25,6 +25,7 @@ export type { SearchApi } from './api'; export { SearchContextProvider, useSearch, + useSearchContextCheck, SearchContextProviderForStorybook, SearchApiProviderForStorybook, } from './context'; diff --git a/plugins/search/src/components/SearchBar/SearchBar.tsx b/plugins/search/src/components/SearchBar/SearchBar.tsx index 70038ecc82..b5d55fed8e 100644 --- a/plugins/search/src/components/SearchBar/SearchBar.tsx +++ b/plugins/search/src/components/SearchBar/SearchBar.tsx @@ -35,6 +35,7 @@ import ClearButton from '@material-ui/icons/Clear'; import { SearchContextProvider, useSearch, + useSearchContextCheck, } from '@backstage/plugin-search-react'; import { TrackSearch } from '../SearchTracker'; @@ -72,7 +73,7 @@ export const SearchBarBase = ({ }: SearchBarBaseProps) => { const configApi = useApi(configApiRef); const [value, setValue] = useState(defaultValue as string); - const hasSearchContext = useSearch(); + const hasSearchContext = useSearchContextCheck(); useEffect(() => { setValue(prevValue =>