GraphContextMenu.tsx 1.8 KB

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