From 3334ad47d40eb9e6bc87988d21eee48898c7b845 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Tue, 22 Feb 2022 10:22:38 +0100 Subject: [PATCH] catalog-react: Remove EntityContext Signed-off-by: Johan Haals --- .changeset/fair-hounds-leave.md | 5 +++ .changeset/pretty-lemons-mate.md | 5 +++ plugins/catalog-react/api-report.md | 4 -- plugins/catalog-react/src/hooks/index.ts | 1 - .../src/hooks/useEntity.test.tsx | 42 +------------------ plugins/catalog-react/src/hooks/useEntity.tsx | 42 ++----------------- .../components/EntityLayout/EntityLayout.tsx | 6 +-- 7 files changed, 18 insertions(+), 87 deletions(-) create mode 100644 .changeset/fair-hounds-leave.md create mode 100644 .changeset/pretty-lemons-mate.md diff --git a/.changeset/fair-hounds-leave.md b/.changeset/fair-hounds-leave.md new file mode 100644 index 0000000000..e986ca3177 --- /dev/null +++ b/.changeset/fair-hounds-leave.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-react': minor +--- + +Removed the deprecated `EntityContext` which have been replaced by `useEntity`, `EntityProvider` and `AsyncEntityProvider`. diff --git a/.changeset/pretty-lemons-mate.md b/.changeset/pretty-lemons-mate.md new file mode 100644 index 0000000000..7dd7aa3646 --- /dev/null +++ b/.changeset/pretty-lemons-mate.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog': patch +--- + +Removed usage of `EntityContext`. diff --git a/plugins/catalog-react/api-report.md b/plugins/catalog-react/api-report.md index 0a7bbbfc9e..37e959f93b 100644 --- a/plugins/catalog-react/api-report.md +++ b/plugins/catalog-react/api-report.md @@ -11,7 +11,6 @@ import { CATALOG_FILTER_EXISTS } from '@backstage/catalog-client'; import { CatalogApi } from '@backstage/catalog-client'; import { ComponentEntity } from '@backstage/catalog-model'; import { ComponentProps } from 'react'; -import { Context } from 'react'; import { Entity } from '@backstage/catalog-model'; import { EntityName } from '@backstage/catalog-model'; import { GetEntitiesResponse } from '@backstage/catalog-client'; @@ -151,9 +150,6 @@ export class DefaultStarredEntitiesApi implements StarredEntitiesApi { toggleStarred(entityRef: string): Promise; } -// @public @deprecated (undocumented) -export const EntityContext: Context; - // @public (undocumented) export type EntityFilter = { getCatalogFilters?: () => Record< diff --git a/plugins/catalog-react/src/hooks/index.ts b/plugins/catalog-react/src/hooks/index.ts index 170ea37394..5ce1fabbc6 100644 --- a/plugins/catalog-react/src/hooks/index.ts +++ b/plugins/catalog-react/src/hooks/index.ts @@ -14,7 +14,6 @@ * limitations under the License. */ export { - EntityContext, useEntity, useEntityFromUrl, EntityProvider, diff --git a/plugins/catalog-react/src/hooks/useEntity.test.tsx b/plugins/catalog-react/src/hooks/useEntity.test.tsx index 9e73ed1475..f61283f2cf 100644 --- a/plugins/catalog-react/src/hooks/useEntity.test.tsx +++ b/plugins/catalog-react/src/hooks/useEntity.test.tsx @@ -16,12 +16,7 @@ import React from 'react'; import { renderHook } from '@testing-library/react-hooks'; -import { - useEntity, - EntityProvider, - AsyncEntityProvider, - EntityContext, -} from './useEntity'; +import { useEntity, EntityProvider, AsyncEntityProvider } from './useEntity'; import { Entity } from '@backstage/catalog-model'; describe('EntityProvider', () => { @@ -103,38 +98,3 @@ describe('AsyncEntityProvider', () => { expect(result.current.refresh).toBe(undefined); }); }); - -describe('EntityContext.Provider', () => { - it('should provide no entity', async () => { - const { result } = renderHook(() => useEntity(), { - wrapper: ({ children }) => ( - - ), - }); - - expect(result.current.entity).toBe(undefined); - expect(result.current.loading).toBe(false); - expect(result.current.error).toBe(undefined); - expect(result.current.refresh).toBe(undefined); - }); - - it('should provide an entity', async () => { - const entity = { kind: 'MyEntity' } as Entity; - const { result } = renderHook(() => useEntity(), { - wrapper: ({ children }) => ( - - ), - }); - - expect(result.current.entity).toBe(entity); - expect(result.current.loading).toBe(false); - expect(result.current.error).toBe(undefined); - expect(result.current.refresh).toBe(undefined); - }); -}); diff --git a/plugins/catalog-react/src/hooks/useEntity.tsx b/plugins/catalog-react/src/hooks/useEntity.tsx index 908bfceb0b..5cb67cb79a 100644 --- a/plugins/catalog-react/src/hooks/useEntity.tsx +++ b/plugins/catalog-react/src/hooks/useEntity.tsx @@ -20,13 +20,7 @@ import { createVersionedValueMap, useVersionedContext, } from '@backstage/version-bridge'; -import React, { - ReactNode, - useEffect, - createContext, - Provider, - Context, -} from 'react'; +import React, { ReactNode, useEffect } from 'react'; import { useNavigate } from 'react-router'; import useAsyncRetry from 'react-use/lib/useAsyncRetry'; import { catalogApiRef } from '../api'; @@ -40,20 +34,6 @@ export type EntityLoadingStatus = { refresh?: VoidFunction; }; -/** - * @public - * @deprecated use `useEntity` and `EntityProvider` or `AsyncEntityProvider` instead. - */ -export const EntityContext: Context = - createContext({ - entity: undefined, - loading: true, - error: undefined, - refresh: () => {}, - }); -// We grab this for use in the new provider, since we're overriding it later on -const OldEntityProvider = EntityContext.Provider; - // This context has support for multiple concurrent versions of this package. // It is currently used in parallel with the old context in order to provide // a smooth transition, but will eventually be the only context we use. @@ -89,11 +69,9 @@ export const AsyncEntityProvider = ({ // We provide both the old and the new context, since // consumers might be doing things like `useContext(EntityContext)` return ( - - - {children} - - + + {children} + ); }; @@ -122,18 +100,6 @@ export const EntityProvider = ({ entity, children }: EntityProviderProps) => ( /> ); -// This is used for forwards compatibility with the new entity context -const CompatibilityProvider = ({ - value, - children, -}: { - value: EntityLoadingStatus; - children: ReactNode; -}) => { - return ; -}; -EntityContext.Provider = CompatibilityProvider as Provider; - /** @public * @deprecated will be deleted shortly due to low external usage, re-implement if needed. */ diff --git a/plugins/catalog/src/components/EntityLayout/EntityLayout.tsx b/plugins/catalog/src/components/EntityLayout/EntityLayout.tsx index 42658109f9..634662f386 100644 --- a/plugins/catalog/src/components/EntityLayout/EntityLayout.tsx +++ b/plugins/catalog/src/components/EntityLayout/EntityLayout.tsx @@ -35,17 +35,17 @@ import { useElementFilter, } from '@backstage/core-plugin-api'; import { - EntityContext, EntityRefLinks, FavoriteEntity, getEntityRelations, InspectEntityDialog, UnregisterEntityDialog, + useEntity, useEntityCompoundName, } from '@backstage/plugin-catalog-react'; import { Box, TabProps } from '@material-ui/core'; import { Alert } from '@material-ui/lab'; -import React, { useContext, useEffect, useState } from 'react'; +import React, { useEffect, useState } from 'react'; import { useLocation, useNavigate } from 'react-router'; import { EntityContextMenu } from '../EntityContextMenu/EntityContextMenu'; @@ -177,7 +177,7 @@ export const EntityLayout = (props: EntityLayoutProps) => { children, } = props; const { kind, namespace, name } = useEntityCompoundName(); - const { entity, loading, error } = useContext(EntityContext); + const { entity, loading, error } = useEntity(); const location = useLocation(); const routes = useElementFilter( children,