NodeGraph: Fix error when clicking link in a context menu (#34817)

* Add prevent default if onClick exists

* Apply field overrides
This commit is contained in:
Andrej Ocenas 2021-05-27 15:33:25 +02:00 committed by GitHub
parent 8ced249c91
commit fcdab32750
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 5 deletions

View File

@ -43,7 +43,16 @@ export const MenuItem: React.FC<MenuItemProps> = React.memo(
href={url ? url : undefined}
target={target}
className={styles.link}
onClick={onClick}
onClick={
onClick
? (event) => {
if (!(event.ctrlKey || event.metaKey || event.shiftKey) && onClick) {
event.preventDefault();
onClick(event);
}
}
: undefined
}
rel={target === '_blank' ? 'noopener noreferrer' : undefined}
>
{icon && <Icon name={icon} className={styles.icon} />} {label}

View File

@ -1,7 +1,7 @@
import React from 'react';
import { useToggle } from 'react-use';
import { Badge, Collapse, useStyles2 } from '@grafana/ui';
import { DataFrame, GrafanaTheme2, TimeRange } from '@grafana/data';
import { Badge, Collapse, useStyles2, useTheme2 } from '@grafana/ui';
import { applyFieldOverrides, DataFrame, GrafanaTheme2, TimeRange } from '@grafana/data';
import { css } from '@emotion/css';
import { ExploreId, StoreState } from '../../types';
import { splitOpen } from './state/main';
@ -30,10 +30,26 @@ interface Props {
export function UnconnectedNodeGraphContainer(props: Props & ConnectedProps<typeof connector>) {
const { dataFrames, range, splitOpen, withTraceView } = props;
const getLinks = useLinks(range, splitOpen);
const theme = useTheme2();
const styles = useStyles2(getStyles);
const { nodes } = useCategorizeFrames(dataFrames);
// This is implicit dependency that is needed for links to work. At some point when replacing variables in the link
// it requires field to have a display property which is added by the overrides even though we don't add any field
// overrides in explore.
const frames = applyFieldOverrides({
fieldConfig: {
defaults: {},
overrides: [],
},
data: dataFrames,
// We don't need proper replace here as it is only used in getLinks and we use getFieldLinks
replaceVariables: (value) => value,
theme,
});
const { nodes } = useCategorizeFrames(frames);
const [open, toggleOpen] = useToggle(false);
const countWarning =
withTraceView && nodes[0]?.length > 1000 ? (
<span className={styles.warningText}> ({nodes[0].length} nodes, can be slow to load)</span>
@ -53,7 +69,7 @@ export function UnconnectedNodeGraphContainer(props: Props & ConnectedProps<type
onToggle={withTraceView ? () => toggleOpen() : undefined}
>
<div style={{ height: withTraceView ? 500 : 600 }}>
<NodeGraph dataFrames={dataFrames} getLinks={getLinks} />
<NodeGraph dataFrames={frames} getLinks={getLinks} />
</div>
</Collapse>
);

View File

@ -108,6 +108,7 @@ function mapMenuItem<T extends NodeDatum | EdgeDatum>(item: T) {
label={link.label}
ariaLabel={link.ariaLabel || link.label}
onClick={link.onClick ? () => link.onClick?.(item) : undefined}
target={'_self'}
/>
);
};