GraphContextMenu.tsx 1.4 KB

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