DerivedFields.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { css } from '@emotion/css';
  2. import React, { useState } from 'react';
  3. import { GrafanaTheme2, VariableOrigin, DataLinkBuiltInVars } from '@grafana/data';
  4. import { Button, useTheme2 } from '@grafana/ui';
  5. import { DerivedFieldConfig } from '../types';
  6. import { DebugSection } from './DebugSection';
  7. import { DerivedField } from './DerivedField';
  8. const getStyles = (theme: GrafanaTheme2) => ({
  9. infoText: css`
  10. padding-bottom: ${theme.spacing(2)};
  11. color: ${theme.colors.text.secondary};
  12. `,
  13. derivedField: css`
  14. margin-bottom: ${theme.spacing(1)};
  15. `,
  16. });
  17. type Props = {
  18. value?: DerivedFieldConfig[];
  19. onChange: (value: DerivedFieldConfig[]) => void;
  20. };
  21. export const DerivedFields = (props: Props) => {
  22. const { value, onChange } = props;
  23. const theme = useTheme2();
  24. const styles = getStyles(theme);
  25. const [showDebug, setShowDebug] = useState(false);
  26. return (
  27. <>
  28. <h3 className="page-heading">Derived fields</h3>
  29. <div className={styles.infoText}>
  30. Derived fields can be used to extract new fields from a log message and create a link from its value.
  31. </div>
  32. <div className="gf-form-group">
  33. {value &&
  34. value.map((field, index) => {
  35. return (
  36. <DerivedField
  37. className={styles.derivedField}
  38. key={index}
  39. value={field}
  40. onChange={(newField) => {
  41. const newDerivedFields = [...value];
  42. newDerivedFields.splice(index, 1, newField);
  43. onChange(newDerivedFields);
  44. }}
  45. onDelete={() => {
  46. const newDerivedFields = [...value];
  47. newDerivedFields.splice(index, 1);
  48. onChange(newDerivedFields);
  49. }}
  50. suggestions={[
  51. {
  52. value: DataLinkBuiltInVars.valueRaw,
  53. label: 'Raw value',
  54. documentation: 'Exact string captured by the regular expression',
  55. origin: VariableOrigin.Value,
  56. },
  57. ]}
  58. />
  59. );
  60. })}
  61. <div>
  62. <Button
  63. variant="secondary"
  64. className={css`
  65. margin-right: 10px;
  66. `}
  67. icon="plus"
  68. onClick={(event) => {
  69. event.preventDefault();
  70. const newDerivedFields = [...(value || []), { name: '', matcherRegex: '' }];
  71. onChange(newDerivedFields);
  72. }}
  73. >
  74. Add
  75. </Button>
  76. {value && value.length > 0 && (
  77. <Button variant="secondary" type="button" onClick={() => setShowDebug(!showDebug)}>
  78. {showDebug ? 'Hide example log message' : 'Show example log message'}
  79. </Button>
  80. )}
  81. </div>
  82. </div>
  83. {showDebug && (
  84. <div className="gf-form-group">
  85. <DebugSection
  86. className={css`
  87. margin-bottom: 10px;
  88. `}
  89. derivedFields={value}
  90. />
  91. </div>
  92. )}
  93. </>
  94. );
  95. };