GraphContextMenu.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import React, { useContext } from 'react';
  2. import { FlotDataPoint } from './GraphContextMenuCtrl';
  3. import { ContextMenu, ContextMenuProps, SeriesIcon, ThemeContext } from '@grafana/ui';
  4. import { dateTime } from '@grafana/data';
  5. import { css } from 'emotion';
  6. type GraphContextMenuProps = ContextMenuProps & {
  7. getContextMenuSource: () => FlotDataPoint | null;
  8. };
  9. export const GraphContextMenu: React.FC<GraphContextMenuProps> = ({ getContextMenuSource, items, ...otherProps }) => {
  10. const theme = useContext(ThemeContext);
  11. const source = getContextMenuSource();
  12. // Do not render items that do not have label specified
  13. const itemsToRender = items
  14. ? items.map(group => ({
  15. ...group,
  16. items: group.items.filter(item => item.label),
  17. }))
  18. : [];
  19. const renderHeader = source
  20. ? () => {
  21. if (!source) {
  22. return null;
  23. }
  24. const timeFormat = source.series.hasMsResolution ? 'YYYY-MM-DD HH:mm:ss.SSS' : 'YYYY-MM-DD HH:mm:ss';
  25. return (
  26. <div
  27. className={css`
  28. padding: ${theme.spacing.xs} ${theme.spacing.sm};
  29. font-size: ${theme.typography.size.sm};
  30. `}
  31. >
  32. <strong>{dateTime(source.datapoint[0]).format(timeFormat)}</strong>
  33. <div>
  34. <SeriesIcon color={source.series.color} />
  35. <span
  36. className={css`
  37. white-space: nowrap;
  38. padding-left: ${theme.spacing.xs};
  39. `}
  40. >
  41. {source.series.alias}
  42. </span>
  43. </div>
  44. </div>
  45. );
  46. }
  47. : null;
  48. return <ContextMenu {...otherProps} items={itemsToRender} renderHeader={renderHeader} />;
  49. };