Skip to content

Commit

Permalink
reuse CreateDataViewButton
Browse files Browse the repository at this point in the history
  • Loading branch information
darnautov committed Apr 16, 2024
1 parent ce01620 commit 6ff569b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
import { EuiButton } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import React, { useCallback, useEffect, useRef } from 'react';
import { type DataView } from '@kbn/data-plugin/common';
import { useMlKibana } from '../../contexts/kibana';

export const CreateDataViewButton = ({
onDataViewCreated,
allowAdHocDataView = false,
}: {
onDataViewCreated: (id: string, type: string, name?: string) => void;
onDataViewCreated: (dataView: DataView) => void;
allowAdHocDataView?: boolean;
}) => {
const { dataViewEditor } = useMlKibana().services;
Expand All @@ -25,10 +26,9 @@ export const CreateDataViewButton = ({
closeDataViewEditorRef.current = dataViewEditor?.openEditor({
onSave: async (dataView) => {
if (dataView.id && onDataViewCreated) {
onDataViewCreated(dataView.id, 'index-pattern', dataView.name);
onDataViewCreated(dataView);
}
},

allowAdHocDataView,
});
}, [onDataViewCreated, dataViewEditor, allowAdHocDataView]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ export const SourceSelection: FC = () => {
uiSettings,
}}
>
<CreateDataViewButton onDataViewCreated={onSearchSelected} allowAdHocDataView={true} />
<CreateDataViewButton
onDataViewCreated={(dataView) => {
onSearchSelected(dataView.id!, 'index-pattern', dataView.getIndexPattern());
}}
allowAdHocDataView={true}
/>
</SavedObjectFinder>
</EuiPanel>
</EuiPageBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ export const Page: FC<PageProps> = ({
>
<EuiFlexGroup direction="row" gutterSize="s">
<CreateDataViewButton
onDataViewCreated={onObjectSelection}
onDataViewCreated={(dataView) => {
onObjectSelection(dataView.id!, 'index-pattern', dataView.getIndexPattern());
}}
allowAdHocDataView={true}
/>
{extraButtons ? extraButtons : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
*/

import type { FC } from 'react';
import React, { useState, useMemo, useEffect, useCallback } from 'react';
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';

import useObservable from 'react-use/lib/useObservable';
import { firstValueFrom } from 'rxjs';
Expand All @@ -15,14 +16,17 @@ import {
EuiAccordion,
EuiCode,
EuiCodeBlock,
EuiFlexGroup,
EuiFlexItem,
EuiFormRow,
EuiSpacer,
EuiSelect,
EuiSpacer,
EuiText,
} from '@elastic/eui';

import { isPopulatedObject } from '@kbn/ml-is-populated-object';
import { i18n } from '@kbn/i18n';
import { CreateDataViewButton } from '../../../components/create_data_view_button';
import { useMlKibana } from '../../../contexts/kibana';
import { RUNNING_STATE } from './inference_base';
import type { InferrerType } from '.';
Expand All @@ -45,6 +49,7 @@ export const InferenceInputFormIndexControls: FC<Props> = ({
setSelectedDataViewId,
selectedField,
setSelectedField,
setDataViewListItems,
} = data;

const runningState = useObservable(inferrer.getRunningState$(), inferrer.getRunningState());
Expand All @@ -53,27 +58,56 @@ export const InferenceInputFormIndexControls: FC<Props> = ({

return (
<>
<EuiFormRow label="Index" fullWidth>
{disableIndexSelection ? (
<EuiText grow={false}>
<EuiCode>
{dataViewListItems.find((item) => item.value === selectedDataViewId)?.text}
</EuiCode>
</EuiText>
) : (
<EuiSelect
options={dataViewListItems}
value={selectedDataViewId}
onChange={(e) => {
inferrer.setSelectedDataViewId(e.target.value);
setSelectedDataViewId(e.target.value);
}}
hasNoInitialSelection={true}
disabled={runningState === RUNNING_STATE.RUNNING}
<EuiFlexGroup gutterSize={'s'} justifyContent={'spaceBetween'} alignItems={'flexEnd'}>
<EuiFlexItem grow={true}>
<EuiFormRow
label={
<FormattedMessage
id="xpack.ml.trainedModels.testModelsFlyout.dataViewTitle"
defaultMessage="Data view"
/>
}
fullWidth
>
{disableIndexSelection ? (
<EuiText grow={false}>
<EuiCode>
{dataViewListItems.find((item) => item.value === selectedDataViewId)?.text}
</EuiCode>
</EuiText>
) : (
<EuiSelect
options={dataViewListItems}
value={selectedDataViewId}
onChange={(e) => {
inferrer.setSelectedDataViewId(e.target.value);
setSelectedDataViewId(e.target.value);
}}
hasNoInitialSelection={true}
disabled={runningState === RUNNING_STATE.RUNNING}
fullWidth
/>
)}
</EuiFormRow>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<CreateDataViewButton
allowAdHocDataView
onDataViewCreated={(dataView) => {
setDataViewListItems((prev) => {
return [
...prev,
{
text: dataView.getIndexPattern(),
value: dataView.id!,
},
];
});
}}
/>
)}
</EuiFormRow>
</EuiFlexItem>
</EuiFlexGroup>

<EuiSpacer size="m" />
<EuiFormRow
label={i18n.translate('xpack.ml.trainedModels.testModelsFlyout.indexInput.fieldInput', {
Expand Down Expand Up @@ -277,5 +311,6 @@ export function useIndexInput({
setSelectedDataViewId,
selectedField,
setSelectedField,
setDataViewListItems,
};
}

0 comments on commit 6ff569b

Please sign in to comment.