From e1de8526aadeb38b389dd31ed7d45b694eff36b3 Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Wed, 13 Apr 2022 15:12:04 +0200 Subject: [PATCH] context managed through version-bridge Signed-off-by: Emma Indal --- .changeset/search-spicy-pots-bow.md | 5 +++ plugins/search-react/package.json | 1 + .../src/context/SearchContext.tsx | 43 ++++++++++++------- 3 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 .changeset/search-spicy-pots-bow.md diff --git a/.changeset/search-spicy-pots-bow.md b/.changeset/search-spicy-pots-bow.md new file mode 100644 index 0000000000..3c2ba183f5 --- /dev/null +++ b/.changeset/search-spicy-pots-bow.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-search-react': patch +--- + +Context managed through version-bridge diff --git a/plugins/search-react/package.json b/plugins/search-react/package.json index f34ade558a..b89abcb5a5 100644 --- a/plugins/search-react/package.json +++ b/plugins/search-react/package.json @@ -34,6 +34,7 @@ "@backstage/plugin-search-common": "^0.3.3-next.1", "@backstage/core-plugin-api": "^1.0.1-next.0", "@backstage/core-app-api": "^1.0.1-next.1", + "@backstage/version-bridge": "^1.0.0", "react-use": "^17.3.2", "@backstage/types": "^1.0.0" }, diff --git a/plugins/search-react/src/context/SearchContext.tsx b/plugins/search-react/src/context/SearchContext.tsx index d6245d5ccf..7da4785fdd 100644 --- a/plugins/search-react/src/context/SearchContext.tsx +++ b/plugins/search-react/src/context/SearchContext.tsx @@ -17,8 +17,11 @@ import { JsonObject } from '@backstage/types'; import { useApi, AnalyticsContext } from '@backstage/core-plugin-api'; import { SearchResultSet } from '@backstage/plugin-search-common'; +import { + createVersionedContext, + createVersionedValueMap, +} from '@backstage/version-bridge'; import React, { - createContext, PropsWithChildren, useCallback, useContext, @@ -50,12 +53,29 @@ export type SearchContextState = { pageCursor?: string; }; +// TODO(@backstage/techdocs-core): stop export this publicly when deprecating the context in @backstage/plugin-techdocs. /** * @public */ -export const SearchContext = createContext( - undefined, -); +export const SearchContext = createVersionedContext<{ + 1: SearchContextValue; +}>('search-context'); + +/** + * @public + */ +export const useSearch = () => { + const context = useContext(SearchContext); + if (!context) { + throw new Error('useSearch must be used within a SearchContextProvider'); + } + + const value = context.atVersion(1); + if (!value) { + throw new Error('No context for version 1 found'); + } + return value; +}; /** * The initial state of `SearchContextProvider`. @@ -128,20 +148,11 @@ export const SearchContextProvider = ({ fetchPreviousPage: hasPreviousPage ? fetchPreviousPage : undefined, }; + const versionedValue = createVersionedValueMap({ 1: value }); + return ( - + ); }; - -/** - * @public - */ -export const useSearch = () => { - const context = useContext(SearchContext); - if (context === undefined) { - throw new Error('useSearch must be used within a SearchContextProvider'); - } - return context; -};