grafana/public/app/features/plugins/utils.ts
Torkel Ödegaard b4f73c9f09
PluginPages: Support plugin pages that don't belong to a section (#55904)
* Fixing pages that don't exist in navtree

* Fix test

* fix lint warning

* Fixes
2022-09-29 13:27:51 +02:00

112 lines
3.5 KiB
TypeScript

import { Location as HistoryLocation } from 'history';
import { GrafanaPlugin, NavIndex, NavModel, NavModelItem, PanelPluginMeta, PluginType } from '@grafana/data';
import { config } from '@grafana/runtime';
import { importPanelPluginFromMeta } from './importPanelPlugin';
import { getPluginSettings } from './pluginSettings';
import { importAppPlugin, importDataSourcePlugin } from './plugin_loader';
export async function loadPlugin(pluginId: string): Promise<GrafanaPlugin> {
const info = await getPluginSettings(pluginId);
let result: GrafanaPlugin | undefined;
if (info.type === PluginType.app) {
result = await importAppPlugin(info);
}
if (info.type === PluginType.datasource) {
result = await importDataSourcePlugin(info);
}
if (info.type === PluginType.panel) {
const panelPlugin = await importPanelPluginFromMeta(info as PanelPluginMeta);
result = panelPlugin as unknown as GrafanaPlugin;
}
if (info.type === PluginType.renderer) {
result = { meta: info } as GrafanaPlugin;
}
if (!result) {
throw new Error('Unknown Plugin type: ' + info.type);
}
return result;
}
export function buildPluginSectionNav(
location: HistoryLocation,
pluginNav: NavModel | null,
navIndex: NavIndex,
pluginId: string
): NavModel | undefined {
// When topnav is disabled we only just show pluginNav like before
if (!config.featureToggles.topnav) {
return pluginNav ?? undefined;
}
let section = getPluginSection(location, navIndex, pluginId);
if (!section) {
return undefined;
}
// shallow clone as we set active flag
section = { ...section };
// If we have plugin nav don't set active page in section as it will cause double breadcrumbs
const currentUrl = config.appSubUrl + location.pathname + location.search;
let activePage: NavModelItem | undefined;
function setPageToActive(page: NavModelItem, currentUrl: string): NavModelItem {
if (!currentUrl.startsWith(page.url ?? '')) {
return page;
}
if (activePage && (activePage.url?.length ?? 0) > (page.url?.length ?? 0)) {
return page;
}
if (activePage) {
activePage.active = false;
}
activePage = { ...page, active: true };
return activePage;
}
// Find and set active page
section.children = (section?.children ?? []).map((child) => {
if (child.children) {
return {
...child,
children: child.children.map((pluginPage) => setPageToActive(pluginPage, currentUrl)),
};
}
return setPageToActive(child, currentUrl);
});
return { main: section, node: activePage ?? section };
}
// TODO make work for sub pages
export function getPluginSection(location: HistoryLocation, navIndex: NavIndex, pluginId: string): NavModelItem {
// First check if this page exist in navIndex using path, some plugin pages are not under their own section
const byPath = navIndex[`standalone-plugin-page-${location.pathname}`];
if (byPath) {
const parent = byPath.parentItem!;
// in case the standalone page is in nested section
return parent.parentItem ?? parent;
}
// Some plugins like cloud home don't have any precense in the navtree so we need to allow those
const navTreeNodeForPlugin = navIndex[`plugin-page-${pluginId}`];
if (!navTreeNodeForPlugin) {
return { id: 'root-plugin-page', text: 'Root plugin page', hideFromBreadcrumbs: true };
}
if (!navTreeNodeForPlugin.parentItem) {
throw new Error('Could not find plugin section');
}
return navTreeNodeForPlugin.parentItem;
}