mirror of
https://github.com/grafana/grafana.git
synced 2025-02-13 00:55:47 -06:00
* chore: fix go lint issues * feat(Routing): route standalone plugin pages to the `AppRoutePage` * feat(plugin.json): introduce a new field called `isCorePage` for `includes` * chore: add explanatory comments for types * refactor(AppRootPage): receive the `pluginId` and `pluginSection` through the props Now we are able to receive these as props as the pluginId is defined on navLinks that are registered by plugins. * chore: update teests for AppRootPage * fix: remove rebase issue * tests(applinks): add a test for checking isCorePage plugin page setting * refactor(applinks): update tests to use FindById() and be more resilient to changes * fix: Go lint issues * refactor(routes): use cleaner types when working with plugin nav nodes Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com> * chore: fix linting issues * t: remove `isCorePage` field from includes Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
78 lines
2.3 KiB
TypeScript
78 lines
2.3 KiB
TypeScript
import { GrafanaPlugin, 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(
|
|
pluginNavSection: NavModelItem,
|
|
pluginNav: NavModel | null,
|
|
currentUrl: string
|
|
): NavModel | undefined {
|
|
// When topnav is disabled we only just show pluginNav like before
|
|
if (!config.featureToggles.topnav) {
|
|
return pluginNav ?? undefined;
|
|
}
|
|
|
|
// shallow clone as we set active flag
|
|
let copiedPluginNavSection = { ...pluginNavSection };
|
|
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
|
|
copiedPluginNavSection.children = (copiedPluginNavSection?.children ?? []).map((child) => {
|
|
if (child.children) {
|
|
return {
|
|
...setPageToActive(child, currentUrl),
|
|
children: child.children.map((pluginPage) => setPageToActive(pluginPage, currentUrl)),
|
|
};
|
|
}
|
|
|
|
return setPageToActive(child, currentUrl);
|
|
});
|
|
|
|
return { main: copiedPluginNavSection, node: activePage ?? copiedPluginNavSection };
|
|
}
|