From 12deed2614b5a9768bf36bb4d899c1e3f4acd4a8 Mon Sep 17 00:00:00 2001 From: Karan Shah Date: Wed, 9 Mar 2022 14:46:59 +0000 Subject: [PATCH] Address further concerns - Remove custom error - Remove enums - The API no longer checks validity of its input, the calling useAsync call does that Signed-off-by: Karan Shah --- plugins/airbrake/src/api/AirbrakeApi.ts | 7 -- plugins/airbrake/src/api/ProductionApi.ts | 6 +- plugins/airbrake/src/api/mock/MockApi.ts | 7 +- .../EntityAirbrakeWidget.tsx | 90 ++++++++----------- 4 files changed, 40 insertions(+), 70 deletions(-) diff --git a/plugins/airbrake/src/api/AirbrakeApi.ts b/plugins/airbrake/src/api/AirbrakeApi.ts index 3abe745b5b..a29aa3eca7 100644 --- a/plugins/airbrake/src/api/AirbrakeApi.ts +++ b/plugins/airbrake/src/api/AirbrakeApi.ts @@ -16,7 +16,6 @@ import { Groups } from './airbrakeGroups'; import { createApiRef } from '@backstage/core-plugin-api'; -import { CustomErrorBase } from '@backstage/errors'; export const airbrakeApiRef = createApiRef({ id: 'plugin.airbrake.service', @@ -25,9 +24,3 @@ export const airbrakeApiRef = createApiRef({ export interface AirbrakeApi { fetchGroups(projectId: string): Promise; } - -export class NoProjectIdError extends CustomErrorBase { - constructor() { - super('Project ID is not present'); - } -} diff --git a/plugins/airbrake/src/api/ProductionApi.ts b/plugins/airbrake/src/api/ProductionApi.ts index 7d1d88a2a4..21ca19ee7d 100644 --- a/plugins/airbrake/src/api/ProductionApi.ts +++ b/plugins/airbrake/src/api/ProductionApi.ts @@ -15,17 +15,13 @@ */ import { Groups } from './airbrakeGroups'; -import { AirbrakeApi, NoProjectIdError } from './AirbrakeApi'; +import { AirbrakeApi } from './AirbrakeApi'; import { DiscoveryApi } from '@backstage/core-plugin-api'; export class ProductionAirbrakeApi implements AirbrakeApi { constructor(private readonly discoveryApi: DiscoveryApi) {} async fetchGroups(projectId: string): Promise { - if (!projectId) { - throw new NoProjectIdError(); - } - const baseUrl = await this.discoveryApi.getBaseUrl('airbrake'); const apiUrl = `${baseUrl}/api/v4/projects/${projectId}/groups`; diff --git a/plugins/airbrake/src/api/mock/MockApi.ts b/plugins/airbrake/src/api/mock/MockApi.ts index f362fbbcaa..dbb1d5c049 100644 --- a/plugins/airbrake/src/api/mock/MockApi.ts +++ b/plugins/airbrake/src/api/mock/MockApi.ts @@ -15,7 +15,7 @@ */ import { Groups } from '../airbrakeGroups'; -import { AirbrakeApi, NoProjectIdError } from '../AirbrakeApi'; +import { AirbrakeApi } from '../AirbrakeApi'; import mockGroupsData from './airbrakeGroupsApiMock.json'; export class MockAirbrakeApi implements AirbrakeApi { @@ -25,10 +25,7 @@ export class MockAirbrakeApi implements AirbrakeApi { this.waitTimeInMillis = waitTimeInMillis; } - fetchGroups(projectId: string): Promise { - if (!projectId) { - return Promise.reject(new NoProjectIdError()); - } + fetchGroups(): Promise { return new Promise(resolve => { setTimeout(() => resolve(mockGroupsData), this.waitTimeInMillis); }); diff --git a/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.tsx b/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.tsx index f235a3e2f5..6c2e4b8686 100644 --- a/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.tsx +++ b/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.tsx @@ -37,70 +37,54 @@ const useStyles = makeStyles(() => ({ }, })); -enum ComponentState { - Loading, - NoProjectId, - Error, - Loaded, -} - export const EntityAirbrakeWidget = ({ entity }: { entity: Entity }) => { const classes = useStyles(); const projectId = useProjectId(entity); const airbrakeApi = useApi(airbrakeApiRef); - let componentState = ComponentState.Loading; const { loading, value, error } = useAsync(() => { + if (!projectId) { + return Promise.resolve(undefined); + } + return airbrakeApi.fetchGroups(projectId); }, [airbrakeApi, projectId]); - if (loading) { - componentState = ComponentState.Loading; - } else if (!projectId) { - componentState = ComponentState.NoProjectId; - } else if (error) { - componentState = ComponentState.Error; + if (!projectId) { + return ( + + ); + } else if (loading) { + return ; } else if (value) { - componentState = ComponentState.Loaded; + return ( + + {value.groups?.map(group => ( + + {group.errors?.map(groupError => ( + + + {groupError.message} + + + ))} + + ))} + + ); } - switch (componentState) { - case ComponentState.Loaded: - return ( - - {value?.groups?.map(group => ( - - {group.errors?.map(groupError => ( - - - {groupError.message} - - - ))} - - ))} - - ); - case ComponentState.NoProjectId: - return ( - - ); - case ComponentState.Loading: - return ; - case ComponentState.Error: - default: - return ( - <> - - - - ); - } + return ( + <> + {error && } + + + ); };