From e1de8526aadeb38b389dd31ed7d45b694eff36b3 Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Wed, 13 Apr 2022 15:12:04 +0200 Subject: [PATCH 1/3] 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; -}; From fbe4918bbfccf07c5f499eab66218e736eec11ab Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Wed, 13 Apr 2022 15:16:20 +0200 Subject: [PATCH 2/3] clarify changeset Signed-off-by: Emma Indal --- .changeset/search-spicy-pots-bow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/search-spicy-pots-bow.md b/.changeset/search-spicy-pots-bow.md index 3c2ba183f5..cbd249116d 100644 --- a/.changeset/search-spicy-pots-bow.md +++ b/.changeset/search-spicy-pots-bow.md @@ -2,4 +2,4 @@ '@backstage/plugin-search-react': patch --- -Context managed through version-bridge +Versioned search context managed through version-bridge From 20a3d62a7ae28664730d600a6b85274f47e70ab2 Mon Sep 17 00:00:00 2001 From: Emma Indal Date: Thu, 14 Apr 2022 13:54:14 +0200 Subject: [PATCH 3/3] remove todo + clarify error message Signed-off-by: Emma Indal --- plugins/search-react/src/context/SearchContext.tsx | 8 ++------ plugins/search-react/src/context/index.tsx | 6 +----- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/plugins/search-react/src/context/SearchContext.tsx b/plugins/search-react/src/context/SearchContext.tsx index 7da4785fdd..6f8067805c 100644 --- a/plugins/search-react/src/context/SearchContext.tsx +++ b/plugins/search-react/src/context/SearchContext.tsx @@ -53,11 +53,7 @@ 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 = createVersionedContext<{ +const SearchContext = createVersionedContext<{ 1: SearchContextValue; }>('search-context'); @@ -72,7 +68,7 @@ export const useSearch = () => { const value = context.atVersion(1); if (!value) { - throw new Error('No context for version 1 found'); + throw new Error('No SearchContext v1 found'); } return value; }; diff --git a/plugins/search-react/src/context/index.tsx b/plugins/search-react/src/context/index.tsx index f2ea486e9d..330da035d6 100644 --- a/plugins/search-react/src/context/index.tsx +++ b/plugins/search-react/src/context/index.tsx @@ -14,11 +14,7 @@ * limitations under the License. */ -export { - SearchContextProvider, - SearchContext, - useSearch, -} from './SearchContext'; +export { SearchContextProvider, useSearch } from './SearchContext'; export type { SearchContextState } from './SearchContext'; export { SearchContextProvider as SearchContextProviderForStorybook,