From 9a918668948d2092e56420e0bc08e22bb6fdc999 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 30 Nov 2020 17:58:40 +0100 Subject: [PATCH] core-api: dry up route collectors --- packages/core-api/src/routing/collectors.tsx | 50 +++++++------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/packages/core-api/src/routing/collectors.tsx b/packages/core-api/src/routing/collectors.tsx index 38e5376547..db6673536d 100644 --- a/packages/core-api/src/routing/collectors.tsx +++ b/packages/core-api/src/routing/collectors.tsx @@ -14,11 +14,22 @@ * limitations under the License. */ -import { isValidElement, ReactNode } from 'react'; +import { isValidElement, ReactElement, ReactNode } from 'react'; import { BackstageRouteObject, RouteRef } from '../routing/types'; import { getComponentData } from '../extensions'; import { createCollector } from '../extensions/traversal'; +function getMountPoint(node: ReactElement): RouteRef | undefined { + const element: ReactNode = node.props?.element; + + let routeRef = getComponentData(node, 'core.mountPoint'); + if (!routeRef && isValidElement(element)) { + routeRef = getComponentData(element, 'core.mountPoint'); + } + + return routeRef; +} + export const routePathCollector = createCollector( () => new Map(), (acc, node, parent) => { @@ -26,26 +37,13 @@ export const routePathCollector = createCollector( return; } - const path: string | undefined = node.props?.path; - const element: ReactNode = node.props?.element; - - const routeRef = getComponentData(node, 'core.mountPoint'); + const routeRef = getMountPoint(node); if (routeRef) { + const path: string | undefined = node.props?.path; if (!path) { throw new Error('Mounted routable extension must have a path'); } acc.set(routeRef, path); - } else if (isValidElement(element)) { - const elementRouteRef = getComponentData( - element, - 'core.mountPoint', - ); - if (elementRouteRef) { - if (!path) { - throw new Error('Route element must have a path'); - } - acc.set(elementRouteRef, path); - } } }, ); @@ -57,24 +55,12 @@ export const routeParentCollector = createCollector( return parentRouteRef; } - const element: ReactNode = node.props?.element; - let nextParent = parentRouteRef; - const routeRef = getComponentData(node, 'core.mountPoint'); + const routeRef = getMountPoint(node); if (routeRef) { acc.set(routeRef, parentRouteRef); nextParent = routeRef; - } else if (isValidElement(element)) { - const elementRouteRef = getComponentData( - element, - 'core.mountPoint', - ); - - if (elementRouteRef) { - acc.set(elementRouteRef, parentRouteRef); - nextParent = elementRouteRef; - } } return nextParent; @@ -90,12 +76,8 @@ export const routeObjectCollector = createCollector( const path: string | undefined = node.props?.path; const caseSensitive: boolean = Boolean(node.props?.caseSensitive); - const element: ReactNode = node.props?.element; - let routeRef = getComponentData(node, 'core.mountPoint'); - if (!routeRef && isValidElement(element)) { - routeRef = getComponentData(element, 'core.mountPoint'); - } + const routeRef = getMountPoint(node); if (routeRef) { const children: BackstageRouteObject[] = []; if (!path) {