import React from 'react'; import { FieldConfig, LinkModel } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { css } from 'emotion'; import { WithContextMenu } from '../ContextMenu/WithContextMenu'; import { linkModelToContextMenuItems } from '../../utils/dataLinks'; interface DataLinksContextMenuProps { children: (props: DataLinksContextMenuApi) => JSX.Element; links: () => LinkModel[]; config: FieldConfig; } export interface DataLinksContextMenuApi { openMenu?: React.MouseEventHandler; targetClassName?: string; } export const DataLinksContextMenu: React.FC = ({ children, links, config }) => { const linksCounter = config.links!.length; const getDataLinksContextMenuItems = () => { return [{ items: linkModelToContextMenuItems(links), label: 'Data links' }]; }; // Use this class name (exposed via render prop) to add context menu indicator to the click target of the visualization const targetClassName = css` cursor: context-menu; `; if (linksCounter > 1) { return ( {({ openMenu }) => { return children({ openMenu, targetClassName }); }} ); } else { const linkModel = links()[0]; return ( {children({})} ); } };