/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * * Any modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ /* * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import React, { useState, useEffect } from 'react'; import { EuiLink, EuiIconTip, EuiText, EuiPopoverFooter, EuiButton, EuiSpacer } from '@elastic/eui'; import { FormattedMessage } from '@osd/i18n/react'; import { DiscoverFieldBucket } from './discover_field_bucket'; import { getWarnings } from './lib/get_warnings'; import { triggerVisualizeActions, isFieldVisualizable, getVisualizeHref, } from './lib/visualize_trigger_utils'; import { Bucket, FieldDetails } from './types'; import { IndexPatternField, IndexPattern } from '../../../../../data/public'; import './discover_field_details.scss'; interface DiscoverFieldDetailsProps { columns: string[]; details: FieldDetails; field: IndexPatternField; indexPattern: IndexPattern; onAddFilter: (field: IndexPatternField | string, value: string, type: '+' | '-') => void; } export function DiscoverFieldDetails({ columns, details, field, indexPattern, onAddFilter, }: DiscoverFieldDetailsProps) { const warnings = getWarnings(field); const [showVisualizeLink, setShowVisualizeLink] = useState(false); const [visualizeLink, setVisualizeLink] = useState(''); useEffect(() => { const checkIfVisualizable = async () => { const visualizable = await isFieldVisualizable(field, indexPattern.id, columns).catch( () => false ); setShowVisualizeLink(visualizable); if (visualizable) { const href = await getVisualizeHref(field, indexPattern.id, columns).catch(() => ''); setVisualizeLink(href || ''); } }; checkIfVisualizable(); }, [field, indexPattern.id, columns]); const handleVisualizeLinkClick = (event: React.MouseEvent) => { // regular link click. let the uiActions code handle the navigation and show popup if needed event.preventDefault(); triggerVisualizeActions(field, indexPattern.id, columns); }; return ( <>
{details.error && ( {details.error} )} {!details.error && details.buckets.length > 0 && (
{details.buckets.map((bucket: Bucket, idx: number) => ( ))}
)} {showVisualizeLink && visualizeLink && (
{/* eslint-disable-next-line @elastic/eui/href-or-on-click */} handleVisualizeLinkClick(e)} href={visualizeLink} size="s" className="dscFieldDetails__visualizeBtn" data-test-subj={`fieldVisualize-${field.name}`} > {warnings.length > 0 && ( )}
)}
{!details.error && ( {!indexPattern.metaFields.includes(field.name) && !field.scripted ? ( onAddFilter('_exists_', field.name, '+')}> {' '} {details.exists} ) : ( {details.exists} )}{' '} / {details.total}{' '} )} ); }