import React , { useState } from 'react' ; import { useAsync } from 'react-use' ; import { Grid } from '@material-ui/core' ; import Alert from '@material-ui/lab/Alert' ; import { Entity } from '@backstage/catalog-model' ; import { useEntity } from "@backstage/plugin-catalog-react" ; import { Page , Content , ContentHeader , SupportButton , MissingAnnotationEmptyState , Progress } from '@backstage/core-components' ; import { useApi } from '@backstage/core-plugin-api' ; import { bugsnagApiRef } from '../..' ; import { ErrorsTable } from '../ErrorsTableComponent' ; import { BUGSNAG_ANNOTATION , useBugsnagData } from '../../hooks/useBugsnagData' ; export const isBugsnagAvailable = ( entity ) => { return Boolean ( entity ?. metadata . annotations ?. [ BUGSNAG_ANNOTATION ] ) ; } ; export const ErrorsOverview = ( ) => { const { entity } = useEntity ( ) ; const organisationName = useBugsnagData ( ) [ 0 ] ; const projectApiKey = useBugsnagData ( ) [ 1 ] ; const api = useApi ( bugsnagApiRef ) ; const [ slug , setOrganisationSlug ] = useState ( '' ) ; const { value , loading , error } = useAsync ( async ( ) => { const organisations = await api . fetchOrganisations ( ) ; const organisation = organisations . find ( org => org . name . includes ( organisationName ) ) ; setOrganisationSlug ( organisation ?. slug || '' ) ; const projects = await api . fetchProjects ( organisation ?. id || '' ) ; const filteredProject = projects . find ( proj => proj . api_key . includes ( projectApiKey ) ) ; return filteredProject ; } ) ; if ( loading ) { return < Progress />; } else if ( error ) { return < Alert severity = "error" > { error . message } < /Alert>; } return isBugsnagAvailable ( entity ) ? ( < Page themeId = "tool" > < Content > < ContentHeader title = "Bugsnag logs" > < SupportButton > Overview of Bugsnag errors < /SupportButton> < /ContentHeader> < Grid > { value ? < Grid item > < ErrorsTable project = { value || { } } organisationName = { slug } / > < /Grid> : null} < /Grid> < /Content> < /Page>) : (<MissingAnnotationEmptyState annotation={BUGSNAG_ANNOTATION}/ > ) ; } ;