FunctionEditor.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { css } from '@emotion/css';
  2. import React from 'react';
  3. import { GrafanaTheme2 } from '@grafana/data';
  4. import { Icon, Tooltip, useStyles2 } from '@grafana/ui';
  5. import { FuncInstance } from '../gfunc';
  6. import { FunctionEditorControls, FunctionEditorControlsProps } from './FunctionEditorControls';
  7. interface FunctionEditorProps extends FunctionEditorControlsProps {
  8. func: FuncInstance;
  9. }
  10. const getStyles = (theme: GrafanaTheme2) => {
  11. return {
  12. icon: css`
  13. margin-right: ${theme.spacing(0.5)};
  14. `,
  15. label: css({
  16. fontWeight: theme.typography.fontWeightMedium,
  17. fontSize: theme.typography.bodySmall.fontSize, // to match .gf-form-label
  18. cursor: 'pointer',
  19. display: 'inline-block',
  20. }),
  21. };
  22. };
  23. const FunctionEditor: React.FC<FunctionEditorProps> = ({ onMoveLeft, onMoveRight, func, ...props }) => {
  24. const styles = useStyles2(getStyles);
  25. const renderContent = ({ updatePopperPosition }: any) => (
  26. <FunctionEditorControls
  27. {...props}
  28. func={func}
  29. onMoveLeft={() => {
  30. onMoveLeft(func);
  31. updatePopperPosition();
  32. }}
  33. onMoveRight={() => {
  34. onMoveRight(func);
  35. updatePopperPosition();
  36. }}
  37. />
  38. );
  39. return (
  40. <>
  41. {func.def.unknown && (
  42. <Tooltip content={<TooltipContent />} placement="bottom" interactive>
  43. <Icon data-testid="warning-icon" name="exclamation-triangle" size="xs" className={styles.icon} />
  44. </Tooltip>
  45. )}
  46. <Tooltip content={renderContent} placement="top" interactive>
  47. <span className={styles.label}>{func.def.name}</span>
  48. </Tooltip>
  49. </>
  50. );
  51. };
  52. const TooltipContent = React.memo(() => {
  53. return (
  54. <span>
  55. This function is not supported. Check your function for typos and{' '}
  56. <a
  57. target="_blank"
  58. className="external-link"
  59. rel="noreferrer noopener"
  60. href="https://graphite.readthedocs.io/en/latest/functions.html"
  61. >
  62. read the docs
  63. </a>{' '}
  64. to see whether you need to upgrade your data source’s version to make this function available.
  65. </span>
  66. );
  67. });
  68. TooltipContent.displayName = 'FunctionEditorTooltipContent';
  69. export { FunctionEditor };