From 9b4b4050958b822d8647190d0a38a101fd8f84b2 Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 2 Jun 2021 17:35:35 +0200 Subject: [PATCH] feat: reworking the techRadar plugin to support providing your own TechRadarApi to override how the data get's into your plugin Signed-off-by: blam --- plugins/tech-radar/README.md | 1 - plugins/tech-radar/src/api.ts | 22 +++++------ .../src/components/RadarComponent.tsx | 37 ++++++++----------- plugins/tech-radar/src/plugin.ts | 5 +++ .../src/{sampleData.ts => sample.ts} | 21 +++++++---- 5 files changed, 44 insertions(+), 42 deletions(-) rename plugins/tech-radar/src/{sampleData.ts => sample.ts} (94%) diff --git a/plugins/tech-radar/README.md b/plugins/tech-radar/README.md index 814ab6cba5..149ab54840 100644 --- a/plugins/tech-radar/README.md +++ b/plugins/tech-radar/README.md @@ -60,7 +60,6 @@ export type TechRadarPageProps = TechRadarComponentProps & { export interface TechRadarPageProps { width: number; height: number; - getData?: () => Promise; svgProps?: object; } ``` diff --git a/plugins/tech-radar/src/api.ts b/plugins/tech-radar/src/api.ts index eca80420bf..6f2a1a5cb1 100644 --- a/plugins/tech-radar/src/api.ts +++ b/plugins/tech-radar/src/api.ts @@ -15,6 +15,17 @@ */ import { MovedState } from './utils/types'; +import { createApiRef } from '@backstage/core'; + +export const techRadarApiRef = createApiRef({ + id: 'plugin.techradar.service', + description: 'Used to populate data in the TechRadar plugin', +}); + +export interface TechRadarApi { + // Loads the TechRadar response data to pass through to the TechRadar component + load: () => Promise; +} /** * Types related to the Radar's visualization. @@ -65,16 +76,5 @@ export interface TechRadarLoaderResponse { export interface TechRadarComponentProps { width: number; height: number; - getData?: () => Promise; svgProps?: object; } - -/** - * Set up the Radar as a Backstage plugin. - */ - -export interface TechRadarApi extends TechRadarComponentProps { - title?: string; - subtitle?: string; - pageTitle?: string; -} diff --git a/plugins/tech-radar/src/components/RadarComponent.tsx b/plugins/tech-radar/src/components/RadarComponent.tsx index a6e033a08e..39746e48fe 100644 --- a/plugins/tech-radar/src/components/RadarComponent.tsx +++ b/plugins/tech-radar/src/components/RadarComponent.tsx @@ -15,38 +15,35 @@ */ import React, { useEffect } from 'react'; -import { Progress, useApi, errorApiRef, ErrorApi } from '@backstage/core'; +import { Progress, useApi, errorApiRef } from '@backstage/core'; import { useAsync } from 'react-use'; import Radar from '../components/Radar'; -import { TechRadarComponentProps, TechRadarLoaderResponse } from '../api'; -import getSampleData from '../sampleData'; +import { + techRadarApiRef, + TechRadarComponentProps, + TechRadarLoaderResponse, +} from '../api'; import { Entry } from '../utils/types'; -const useTechRadarLoader = (props: TechRadarComponentProps) => { - const errorApi = useApi(errorApiRef); +const useTechRadarLoader = () => { + const errorApi = useApi(errorApiRef); + const techRadarApi = useApi(techRadarApiRef); - const { getData } = props; - - const state = useAsync(async () => { - if (getData) { - const response: TechRadarLoaderResponse = await getData(); - return response; - } - return undefined; - }, [getData, errorApi]); + const { error, value, loading } = useAsync(async () => techRadarApi.load(), [ + techRadarApi, + ]); useEffect(() => { - const { error } = state; if (error) { errorApi.post(error); } - }, [errorApi, state]); + }, [error, errorApi]); - return state; + return { loading, value, error }; }; const RadarComponent = (props: TechRadarComponentProps): JSX.Element => { - const { loading, error, value: data } = useTechRadarLoader(props); + const { loading, error, value: data } = useTechRadarLoader(); const mapToEntries = ( loaderResponse: TechRadarLoaderResponse | undefined, @@ -89,8 +86,4 @@ const RadarComponent = (props: TechRadarComponentProps): JSX.Element => { ); }; -RadarComponent.defaultProps = { - getData: getSampleData, -}; - export default RadarComponent; diff --git a/plugins/tech-radar/src/plugin.ts b/plugins/tech-radar/src/plugin.ts index 63af1f390f..e128600094 100644 --- a/plugins/tech-radar/src/plugin.ts +++ b/plugins/tech-radar/src/plugin.ts @@ -18,8 +18,12 @@ import { createPlugin, createRouteRef, createRoutableExtension, + createApiFactory, } from '@backstage/core'; +import { techRadarApiRef } from './api'; +import { SampleTechRadarApi } from './sample'; + const rootRouteRef = createRouteRef({ title: 'Tech Radar', }); @@ -29,6 +33,7 @@ export const techRadarPlugin = createPlugin({ routes: { root: rootRouteRef, }, + apis: [createApiFactory(techRadarApiRef, new SampleTechRadarApi())], }); export const TechRadarPage = techRadarPlugin.provide( diff --git a/plugins/tech-radar/src/sampleData.ts b/plugins/tech-radar/src/sample.ts similarity index 94% rename from plugins/tech-radar/src/sampleData.ts rename to plugins/tech-radar/src/sample.ts index 70b9303f39..ff895bcdb7 100644 --- a/plugins/tech-radar/src/sampleData.ts +++ b/plugins/tech-radar/src/sample.ts @@ -19,6 +19,7 @@ import { RadarQuadrant, RadarEntry, TechRadarLoaderResponse, + TechRadarApi, } from './api'; const rings = new Array(); @@ -164,15 +165,19 @@ entries.push({ ], url: '#', key: 'github-actions', - id: 'github-actions', - title: 'GitHub Actions', + id: 'github-actiosns', + title: 'GitHub Acssstions', quadrant: 'infrastructure', }); -export default function getSampleData(): Promise { - return Promise.resolve({ - rings, - quadrants, - entries, - }); +export const mock: TechRadarLoaderResponse = { + entries, + quadrants, + rings, +}; + +export class SampleTechRadarApi implements TechRadarApi { + async load() { + return mock; + } }