forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sources_panel_for_start_chat.tsx
73 lines (65 loc) · 2.69 KB
/
sources_panel_for_start_chat.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { EuiFlexGroup, EuiFlexItem, EuiLoadingSpinner } from '@elastic/eui';
import React, { useEffect } from 'react';
import { i18n } from '@kbn/i18n';
import { useController } from 'react-hook-form';
import { AddIndicesField } from './add_indices_field';
import { IndicesTable } from './indices_table';
import { StartChatPanel } from '../start_chat_panel';
import { CreateIndexCallout } from './create_index_callout';
import { useSourceIndicesField } from '../../hooks/useSourceIndicesField';
import { useQueryIndices } from '../../hooks/useQueryIndices';
import { ChatFormFields } from '../../types';
import { useIndicesFields } from '../../hooks/useIndicesFields';
import { createQuery, getDefaultQueryFields } from '../../lib/create_query';
export const SourcesPanelForStartChat: React.FC = () => {
const { selectedIndices, removeIndex, addIndex } = useSourceIndicesField();
const { indices, isLoading } = useQueryIndices();
const { fields } = useIndicesFields(selectedIndices || []);
const {
field: { onChange: elasticsearchQueryOnChange },
} = useController({
name: ChatFormFields.elasticsearchQuery,
defaultValue: {},
});
useEffect(() => {
if (fields) {
const defaultFields = getDefaultQueryFields(fields);
elasticsearchQueryOnChange(createQuery(defaultFields, fields));
}
}, [selectedIndices, fields, elasticsearchQueryOnChange]);
return (
<StartChatPanel
title={i18n.translate('aiPlayground.emptyPrompts.sources.title', {
defaultMessage: 'Select sources',
})}
description={i18n.translate('aiPlayground.emptyPrompts.sources.description', {
defaultMessage: 'Where should the data for this chat experience be retrieved from?',
})}
isValid={!!selectedIndices.length}
>
{!!selectedIndices?.length && (
<EuiFlexItem>
<IndicesTable indices={selectedIndices} onRemoveClick={removeIndex} />
</EuiFlexItem>
)}
{isLoading && (
<EuiFlexGroup justifyContent="center" alignItems="center">
<EuiLoadingSpinner size="l" />
</EuiFlexGroup>
)}
{!isLoading && !!indices?.length && (
<EuiFlexItem>
<AddIndicesField selectedIndices={selectedIndices} onIndexSelect={addIndex} />
</EuiFlexItem>
)}
{!isLoading && !indices?.length && <CreateIndexCallout />}
</StartChatPanel>
);
};