From 1ff191d593895983878629f34797cbb3f747989a Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Sun, 28 May 2023 19:53:30 +0200 Subject: [PATCH] Add plugins set to routeObject and use in analytics if 1 Signed-off-by: Eric Peterson --- .../core-app-api/src/routing/RouteTracker.tsx | 5 +++- .../core-app-api/src/routing/collectors.tsx | 29 +++++++++++++++++++ packages/core-app-api/src/routing/types.ts | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/core-app-api/src/routing/RouteTracker.tsx b/packages/core-app-api/src/routing/RouteTracker.tsx index 5ec858b28e..39f4c6f269 100644 --- a/packages/core-app-api/src/routing/RouteTracker.tsx +++ b/packages/core-app-api/src/routing/RouteTracker.tsx @@ -21,6 +21,7 @@ import { AnalyticsContext, RouteRef, AnalyticsEventAttributes, + BackstagePlugin, } from '@backstage/core-plugin-api'; import { BackstageRouteObject } from './types'; @@ -53,8 +54,10 @@ const getExtensionContext = ( // If there is a single route ref, return it. // todo: get routeRef of rendered gathered mount point(?) let routeRef: RouteRef | undefined; + let plugin: BackstagePlugin | undefined; if (routeObject.routeRefs.size === 1) { routeRef = routeObject.routeRefs.values().next().value; + plugin = routeObject.plugins.values().next().value; } const params = Object.entries( @@ -68,7 +71,7 @@ const getExtensionContext = ( return { extension: 'App', - pluginId: routeObject.plugin?.getId() || 'root', + pluginId: plugin?.getId() || 'root', ...(routeRef ? { routeRef: (routeRef as { id?: string }).id } : {}), params, }; diff --git a/packages/core-app-api/src/routing/collectors.tsx b/packages/core-app-api/src/routing/collectors.tsx index 0bf5e676a4..bd795d7947 100644 --- a/packages/core-app-api/src/routing/collectors.tsx +++ b/packages/core-app-api/src/routing/collectors.tsx @@ -33,6 +33,7 @@ export const MATCH_ALL_ROUTE: BackstageRouteObject = { path: '*', element: 'match-all', // These elements aren't used, so we add in a bit of debug information routeRefs: new Set(), + plugins: new Set(), }; function stringifyNode(node: ReactNode): string { @@ -45,6 +46,16 @@ function stringifyNode(node: ReactNode): string { return String(anyNode); } +const pluginSet = ( + plugin: BackstagePlugin | undefined, +): Set => { + const set = new Set(); + if (plugin) { + set.add(plugin); + } + return set; +}; + interface RoutingV2CollectorContext { routeRef?: RouteRef; gatherPath?: string; @@ -132,6 +143,7 @@ export const routingV2Collector = createCollector( routeRefs: new Set(), caseSensitive: Boolean(node.props?.caseSensitive), children: [MATCH_ALL_ROUTE], + plugins: new Set(), plugin: undefined, }; parentChildren.push(newObj); @@ -162,6 +174,7 @@ export const routingV2Collector = createCollector( routeRefs: new Set([routeRef]), caseSensitive: Boolean(node.props?.caseSensitive), children: [MATCH_ALL_ROUTE], + plugins: pluginSet(plugin), plugin, }; parentChildren.push(newObj); @@ -187,6 +200,15 @@ export const routingV2Collector = createCollector( } ctx?.obj?.routeRefs.add(mountPoint); + + const mountPointPlugin = getComponentData( + node, + 'core.plugin', + ); + if (mountPointPlugin) { + ctx?.obj?.plugins.add(mountPointPlugin); + } + acc.paths.set(mountPoint, ctx.gatherPath); acc.parents.set(mountPoint, ctx?.gatherRouteRef); @@ -306,6 +328,12 @@ export const routingV1Collector = createCollector( element: 'mounted', routeRefs: new Set([routeRef]), children: [MATCH_ALL_ROUTE], + plugins: pluginSet( + getComponentData( + node.props.element, + 'core.plugin', + ), + ), plugin: getComponentData( node.props.element, 'core.plugin', @@ -336,6 +364,7 @@ export const routingV1Collector = createCollector( element: 'gathered', routeRefs: new Set(), children: [MATCH_ALL_ROUTE], + plugins: pluginSet(ctx?.obj?.plugin), plugin: ctx?.obj?.plugin, }; parentChildren.push(currentObj); diff --git a/packages/core-app-api/src/routing/types.ts b/packages/core-app-api/src/routing/types.ts index e2cf665982..3515c70d5c 100644 --- a/packages/core-app-api/src/routing/types.ts +++ b/packages/core-app-api/src/routing/types.ts @@ -54,6 +54,7 @@ export interface BackstageRouteObject { element: React.ReactNode; path: string; routeRefs: Set; + plugins: Set; plugin?: BackstagePlugin; }