From 6a96ba0eaae075f1c4d84a97fbfb9259cb698815 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Mon, 11 Apr 2022 11:05:33 +0200 Subject: [PATCH] Using version-bridge for the TechDocs Reader Page context. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Anders Näsman Signed-off-by: Eric Peterson --- plugins/techdocs/package.json | 1 + .../components/TechDocsReaderPage/context.tsx | 27 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/plugins/techdocs/package.json b/plugins/techdocs/package.json index 9813bdc66f..d383edc40b 100644 --- a/plugins/techdocs/package.json +++ b/plugins/techdocs/package.json @@ -47,6 +47,7 @@ "@backstage/plugin-catalog": "^1.1.0-next.2", "@backstage/plugin-search": "^0.7.5-next.0", "@backstage/techdocs-addons": "^0.0.0", + "@backstage/version-bridge": "^1.0.0", "@backstage/theme": "^0.2.15", "@material-ui/core": "^4.12.2", "@material-ui/icons": "^4.9.1", diff --git a/plugins/techdocs/src/reader/components/TechDocsReaderPage/context.tsx b/plugins/techdocs/src/reader/components/TechDocsReaderPage/context.tsx index a2c1897cbe..66fb97cfa7 100644 --- a/plugins/techdocs/src/reader/components/TechDocsReaderPage/context.tsx +++ b/plugins/techdocs/src/reader/components/TechDocsReaderPage/context.tsx @@ -19,7 +19,6 @@ import React, { memo, Dispatch, SetStateAction, - createContext, useContext, useState, } from 'react'; @@ -27,6 +26,10 @@ import useAsync, { AsyncState } from 'react-use/lib/useAsync'; import { useApi } from '@backstage/core-plugin-api'; import { CompoundEntityRef } from '@backstage/catalog-model'; +import { + createVersionedContext, + createVersionedValueMap, +} from '@backstage/version-bridge'; import { techdocsApiRef } from '../../../api'; import { TechDocsEntityMetadata, TechDocsMetadata } from '../../../types'; @@ -78,15 +81,26 @@ export const defaultTechDocsReaderPageValue: TechDocsReaderPageValue = { entityName: { kind: '', name: '', namespace: '' }, }; -export const TechDocsReaderPageContext = createContext( - defaultTechDocsReaderPageValue, -); +export const TechDocsReaderPageContext = createVersionedContext<{ + 1: TechDocsReaderPageValue; +}>('techdocs-reader-page-context'); /** * Hook used to get access to shared state between reader page components. * @public */ export const useTechDocsReaderPage = () => { - return useContext(TechDocsReaderPageContext); + const versionedContext = useContext(TechDocsReaderPageContext); + + if (versionedContext === undefined) { + return defaultTechDocsReaderPageValue; + } + + const context = versionedContext.atVersion(1); + if (context === undefined) { + throw new Error('No context found for version 1.'); + } + + return context; }; /** @@ -143,9 +157,10 @@ export const TechDocsReaderPageProvider = memo( subtitle, setSubtitle, }; + const versionedValue = createVersionedValueMap({ 1: value }); return ( - + {children instanceof Function ? children(value) : children} );