chore: deprecate some of the loading things from useEntity and force entity to be present

Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
blam
2022-03-01 11:27:37 +01:00
parent 094f404c3f
commit babb280c77
4 changed files with 58 additions and 16 deletions
+3
View File
@@ -18,11 +18,14 @@ export {
useEntityFromUrl,
EntityProvider,
AsyncEntityProvider,
useAsyncEntity,
} from './useEntity';
export type {
EntityLoadingStatus,
EntityProviderProps,
AsyncEntityProviderProps,
UseEntityResponse,
UseAsyncEntityResponse,
} from './useEntity';
export { useEntityCompoundName } from './useEntityCompoundName';
export {
+47 -11
View File
@@ -128,26 +128,35 @@ export const useEntityFromUrl = (): EntityLoadingStatus => {
return { entity, loading, error, refresh };
};
export interface UseEntityResponse<T> {
entity: T;
/** @deprecated use useAsyncEntity instead */
loading: boolean;
/** @deprecated use useAsyncEntity instead */
error?: Error;
/** @deprecated use useAsyncEntity instead */
refresh?: VoidFunction;
}
export interface UseAsyncEntityResponse<T> {
entity?: T;
loading: boolean;
error?: Error;
refresh?: VoidFunction;
}
/**
* Grab the current entity from the context and its current loading state.
* Grab the current entity from the context, throws if the entity has not yet been loaded
* or is not available.
*
* @public
*/
export function useEntity<T extends Entity = Entity>() {
export function useEntity<T extends Entity = Entity>(): UseEntityResponse<T> {
const versionedHolder =
useVersionedContext<{ 1: EntityLoadingStatus }>('entity-context');
if (!versionedHolder) {
// TODO(Rugvip): Throw this once we fully migrate to the new context
// throw new Error('Entity context is not available');
return {
entity: undefined as unknown as T,
loading: true,
error: undefined,
refresh: () => {},
};
throw new Error('Entity context is not available');
}
const value = versionedHolder.atVersion(1);
@@ -155,6 +164,33 @@ export function useEntity<T extends Entity = Entity>() {
throw new Error('EntityContext v1 not available');
}
if (!value.entity) {
throw new Error('Entity has not been loaded yet');
}
const { entity, loading, error, refresh } = value;
return { entity: entity as T, loading, error, refresh };
}
/**
* Grab the current entity from the context, provides loading state and errors, and the ability to refresh.
*
* @public
*/
export function useAsyncEntity<
T extends Entity = Entity,
>(): UseAsyncEntityResponse<T> {
const versionedHolder =
useVersionedContext<{ 1: EntityLoadingStatus }>('entity-context');
if (!versionedHolder) {
throw new Error('Entity context is not available');
}
const value = versionedHolder.atVersion(1);
if (!value) {
throw new Error('EntityContext v1 not available');
}
const { entity, loading, error, refresh } = value;
return { entity: entity as T, loading, error, refresh };
}
@@ -15,7 +15,7 @@
*/
import { Entity } from '@backstage/catalog-model';
import { useEntity } from '@backstage/plugin-catalog-react';
import { useAsyncEntity } from '@backstage/plugin-catalog-react';
import React, { ReactNode, ReactElement } from 'react';
import {
attachComponentData,
@@ -63,7 +63,7 @@ export interface EntitySwitchProps {
/** @public */
export const EntitySwitch = (props: EntitySwitchProps) => {
const { entity } = useEntity();
const { entity } = useAsyncEntity();
const apis = useApiHolder();
const results = useElementFilter(
props.children,
@@ -75,6 +75,9 @@ export const EntitySwitch = (props: EntitySwitchProps) => {
})
.getElements()
.flatMap<SwitchCaseResult>((element: ReactElement) => {
if (!entity) {
return [];
}
const { if: condition, children: elementsChildren } =
element.props as EntitySwitchCase;
return [
@@ -27,7 +27,7 @@ function strCmp(a: string | undefined, b: string | undefined): boolean {
* @public
*/
export function isKind(kind: string) {
return (entity: Entity) => strCmp(entity?.kind, kind);
return (entity: Entity) => strCmp(entity.kind, kind);
}
/**
@@ -36,7 +36,7 @@ export function isKind(kind: string) {
*/
export function isComponentType(type: string) {
return (entity: Entity) => {
if (!strCmp(entity?.kind, 'component')) {
if (!strCmp(entity.kind, 'component')) {
return false;
}
const componentEntity = entity as ComponentEntity;
@@ -49,5 +49,5 @@ export function isComponentType(type: string) {
* @public
*/
export function isNamespace(namespace: string) {
return (entity: Entity) => strCmp(entity?.metadata?.namespace, namespace);
return (entity: Entity) => strCmp(entity.metadata?.namespace, namespace);
}