Merge pull request #9662 from backstage/jhaals/useEntityFromUrl

catalog: Deprecate `useEntityFromUrl` and `useEntityCompoundName`
This commit is contained in:
Johan Haals
2022-02-21 11:34:11 +01:00
committed by GitHub
7 changed files with 73 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog': patch
---
Internalize deprecated `useEntityFromUrl` hook
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-react': patch
---
Deprecated `useEntityFromUrl` and the `useEntityCompoundName` hooks as these have very low utility value.
+2 -2
View File
@@ -541,14 +541,14 @@ export function useEntity<T extends Entity = Entity>(): {
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
@@ -134,7 +134,9 @@ const CompatibilityProvider = ({
};
EntityContext.Provider = CompatibilityProvider as Provider<EntityLoadingStatus>;
/** @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();
@@ -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);
@@ -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() {
@@ -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 };
};