grafana/public/app/features/dashboard/dashgrid/PanelHeader/PanelHeaderMenu.tsx

106 lines
3.0 KiB
TypeScript
Raw Normal View History

import classnames from 'classnames';
import React, { PureComponent } from 'react';
import { PanelMenuItem } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
import { Menu } from '@grafana/ui';
import { PanelHeaderMenuItem } from './PanelHeaderMenuItem';
export interface Props {
items: PanelMenuItem[];
style?: React.CSSProperties;
itemsClassName?: string;
className?: string;
}
export class PanelHeaderMenu extends PureComponent<Props> {
renderItems = (menu: PanelMenuItem[], isSubMenu = false) => {
return (
<ul
className={classnames('dropdown-menu', 'dropdown-menu--menu', 'panel-menu', this.props.itemsClassName)}
style={this.props.style}
role={isSubMenu ? '' : 'menu'}
>
{menu.map((menuItem, idx: number) => {
return (
<PanelHeaderMenuItem
key={`${menuItem.text}${idx}`}
type={menuItem.type}
text={menuItem.text}
iconClassName={menuItem.iconClassName}
onClick={menuItem.onClick}
shortcut={menuItem.shortcut}
Plugins: Extend panel menu with links from plugins (#63089) * feat(plugins): introduce dashboard panel menu placement for adding menu items * test: add test for getPanelMenu() * added an unique identifier for each extension. * added context to getPluginExtensions. * wip * Wip * wiwip * Wip * feat: WWWIIIIPPPP 🧨 * Wip * Renamed some of the types to align a bit better. * added limit to how many extensions a plugin can register per placement. * decreased number of items to 2 * will trim the lenght of titles to max 25 chars. * wrapping configure function with error handling. * added error handling for all scenarios. * moved extension menu items to the bottom of the more sub menu. * added tests for configuring the title. * minor refactorings. * changed so you need to specify the full path in package.json. * wip * removed unused type. * big refactor to make things simpler and to centralize all configure error/validation handling. * added missing import. * fixed failing tests. * fixed tests. * revert(extensions): remove static extensions config in favour of registering via AppPlugin APIs * removed the compose that didn't work for some reason. * added tests just to verify that validation and error handling is tied together in configuration function. * adding some more values to the context. * draft validation. * added missing tests for getPanelMenu. * added more tests. * refactor(extensions): move logic for validating extension link config to function * Fixed ts errors. * Update packages/grafana-data/src/types/app.ts Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com> * Update packages/grafana-runtime/src/services/pluginExtensions/extensions.test.ts Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com> * refactor(extensions): rename limiter -> pluginPlacementCount * refactor(getpanelmenu): remove redundant continue statement --------- Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com> Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>
2023-03-02 08:42:00 -06:00
href={menuItem.href}
>
{menuItem.subMenu && this.renderItems(menuItem.subMenu, true)}
</PanelHeaderMenuItem>
);
})}
</ul>
);
};
render() {
return (
<div className={classnames('panel-menu-container', 'dropdown', 'open', this.props.className)}>
{this.renderItems(flattenGroups(this.props.items))}
</div>
);
}
}
function flattenGroups(items: PanelMenuItem[]): PanelMenuItem[] {
return items.reduce((all: PanelMenuItem[], item) => {
if (Array.isArray(item.subMenu) && item.type === 'submenu') {
all.push({
...item,
subMenu: flattenGroups(item.subMenu),
});
return all;
}
if (Array.isArray(item.subMenu) && item.type === 'group') {
const { subMenu, ...rest } = item;
all.push(rest);
all.push.apply(all, flattenGroups(subMenu));
return all;
}
all.push(item);
return all;
}, []);
}
export function PanelHeaderMenuNew({ items }: Props) {
const renderItems = (items: PanelMenuItem[]) => {
return items.map((item) => {
switch (item.type) {
case 'divider':
return <Menu.Divider key={item.text} />;
case 'group':
return (
<Menu.Group key={item.text} label={item.text}>
{item.subMenu ? renderItems(item.subMenu) : undefined}
</Menu.Group>
);
default:
return (
<Menu.Item
key={item.text}
label={item.text}
icon={item.iconClassName}
childItems={item.subMenu ? renderItems(item.subMenu) : undefined}
url={item.href}
onClick={item.onClick}
shortcut={item.shortcut}
testId={selectors.components.Panels.Panel.menuItems(item.text)}
/>
);
}
});
};
return <Menu>{renderItems(items)}</Menu>;
}