import { css } from '@emotion/css';
import React, { useState } from 'react';
import { GrafanaTheme2, VariableOrigin, DataLinkBuiltInVars } from '@grafana/data';
import { Button, useTheme2 } from '@grafana/ui';
import { DerivedFieldConfig } from '../types';
import { DebugSection } from './DebugSection';
import { DerivedField } from './DerivedField';
const getStyles = (theme: GrafanaTheme2) => ({
infoText: css`
padding-bottom: ${theme.spacing(2)};
color: ${theme.colors.text.secondary};
`,
derivedField: css`
margin-bottom: ${theme.spacing(1)};
`,
});
type Props = {
value?: DerivedFieldConfig[];
onChange: (value: DerivedFieldConfig[]) => void;
};
export const DerivedFields = (props: Props) => {
const { value, onChange } = props;
const theme = useTheme2();
const styles = getStyles(theme);
const [showDebug, setShowDebug] = useState(false);
return (
<>
Derived fields
Derived fields can be used to extract new fields from a log message and create a link from its value.
{value &&
value.map((field, index) => {
return (
{
const newDerivedFields = [...value];
newDerivedFields.splice(index, 1, newField);
onChange(newDerivedFields);
}}
onDelete={() => {
const newDerivedFields = [...value];
newDerivedFields.splice(index, 1);
onChange(newDerivedFields);
}}
suggestions={[
{
value: DataLinkBuiltInVars.valueRaw,
label: 'Raw value',
documentation: 'Exact string captured by the regular expression',
origin: VariableOrigin.Value,
},
]}
/>
);
})}
{value && value.length > 0 && (
)}
{showDebug && (
)}
>
);
};