-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(backstage-plugin): Extract logic to hooks (#108)
* refactor(backstage-plugin): Extract logic to hooks Extract out error/response logic from component to a reusable hook to avoid complicated state in the component. Use a context to provide metric details for the request to avoid prop drilling. Create a reusable component to reuse logic for switching between a graph, loading bar or error. Addresses #71 * refactor(backstage-plugin): Remove comment for test Remove the comment about extracting the test for the hook. Tests should follow user usage. Addresses #71 * refactor(backstage-plugin): Extract overview to hook Extract logic to download and show progress/error to a hook/separate component. Remove unused error file. Addresses #71 * refactor(backstage-plugin): Move hook to own file Move benchmark hook to it's own file. Rename overview to benchmark for consistency. Addresses #71
- Loading branch information
1 parent
710a775
commit ec4f07e
Showing
6 changed files
with
187 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
backstage-plugin/plugins/open-dora/src/hooks/MetricBenchmarkHook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useApi } from '@backstage/core-plugin-api'; | ||
import { useEffect, useState } from 'react'; | ||
import { dfBenchmarkKey } from '../models/DfBenchmarkData'; | ||
import { groupDataServiceApiRef } from '../services/GroupDataService'; | ||
|
||
export const useMetricBenchmark = (type: string) => { | ||
const groupDataService = useApi(groupDataServiceApiRef); | ||
const [benchmark, setDfBenchmark] = useState<dfBenchmarkKey | undefined>(); | ||
const [error, setError] = useState<Error | undefined>(); | ||
|
||
useEffect(() => { | ||
groupDataService.retrieveBenchmarkData({ type: type }).then(response => { | ||
setDfBenchmark(response.key); | ||
}, setError); | ||
}, [groupDataService, type]); | ||
|
||
return { error: error, benchmark: benchmark }; | ||
}; |
31 changes: 31 additions & 0 deletions
31
backstage-plugin/plugins/open-dora/src/hooks/MetricDataHook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useApi } from '@backstage/core-plugin-api'; | ||
import { useContext, useEffect, useState } from 'react'; | ||
import { MetricData } from '../models/MetricData'; | ||
import { groupDataServiceApiRef } from '../services/GroupDataService'; | ||
import { MetricContext } from '../services/MetricContext'; | ||
|
||
export const useMetricData = (type: string) => { | ||
const groupDataService = useApi(groupDataServiceApiRef); | ||
const [chartData, setChartData] = useState<MetricData | undefined>(); | ||
const [error, setError] = useState<Error | undefined>(); | ||
const { aggregation, team, project } = useContext(MetricContext); | ||
|
||
useEffect(() => { | ||
groupDataService | ||
.retrieveMetricDataPoints({ | ||
type: type, | ||
team: team, | ||
aggregation: aggregation, | ||
project: project, | ||
}) | ||
.then(response => { | ||
if (response.dataPoints.length > 0) { | ||
setChartData(response); | ||
} else { | ||
setError(new Error('No data found')); | ||
} | ||
}, setError); | ||
}, [aggregation, team, project, groupDataService, type]); | ||
|
||
return { error: error, chartData: chartData }; | ||
}; |
Oops, something went wrong.