|
| 1 | +import { EuiFlexGroup, EuiFlexItem, EuiForm, EuiFormRow } from '@elastic/eui'; |
| 2 | +import React, { SyntheticEvent, useEffect, useMemo, useRef, useState } from 'react'; |
| 3 | +import { IDataPluginServices, PersistedLog } from '../../../../../src/plugins/data/public'; |
| 4 | +import { SearchBarExtensionDependencies } from '../../../../../src/plugins/data/public/ui/search_bar_extensions/search_bar_extension'; |
| 5 | +import { useOpenSearchDashboards } from '../../../../../src/plugins/opensearch_dashboards_react/public'; |
| 6 | +import { getStorage } from '../../services'; |
| 7 | +import { useGenerateQuery } from '../hooks'; |
| 8 | +import { getPersistedLog, ProhibitedQueryError } from '../utils'; |
| 9 | +import { QueryAssistCallOut, QueryAssistCallOutType } from './call_outs'; |
| 10 | +import { QueryAssistInput } from './query_assist_input'; |
| 11 | +import { QueryAssistSubmitButton } from './submit_button'; |
| 12 | + |
| 13 | +interface QueryAssistInputProps { |
| 14 | + dependencies: SearchBarExtensionDependencies; |
| 15 | +} |
| 16 | + |
| 17 | +export const QueryAssistBar: React.FC<QueryAssistInputProps> = (props) => { |
| 18 | + const { services } = useOpenSearchDashboards<IDataPluginServices>(); |
| 19 | + const inputRef = useRef<HTMLInputElement>(null); |
| 20 | + const storage = getStorage(); |
| 21 | + const persistedLog: PersistedLog = useMemo( |
| 22 | + () => getPersistedLog(services.uiSettings, storage, 'query-assist'), |
| 23 | + [services.uiSettings, storage] |
| 24 | + ); |
| 25 | + const { generateQuery, loading } = useGenerateQuery(); |
| 26 | + const [callOutType, setCallOutType] = useState<QueryAssistCallOutType>(); |
| 27 | + const dismissCallout = () => setCallOutType(undefined); |
| 28 | + const mounted = useRef(false); |
| 29 | + const selectedIndex = props.dependencies.indexPatterns?.at(0)?.title; |
| 30 | + const previousQuestionRef = useRef<string>(); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + mounted.current = true; |
| 34 | + return () => { |
| 35 | + mounted.current = false; |
| 36 | + }; |
| 37 | + }, []); |
| 38 | + |
| 39 | + const onSubmit = async (e: SyntheticEvent) => { |
| 40 | + e.preventDefault(); |
| 41 | + if (!inputRef.current?.value) { |
| 42 | + setCallOutType('empty_query'); |
| 43 | + return; |
| 44 | + } |
| 45 | + if (!selectedIndex) { |
| 46 | + setCallOutType('empty_index'); |
| 47 | + return; |
| 48 | + } |
| 49 | + dismissCallout(); |
| 50 | + previousQuestionRef.current = inputRef.current.value; |
| 51 | + persistedLog.add(inputRef.current.value); |
| 52 | + const params = { |
| 53 | + question: inputRef.current.value, |
| 54 | + index: selectedIndex, |
| 55 | + language: 'PPL', |
| 56 | + }; |
| 57 | + const { response, error } = await generateQuery(params); |
| 58 | + if (!mounted.current) return; |
| 59 | + if (error) { |
| 60 | + if (error instanceof ProhibitedQueryError) { |
| 61 | + setCallOutType('invalid_query'); |
| 62 | + } else { |
| 63 | + services.notifications.toasts.addError(error, { title: 'Failed to generate results' }); |
| 64 | + } |
| 65 | + } else if (response) { |
| 66 | + services.data.query.queryString.setQuery({ |
| 67 | + query: response.query, |
| 68 | + language: params.language, |
| 69 | + }); |
| 70 | + if (response.timeRange) services.data.query.timefilter.timefilter.setTime(response.timeRange); |
| 71 | + setCallOutType('query_generated'); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + return ( |
| 76 | + <EuiForm component="form" onSubmit={onSubmit}> |
| 77 | + <EuiFormRow fullWidth> |
| 78 | + <EuiFlexGroup gutterSize="s" responsive={false} alignItems="center"> |
| 79 | + <EuiFlexItem> |
| 80 | + <QueryAssistInput |
| 81 | + inputRef={inputRef} |
| 82 | + persistedLog={persistedLog} |
| 83 | + selectedIndex={selectedIndex} |
| 84 | + previousQuestion={previousQuestionRef.current} |
| 85 | + /> |
| 86 | + </EuiFlexItem> |
| 87 | + <EuiFlexItem grow={false}> |
| 88 | + <QueryAssistSubmitButton isDisabled={loading} /> |
| 89 | + </EuiFlexItem> |
| 90 | + </EuiFlexGroup> |
| 91 | + </EuiFormRow> |
| 92 | + <QueryAssistCallOut type={callOutType} onDismiss={dismissCallout} /> |
| 93 | + </EuiForm> |
| 94 | + ); |
| 95 | +}; |
0 commit comments