From bc3f2c1f20fdbaf7ce3fbdbcf21f4a02db181c2e Mon Sep 17 00:00:00 2001 From: bnechyporenko Date: Tue, 27 Sep 2022 09:11:30 +0200 Subject: [PATCH] Incorporated partially the feedback Signed-off-by: bnechyporenko --- .../EntityRefLink/EntityRefLinks.tsx | 4 +- .../EntityRefLink/FetchedEntityRefLinks.tsx | 60 ++++++------------- 2 files changed, 22 insertions(+), 42 deletions(-) diff --git a/plugins/catalog-react/src/components/EntityRefLink/EntityRefLinks.tsx b/plugins/catalog-react/src/components/EntityRefLink/EntityRefLinks.tsx index 9f66fffd7a..bb8950041a 100644 --- a/plugins/catalog-react/src/components/EntityRefLink/EntityRefLinks.tsx +++ b/plugins/catalog-react/src/components/EntityRefLink/EntityRefLinks.tsx @@ -54,8 +54,10 @@ export function EntityRefLinks< if (fetchEntities) { return ( ); } diff --git a/plugins/catalog-react/src/components/EntityRefLink/FetchedEntityRefLinks.tsx b/plugins/catalog-react/src/components/EntityRefLink/FetchedEntityRefLinks.tsx index 5dbe429496..6b6ead7872 100644 --- a/plugins/catalog-react/src/components/EntityRefLink/FetchedEntityRefLinks.tsx +++ b/plugins/catalog-react/src/components/EntityRefLink/FetchedEntityRefLinks.tsx @@ -13,7 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Entity, CompoundEntityRef } from '@backstage/catalog-model'; +import { + Entity, + CompoundEntityRef, + parseEntityRef, +} from '@backstage/catalog-model'; import React from 'react'; import { EntityRefLink } from './EntityRefLink'; import { ErrorPanel, LinkProps, Progress } from '@backstage/core-components'; @@ -31,7 +35,6 @@ export type FetchedEntityRefLinksProps< > = { defaultKind?: string; entityRefs: TRef[]; - fetchEntities: true; getTitle?(entity: Entity): string | undefined; } & Omit; @@ -44,60 +47,35 @@ export type FetchedEntityRefLinksProps< export function FetchedEntityRefLinks< TRef extends string | CompoundEntityRef | Entity, >(props: FetchedEntityRefLinksProps) { - const { entityRefs, defaultKind, fetchEntities, getTitle, ...linkProps } = - props; + const { entityRefs, defaultKind, getTitle, ...linkProps } = props; const catalogApi = useApi(catalogApiRef); const { - value: refToEntity = new Map(), + value: entities = new Array(), loading, error, - } = useAsync( - () => - entityRefs.reduce( - async (promisedAcc: Promise>, entityRef: TRef) => { - const acc = await promisedAcc; - const entity: Entity | undefined = - 'metadata' in entityRef - ? (entityRef as Entity) - : await catalogApi.getEntityByRef( - entityRef as string | CompoundEntityRef, - ); - if (entity) { - acc.set(entityRef, entity); - } - return acc; - }, - Promise.resolve(new Map()), - ), - [catalogApi, entityRefs], - ); + } = useAsync(async () => { + const refs = entityRefs.reduce((acc, current) => { + return 'metadata' in current ? acc : [...acc, parseEntityRef(current)]; + }, new Array()); + + return refs + ? (await catalogApi.getEntities({ filter: refs })).items + : (entityRefs as Array); + }, [entityRefs]); if (loading) { return ; } if (error) { - return ( - <> - - - ); + return ; } return ( <> - {entityRefs.map((r: TRef, i) => { - let title: string | undefined; - - if (typeof r === 'string' || !('metadata' in r)) { - const entity = refToEntity.get(r); - title = getTitle && entity ? getTitle(entity) : undefined; - } else { - title = getTitle ? getTitle(r as Entity) : undefined; - } - + {entities.map((r: Entity, i) => { return ( {i > 0 && ', '} @@ -105,7 +83,7 @@ export function FetchedEntityRefLinks< {...linkProps} defaultKind={defaultKind} entityRef={r} - title={title} + title={getTitle ? getTitle(r as Entity) : undefined} /> );