From f7b3c2f1b0264f9ffa95fcdce2f89223374d1680 Mon Sep 17 00:00:00 2001 From: Karan Shah Date: Tue, 1 Mar 2022 09:59:12 +0000 Subject: [PATCH] Initial commit Signed-off-by: Karan Shah --- .../EntityAirbrakeWidget.tsx | 109 +++++++++++------- 1 file changed, 65 insertions(+), 44 deletions(-) diff --git a/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.tsx b/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.tsx index b11116aeef..93bf490fa8 100644 --- a/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.tsx +++ b/plugins/airbrake/src/components/EntityAirbrakeWidget/EntityAirbrakeWidget.tsx @@ -14,7 +14,7 @@ * limitations under the License. */ import { Entity } from '@backstage/catalog-model'; -import React, { useEffect } from 'react'; +import React, { useEffect, useState } from 'react'; import { Grid, Typography } from '@material-ui/core'; import { EmptyState, @@ -36,59 +36,80 @@ const useStyles = makeStyles(() => ({ }, })); +enum ComponentState { + Loading, + NoProjectId, + Error, + Loaded, +} + export const EntityAirbrakeWidget = ({ entity }: { entity: Entity }) => { const classes = useStyles(); const projectId = useProjectId(entity); const errorApi = useApi(errorApiRef); const airbrakeApi = useApi(airbrakeApiRef); - - const { loading, value, error } = useAsync( - () => airbrakeApi.fetchGroups(projectId), - [airbrakeApi, projectId], + const [componentState, setComponentState] = useState( + ComponentState.Loading, ); - useEffect(() => { - if (error) { - errorApi.post(error); - } - }, [error, errorApi]); - - if (loading || !projectId || error) { - return ( - - {loading && } - - {!loading && !projectId && ( - - )} - - {!loading && error && ( - - )} - - ); + if (!projectId) { + setComponentState(ComponentState.NoProjectId); } - return ( - - {value?.groups?.map(group => ( - - {group.errors?.map(groupError => ( - - - {groupError.message} - - + const { loading, value, error } = useAsync(async () => { + const result = await airbrakeApi.fetchGroups(projectId); + setComponentState(ComponentState.Loaded); + return result; + }, [airbrakeApi, projectId]); + + if (loading) { + setComponentState(ComponentState.Loading); + } + + if (componentState !== ComponentState.NoProjectId && error) { + setComponentState(ComponentState.Error); + } + + useEffect(() => { + if (componentState === ComponentState.Error && error) { + errorApi.post(error); + } + }, [componentState, error, errorApi]); + + 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 ( + + ); + } };