GraphContextMenu.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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, ...otherProps }) => {
  10. const theme = useContext(ThemeContext);
  11. const source = getContextMenuSource();
  12. const renderHeader = source
  13. ? () => {
  14. if (!source) {
  15. return null;
  16. }
  17. const timeFormat = source.series.hasMsResolution ? 'YYYY-MM-DD HH:mm:ss.SSS' : 'YYYY-MM-DD HH:mm:ss';
  18. return (
  19. <div
  20. className={css`
  21. padding: ${theme.spacing.xs} ${theme.spacing.sm};
  22. font-size: ${theme.typography.size.sm};
  23. `}
  24. >
  25. <strong>{dateTime(source.datapoint[0]).format(timeFormat)}</strong>
  26. <div>
  27. <SeriesIcon color={source.series.color} />
  28. <span
  29. className={css`
  30. white-space: nowrap;
  31. padding-left: ${theme.spacing.xs};
  32. `}
  33. >
  34. {source.series.alias}
  35. </span>
  36. </div>
  37. </div>
  38. );
  39. }
  40. : null;
  41. return <ContextMenu {...otherProps} renderHeader={renderHeader} />;
  42. };