/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import './datapanel.scss';
import React, { memo, useCallback } from 'react';
import { i18n } from '@kbn/i18n';
import {
EuiText,
EuiNotificationBadge,
EuiSpacer,
EuiAccordion,
EuiLoadingSpinner,
EuiIconTip,
} from '@elastic/eui';
import classNames from 'classnames';
import { DataPublicPluginStart } from 'src/plugins/data/public';
import { IndexPatternField } from './types';
import { FieldItem } from './field_item';
import { Query, Filter } from '../../../../../src/plugins/data/public';
import { DatasourceDataPanelProps } from '../types';
import { IndexPattern } from './types';
import { ChartsPluginSetup } from '../../../../../src/plugins/charts/public';
export interface FieldItemSharedProps {
core: DatasourceDataPanelProps['core'];
data: DataPublicPluginStart;
chartsThemeService: ChartsPluginSetup['theme'];
indexPattern: IndexPattern;
highlight?: string;
query: Query;
dateRange: DatasourceDataPanelProps['dateRange'];
filters: Filter[];
}
export interface FieldsAccordionProps {
initialIsOpen: boolean;
onToggle: (open: boolean) => void;
id: string;
label: string;
helpTooltip?: string;
hasLoaded: boolean;
fieldsCount: number;
isFiltered: boolean;
paginatedFields: IndexPatternField[];
fieldProps: FieldItemSharedProps;
renderCallout: JSX.Element;
exists: (field: IndexPatternField) => boolean;
showExistenceFetchError?: boolean;
hideDetails?: boolean;
}
export const InnerFieldsAccordion = function InnerFieldsAccordion({
initialIsOpen,
onToggle,
id,
label,
helpTooltip,
hasLoaded,
fieldsCount,
isFiltered,
paginatedFields,
fieldProps,
renderCallout,
exists,
hideDetails,
showExistenceFetchError,
}: FieldsAccordionProps) {
const renderField = useCallback(
(field: IndexPatternField) => (
),
[fieldProps, exists, hideDetails]
);
const titleClassname = classNames({
// eslint-disable-next-line @typescript-eslint/naming-convention
lnsInnerIndexPatternDataPanel__titleTooltip: !!helpTooltip,
});
return (
{label}
{!!helpTooltip && (
)}
}
extraAction={
showExistenceFetchError ? (
) : hasLoaded ? (
{fieldsCount}
) : (
)
}
>
{hasLoaded &&
(!!fieldsCount ? (
{paginatedFields && paginatedFields.map(renderField)}
) : (
renderCallout
))}
);
};
export const FieldsAccordion = memo(InnerFieldsAccordion);