From 2d339b5f2c72e509fe354883e2a3337ef68f0963 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Fri, 18 Feb 2022 14:45:28 +0100 Subject: [PATCH 1/2] catalog: Deprecate `useEntityFromUrl` and `useEntityCompoundName` Signed-off-by: Johan Haals --- .changeset/afraid-ravens-beam.md | 5 ++ .changeset/tame-pants-count.md | 5 ++ plugins/catalog-react/api-report.md | 4 +- plugins/catalog-react/src/hooks/useEntity.tsx | 4 +- .../src/hooks/useEntityCompoundName.ts | 1 + .../CatalogEntityPage/CatalogEntityPage.tsx | 6 +- .../CatalogEntityPage/UseEntityFromUrl.ts | 55 +++++++++++++++++++ 7 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 .changeset/afraid-ravens-beam.md create mode 100644 .changeset/tame-pants-count.md create mode 100644 plugins/catalog/src/components/CatalogEntityPage/UseEntityFromUrl.ts diff --git a/.changeset/afraid-ravens-beam.md b/.changeset/afraid-ravens-beam.md new file mode 100644 index 0000000000..b2596823d5 --- /dev/null +++ b/.changeset/afraid-ravens-beam.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog': patch +--- + +Internalize deprecated `useEntityFromUrl` hook diff --git a/.changeset/tame-pants-count.md b/.changeset/tame-pants-count.md new file mode 100644 index 0000000000..e2a35c929b --- /dev/null +++ b/.changeset/tame-pants-count.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-react': patch +--- + +Deprecated `useEntityFromUrl` and the `useEntityCompoundName` hooks as these have very low utility value. diff --git a/plugins/catalog-react/api-report.md b/plugins/catalog-react/api-report.md index 104472cbfe..efe0cbcd02 100644 --- a/plugins/catalog-react/api-report.md +++ b/plugins/catalog-react/api-report.md @@ -541,14 +541,14 @@ export function useEntity(): { refresh: VoidFunction | undefined; }; -// @public +// @public @deprecated export const useEntityCompoundName: () => { kind: string; namespace: string; name: string; }; -// @public (undocumented) +// @public @deprecated (undocumented) export const useEntityFromUrl: () => EntityLoadingStatus; // @public diff --git a/plugins/catalog-react/src/hooks/useEntity.tsx b/plugins/catalog-react/src/hooks/useEntity.tsx index 8637b820d4..908bfceb0b 100644 --- a/plugins/catalog-react/src/hooks/useEntity.tsx +++ b/plugins/catalog-react/src/hooks/useEntity.tsx @@ -134,7 +134,9 @@ const CompatibilityProvider = ({ }; EntityContext.Provider = CompatibilityProvider as Provider; -/** @public */ +/** @public + * @deprecated will be deleted shortly due to low external usage, re-implement if needed. + */ export const useEntityFromUrl = (): EntityLoadingStatus => { const { kind, namespace, name } = useEntityCompoundName(); const navigate = useNavigate(); diff --git a/plugins/catalog-react/src/hooks/useEntityCompoundName.ts b/plugins/catalog-react/src/hooks/useEntityCompoundName.ts index 75affc98b2..481c18f8a0 100644 --- a/plugins/catalog-react/src/hooks/useEntityCompoundName.ts +++ b/plugins/catalog-react/src/hooks/useEntityCompoundName.ts @@ -19,6 +19,7 @@ import { useRouteRefParams } from '@backstage/core-plugin-api'; /** * Grabs entity kind, namespace, and name from the location * @public + * @deprecated use {@link @backstage/core-plugin-api#useRouteRefParams} instead */ export const useEntityCompoundName = () => { const { kind, namespace, name } = useRouteRefParams(entityRouteRef); diff --git a/plugins/catalog/src/components/CatalogEntityPage/CatalogEntityPage.tsx b/plugins/catalog/src/components/CatalogEntityPage/CatalogEntityPage.tsx index da578a8892..a7dedaf1d6 100644 --- a/plugins/catalog/src/components/CatalogEntityPage/CatalogEntityPage.tsx +++ b/plugins/catalog/src/components/CatalogEntityPage/CatalogEntityPage.tsx @@ -16,10 +16,8 @@ import React from 'react'; import { Outlet } from 'react-router'; -import { - useEntityFromUrl, - AsyncEntityProvider, -} from '@backstage/plugin-catalog-react'; +import { AsyncEntityProvider } from '@backstage/plugin-catalog-react'; +import { useEntityFromUrl } from './UseEntityFromUrl'; /** @public */ export function CatalogEntityPage() { diff --git a/plugins/catalog/src/components/CatalogEntityPage/UseEntityFromUrl.ts b/plugins/catalog/src/components/CatalogEntityPage/UseEntityFromUrl.ts new file mode 100644 index 0000000000..605305882e --- /dev/null +++ b/plugins/catalog/src/components/CatalogEntityPage/UseEntityFromUrl.ts @@ -0,0 +1,55 @@ +/* + * Copyright 2022 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { + errorApiRef, + useApi, + useRouteRefParams, +} from '@backstage/core-plugin-api'; +import { + catalogApiRef, + EntityLoadingStatus, + entityRouteRef, +} from '@backstage/plugin-catalog-react'; +import { useEffect } from 'react'; +import { useNavigate } from 'react-router'; +import useAsyncRetry from 'react-use/lib/useAsyncRetry'; + +export const useEntityFromUrl = (): EntityLoadingStatus => { + const { kind, namespace, name } = useRouteRefParams(entityRouteRef); + const navigate = useNavigate(); + const errorApi = useApi(errorApiRef); + const catalogApi = useApi(catalogApiRef); + + const { + value: entity, + error, + loading, + retry: refresh, + } = useAsyncRetry( + () => catalogApi.getEntityByName({ kind, namespace, name }), + [catalogApi, kind, namespace, name], + ); + + useEffect(() => { + if (!name) { + errorApi.post(new Error('No name provided!')); + navigate('/'); + } + }, [errorApi, navigate, error, loading, entity, name]); + + return { entity, loading, error, refresh }; +}; From b0f2c7d3a6b63adddeaf1fbffd781e56e5c97595 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Mon, 21 Feb 2022 10:03:35 +0100 Subject: [PATCH 2/2] chore: Rename file Signed-off-by: Johan Haals --- .../src/components/CatalogEntityPage/CatalogEntityPage.tsx | 2 +- .../{UseEntityFromUrl.ts => useEntityFromUrl.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename plugins/catalog/src/components/CatalogEntityPage/{UseEntityFromUrl.ts => useEntityFromUrl.ts} (100%) diff --git a/plugins/catalog/src/components/CatalogEntityPage/CatalogEntityPage.tsx b/plugins/catalog/src/components/CatalogEntityPage/CatalogEntityPage.tsx index a7dedaf1d6..fe32e78959 100644 --- a/plugins/catalog/src/components/CatalogEntityPage/CatalogEntityPage.tsx +++ b/plugins/catalog/src/components/CatalogEntityPage/CatalogEntityPage.tsx @@ -17,7 +17,7 @@ import React from 'react'; import { Outlet } from 'react-router'; import { AsyncEntityProvider } from '@backstage/plugin-catalog-react'; -import { useEntityFromUrl } from './UseEntityFromUrl'; +import { useEntityFromUrl } from './useEntityFromUrl'; /** @public */ export function CatalogEntityPage() { diff --git a/plugins/catalog/src/components/CatalogEntityPage/UseEntityFromUrl.ts b/plugins/catalog/src/components/CatalogEntityPage/useEntityFromUrl.ts similarity index 100% rename from plugins/catalog/src/components/CatalogEntityPage/UseEntityFromUrl.ts rename to plugins/catalog/src/components/CatalogEntityPage/useEntityFromUrl.ts