Bazaar: Added 'isBazaarAvailable' helper

The EntityBazaarInfoCard shows the bazaar data for components that
have a bazaar item linked. The isBazaarAvailable helper makes it
possible to only include the EntityBazaarInfoCard for components
that actually have a bazaar project linked.

Closes #15013

Signed-off-by: Niklas Aronsson <niklasar@axis.com>
This commit is contained in:
Niklas Aronsson
2022-12-30 11:55:08 +01:00
parent 7f4abd5f1c
commit 41d0b0a24a
5 changed files with 45 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-bazaar': patch
---
Added `isBazaarAvailable` helper to be used with the `EntitySwitch`.
+8 -5
View File
@@ -49,17 +49,20 @@ Add a **Bazaar icon** to the Sidebar to easily access the Bazaar. In `packages/a
Add a **Bazaar card** to the overview tab on the `packages/app/src/components/catalog/EntityPage.tsx` add:
```diff
+ import { EntityBazaarInfoCard } from '@backstage/plugin-bazaar';
+ import { EntityBazaarInfoCard, isBazaarAvailable } from '@backstage/plugin-bazaar';
const overviewContent = (
<Grid item md={8} xs={12}>
<EntityAboutCard variant="gridItem" />
</Grid>
+ <Grid item sm={6}>
+ <EntityBazaarInfoCard />
+ </Grid>
+ <EntitySwitch>
+ <EntitySwitch.Case if={isBazaarAvailable}>
+ <Grid item sm={6}>
+ <EntityBazaarInfoCard />
+ </Grid>
+ </EntitySwitch.Case>
+ </EntitySwitch>
{/* ...other entity-cards */}
```
+10
View File
@@ -5,7 +5,9 @@
```ts
/// <reference types="react" />
import { ApiHolder } from '@backstage/core-plugin-api';
import { BackstagePlugin } from '@backstage/core-plugin-api';
import { Entity } from '@backstage/catalog-model';
import { RouteRef } from '@backstage/core-plugin-api';
// @public (undocumented)
@@ -36,6 +38,14 @@ export const bazaarPlugin: BackstagePlugin<
// @public (undocumented)
export const EntityBazaarInfoCard: () => JSX.Element | null;
// @public (undocumented)
export const isBazaarAvailable: (
entity: Entity,
context: {
apis: ApiHolder;
},
) => Promise<boolean>;
// @public (undocumented)
export const SortView: () => JSX.Element;
+21
View File
@@ -14,7 +14,9 @@
* limitations under the License.
*/
import { Entity, stringifyEntityRef } from '@backstage/catalog-model';
import {
ApiHolder,
createApiRef,
DiscoveryApi,
FetchApi,
@@ -46,6 +48,25 @@ export interface BazaarApi {
deleteProject(id: number): Promise<void>;
}
/** @public */
export const isBazaarAvailable = async (
entity: Entity,
context: { apis: ApiHolder },
): Promise<boolean> => {
const bazaarClient = context.apis.get(bazaarApiRef);
if (bazaarClient === undefined) {
return false;
}
const entityRef = stringifyEntityRef({
kind: entity.kind,
name: entity.metadata.name,
namespace: entity.metadata.namespace,
});
const response = await bazaarClient.getProjectByRef(entityRef);
const project = await response.json();
return project.data.length > 0;
};
export class BazaarClient implements BazaarApi {
private readonly identityApi: IdentityApi;
private readonly discoveryApi: DiscoveryApi;
+1
View File
@@ -15,6 +15,7 @@
*/
export { bazaarPlugin, BazaarPage } from './plugin';
export { isBazaarAvailable } from './api';
export { BazaarOverviewCard } from './components/BazaarOverviewCard';
export type { BazaarOverviewCardProps } from './components/BazaarOverviewCard';
export { EntityBazaarInfoCard } from './components/EntityBazaarInfoCard';