refactor(search): apply review suggestion

Use search context checker in search modal and create local context only if needed.

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2022-06-21 13:46:18 +02:00
parent 509c4092f0
commit a620f777b3
@@ -35,6 +35,7 @@ import {
SearchResult,
SearchResultPager,
useSearch,
useSearchContextCheck,
} from '@backstage/plugin-search-react';
import { useRouteRef } from '@backstage/core-plugin-api';
import { Link, useContent } from '@backstage/core-components';
@@ -172,13 +173,11 @@ export const Modal = ({ toggleModal }: SearchModalProps) => {
const Context = ({ children }: PropsWithChildren<{}>) => {
// Checks if there is a parent context already defined and, if not, creates a new local context.
try {
// Throws an exception if there is no parent context already defined
useSearch();
const hasParentContext = useSearchContextCheck();
if (hasParentContext) {
return <>{children}</>;
} catch {
return <SearchContextProvider>{children}</SearchContextProvider>;
}
return <SearchContextProvider>{children}</SearchContextProvider>;
};
/**