import { css } from '@emotion/css';
import cx from 'classnames';
import React, { useState } from 'react';
import { ArrayVector, Field, FieldType, LinkModel } from '@grafana/data';
import { LegacyForms } from '@grafana/ui';
import { getFieldLinksForExplore } from '../../../../features/explore/utils/links';
import { DerivedFieldConfig } from '../types';
const { FormField } = LegacyForms;
type Props = {
derivedFields?: DerivedFieldConfig[];
className?: string;
};
export const DebugSection = (props: Props) => {
const { derivedFields, className } = props;
const [debugText, setDebugText] = useState('');
let debugFields: DebugField[] = [];
if (debugText && derivedFields) {
debugFields = makeDebugFields(derivedFields, debugText);
}
return (
setDebugText(event.currentTarget.value)}
/>
}
/>
{!!debugFields.length && }
);
};
type DebugFieldItemProps = {
fields: DebugField[];
};
const DebugFields = ({ fields }: DebugFieldItemProps) => {
return (
Name |
Value |
Url |
{fields.map((field) => {
let value: any = field.value;
if (field.error) {
value = field.error.message;
} else if (field.href) {
value = {value};
}
return (
{field.name} |
{value} |
{field.href ? {field.href} : ''} |
);
})}
);
};
type DebugField = {
name: string;
error?: any;
value?: string;
href?: string;
};
function makeDebugFields(derivedFields: DerivedFieldConfig[], debugText: string): DebugField[] {
return derivedFields
.filter((field) => field.name && field.matcherRegex)
.map((field) => {
try {
const testMatch = debugText.match(field.matcherRegex);
const value = testMatch && testMatch[1];
let link: LinkModel | null = null;
if (field.url && value) {
link = getFieldLinksForExplore({
field: {
name: '',
type: FieldType.string,
values: new ArrayVector([value]),
config: {
links: [{ title: '', url: field.url }],
},
},
rowIndex: 0,
range: {} as any,
})[0];
}
return {
name: field.name,
value: value || '',
href: link && link.href,
} as DebugField;
} catch (error) {
return {
name: field.name,
error,
} as DebugField;
}
});
}