2022-11-07 08:19:31 -06:00
import React from 'react' ;
import { NavModelItem } from '@grafana/data' ;
import { RouteDescriptor } from 'app/core/navigation/types' ;
import { getRootSectionForNode } from 'app/core/selectors/navModel' ;
import AppRootPage from 'app/features/plugins/components/AppRootPage' ;
import { getState } from 'app/store/store' ;
export function getAppPluginRoutes ( ) : RouteDescriptor [ ] {
const state = getState ( ) ;
const { navIndex } = state ;
const isStandalonePluginPage = ( id : string ) = > id . startsWith ( 'standalone-plugin-page-/' ) ;
const isPluginNavModelItem = ( model : NavModelItem ) : model is PluginNavModelItem = >
'pluginId' in model && 'id' in model ;
2022-11-16 04:49:34 -06:00
const explicitAppPluginRoutes = Object . values ( navIndex )
2022-11-07 08:19:31 -06:00
. filter < PluginNavModelItem > ( isPluginNavModelItem )
. map ( ( navItem ) = > {
const pluginNavSection = getRootSectionForNode ( navItem ) ;
const appPluginUrl = ` /a/ ${ navItem . pluginId } ` ;
const path = isStandalonePluginPage ( navItem . id ) ? navItem . url || appPluginUrl : appPluginUrl ; // Only standalone pages can use core URLs, otherwise we fall back to "/a/:pluginId"
2023-02-03 02:01:34 -06:00
const isSensitive = isStandalonePluginPage ( navItem . id ) && ! navItem . url ? . startsWith ( '/a/' ) ; // Have case-sensitive URLs only for standalone pages that have custom URLs
2022-11-07 08:19:31 -06:00
return {
path ,
exact : false , // route everything under this path to the plugin, so it can define more routes under this path
2023-02-03 02:01:34 -06:00
sensitive : isSensitive ,
2022-11-07 08:19:31 -06:00
component : ( ) = > < AppRootPage pluginId = { navItem . pluginId } pluginNavSection = { pluginNavSection } / > ,
} ;
} ) ;
2022-11-16 04:49:34 -06:00
return [
. . . explicitAppPluginRoutes ,
// Fallback route for plugins that don't have any pages under includes
{
path : '/a/:pluginId' ,
exact : false , // route everything under this path to the plugin, so it can define more routes under this path
component : ( { match } ) = > < AppRootPage pluginId = { match . params . pluginId } pluginNavSection = { navIndex . home } / > ,
} ,
] ;
2022-11-07 08:19:31 -06:00
}
interface PluginNavModelItem extends Omit < NavModelItem , ' pluginId ' | ' id ' > {
pluginId : string ;
id : string ;
}