Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/plugins/discover/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"navigation",
"uiActions",
"savedObjects",
"dataViewFieldEditor"
"dataViewFieldEditor",
"dataViewEditor"
],
"optionalPlugins": ["home", "share", "usageCollection", "spaces"],
"requiredBundles": ["kibanaUtils", "home", "kibanaReact", "dataViews"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
import { FieldStatisticsTable } from '../field_stats_table';
import { VIEW_MODE } from '../../../../components/view_mode_toggle';
import { DOCUMENTS_VIEW_CLICK, FIELD_STATISTICS_VIEW_CLICK } from '../field_stats_table/constants';
import { DataViewType } from '../../../../../../data_views/common';
import { DataViewType, DataView } from '../../../../../../data_views/common';

/**
* Local storage key for sidebar persistence state
Expand Down Expand Up @@ -204,6 +204,14 @@ export function DiscoverLayout({
}, [isSidebarClosed, storage]);

const contentCentered = resultState === 'uninitialized' || resultState === 'none';
const onDataViewCreated = useCallback(
(dataView: DataView) => {
if (dataView.id) {
onChangeIndexPattern(dataView.id);
}
},
[onChangeIndexPattern]
);

return (
<EuiPage className="dscPage" data-fetch-counter={fetchCounter.current}>
Expand Down Expand Up @@ -245,6 +253,7 @@ export function DiscoverLayout({
useNewFieldsApi={useNewFieldsApi}
onEditRuntimeField={onEditRuntimeField}
viewMode={viewMode}
onDataViewCreated={onDataViewCreated}
/>
</EuiFlexItem>
<EuiHideFor sizes={['xs', 's']}>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('Discover DataView Management', () => {
const indexPattern = stubLogstashIndexPattern;

const editField = jest.fn();
const createNewDataView = jest.fn();

const mountComponent = () => {
return mountWithIntl(
Expand All @@ -62,6 +63,7 @@ describe('Discover DataView Management', () => {
editField={editField}
selectedIndexPattern={indexPattern}
useNewFieldsApi={true}
createNewDataView={createNewDataView}
/>
</KibanaContextProvider>
);
Expand All @@ -81,7 +83,7 @@ describe('Discover DataView Management', () => {
button.simulate('click');

expect(component.find(EuiContextMenuPanel).length).toBe(1);
expect(component.find(EuiContextMenuItem).length).toBe(2);
expect(component.find(EuiContextMenuItem).length).toBe(3);
});

test('click on an add button executes editField callback', () => {
Expand All @@ -103,4 +105,14 @@ describe('Discover DataView Management', () => {
manageButton.simulate('click');
expect(mockServices.core.application.navigateToApp).toHaveBeenCalled();
});

test('click on add dataView button executes createNewDataView callback', () => {
const component = mountComponent();
const button = findTestSubject(component, 'discoverIndexPatternActions');
button.simulate('click');

const manageButton = findTestSubject(component, 'dataview-create-new');
manageButton.simulate('click');
expect(createNewDataView).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
*/

import React, { useState } from 'react';
import { EuiButtonIcon, EuiContextMenuItem, EuiContextMenuPanel, EuiPopover } from '@elastic/eui';
import {
EuiButtonIcon,
EuiContextMenuItem,
EuiContextMenuPanel,
EuiHorizontalRule,
EuiPopover,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { useDiscoverServices } from '../../../../utils/use_discover_services';
import { DataView } from '../../../../../../data/common';
Expand All @@ -26,11 +32,16 @@ export interface DiscoverIndexPatternManagementProps {
* @param fieldName
*/
editField: (fieldName?: string) => void;

/**
* Callback to execute on create new data action
*/
createNewDataView: () => void;
}

export function DiscoverIndexPatternManagement(props: DiscoverIndexPatternManagementProps) {
const { dataViewFieldEditor, core } = useDiscoverServices();
const { useNewFieldsApi, selectedIndexPattern, editField } = props;
const { useNewFieldsApi, selectedIndexPattern, editField, createNewDataView } = props;
const dataViewEditPermission = dataViewFieldEditor?.userPermissions.editIndexPattern();
const canEditDataViewField = !!dataViewEditPermission && useNewFieldsApi;
const [isAddIndexPatternFieldPopoverOpen, setIsAddIndexPatternFieldPopoverOpen] = useState(false);
Expand All @@ -45,7 +56,7 @@ export function DiscoverIndexPatternManagement(props: DiscoverIndexPatternManage

return (
<EuiPopover
panelPaddingSize="s"
panelPaddingSize="none"
isOpen={isAddIndexPatternFieldPopoverOpen}
closePopover={() => {
setIsAddIndexPatternFieldPopoverOpen(false);
Expand All @@ -67,7 +78,8 @@ export function DiscoverIndexPatternManagement(props: DiscoverIndexPatternManage
}
>
<EuiContextMenuPanel
size="s"
size="m"
title="Data view"
items={[
<EuiContextMenuItem
key="add"
Expand All @@ -79,7 +91,7 @@ export function DiscoverIndexPatternManagement(props: DiscoverIndexPatternManage
}}
>
{i18n.translate('discover.fieldChooser.indexPatterns.addFieldButton', {
defaultMessage: 'Add field to data view',
defaultMessage: 'Add field',
})}
</EuiContextMenuItem>,
<EuiContextMenuItem
Expand All @@ -94,7 +106,21 @@ export function DiscoverIndexPatternManagement(props: DiscoverIndexPatternManage
}}
>
{i18n.translate('discover.fieldChooser.indexPatterns.manageFieldButton', {
defaultMessage: 'Manage data view fields',
defaultMessage: 'Manage settings',
})}
</EuiContextMenuItem>,
<EuiHorizontalRule style={{ margin: '0px' }} />,
<EuiContextMenuItem
key="new"
icon="plusInCircleFilled"
data-test-subj="dataview-create-new"
onClick={() => {
setIsAddIndexPatternFieldPopoverOpen(false);
createNewDataView();
}}
>
{i18n.translate('discover.fieldChooser.dataViews.createNewDataView', {
defaultMessage: 'Create new data view',
})}
</EuiContextMenuItem>,
]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ function getCompProps(): DiscoverSidebarProps {
onEditRuntimeField: jest.fn(),
editField: jest.fn(),
viewMode: VIEW_MODE.DOCUMENT_LEVEL,
createNewDataView: jest.fn(),
onDataViewCreated: jest.fn(),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export interface DiscoverSidebarProps extends Omit<DiscoverSidebarResponsiveProp

editField: (fieldName?: string) => void;

createNewDataView: () => void;

/**
* a statistics of the distribution of fields in the given hits
*/
Expand Down Expand Up @@ -104,6 +106,7 @@ export function DiscoverSidebarComponent({
closeFlyout,
editField,
viewMode,
createNewDataView,
}: DiscoverSidebarProps) {
const { uiSettings, dataViewFieldEditor } = useDiscoverServices();
const [fields, setFields] = useState<DataViewField[] | null>(null);
Expand Down Expand Up @@ -299,6 +302,7 @@ export function DiscoverSidebarComponent({
selectedIndexPattern={selectedIndexPattern}
editField={editField}
useNewFieldsApi={useNewFieldsApi}
createNewDataView={createNewDataView}
/>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down Expand Up @@ -336,6 +340,7 @@ export function DiscoverSidebarComponent({
selectedIndexPattern={selectedIndexPattern}
useNewFieldsApi={useNewFieldsApi}
editField={editField}
createNewDataView={createNewDataView}
/>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function getCompProps(): DiscoverSidebarResponsiveProps {
trackUiMetric: jest.fn(),
onEditRuntimeField: jest.fn(),
viewMode: VIEW_MODE.DOCUMENT_LEVEL,
onDataViewCreated: jest.fn(),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export interface DiscoverSidebarResponsiveProps {
* callback to execute on edit runtime field
*/
onEditRuntimeField: () => void;
/**
* callback to execute on create dataview
*/
onDataViewCreated: (dataView: DataView) => void;
/**
* Discover view mode
*/
Expand All @@ -115,7 +119,13 @@ export interface DiscoverSidebarResponsiveProps {
*/
export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps) {
const services = useDiscoverServices();
const { selectedIndexPattern, onEditRuntimeField, useNewFieldsApi, onChangeIndexPattern } = props;
const {
selectedIndexPattern,
onEditRuntimeField,
useNewFieldsApi,
onChangeIndexPattern,
onDataViewCreated,
} = props;
const [fieldFilter, setFieldFilter] = useState(getDefaultFieldFilter());
const [isFlyoutVisible, setIsFlyoutVisible] = useState(false);
/**
Expand Down Expand Up @@ -146,12 +156,16 @@ export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps)
}, [selectedIndexPattern]);

const closeFieldEditor = useRef<() => void | undefined>();
const closeDataViewEditor = useRef<() => void | undefined>();
Comment thread
kertal marked this conversation as resolved.

useEffect(() => {
const cleanup = () => {
if (closeFieldEditor?.current) {
closeFieldEditor?.current();
}
if (closeDataViewEditor?.current) {
closeDataViewEditor?.current();
}
};
return () => {
// Make sure to close the editor when unmounting
Expand All @@ -163,11 +177,15 @@ export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps)
closeFieldEditor.current = ref;
}, []);

const setDataViewEditorRef = useCallback((ref: () => void | undefined) => {
closeDataViewEditor.current = ref;
}, []);

const closeFlyout = useCallback(() => {
setIsFlyoutVisible(false);
}, []);

const { dataViewFieldEditor } = services;
const { dataViewFieldEditor, dataViewEditor } = services;

const editField = useCallback(
(fieldName?: string) => {
Expand Down Expand Up @@ -203,6 +221,24 @@ export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps)
]
);

const createNewDataView = useCallback(() => {
const indexPatternFieldEditPermission = dataViewEditor.userPermissions.editDataView;
if (!indexPatternFieldEditPermission) {
return;
}
const ref = dataViewEditor.openEditor({
onSave: async (dataView) => {
onDataViewCreated(dataView);
},
});
if (setDataViewEditorRef) {
setDataViewEditorRef(ref);
}
if (closeFlyout) {
closeFlyout();
}
}, [dataViewEditor, setDataViewEditorRef, closeFlyout, onDataViewCreated]);

if (!selectedIndexPattern) {
return null;
}
Expand All @@ -218,6 +254,7 @@ export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps)
fieldCounts={fieldCounts.current}
setFieldFilter={setFieldFilter}
editField={editField}
createNewDataView={createNewDataView}
/>
</EuiHideFor>
)}
Expand All @@ -244,6 +281,7 @@ export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps)
selectedIndexPattern={selectedIndexPattern}
editField={editField}
useNewFieldsApi={useNewFieldsApi}
createNewDataView={createNewDataView}
/>
</EuiFlexItem>
</EuiFlexGroup>
Expand Down Expand Up @@ -307,6 +345,7 @@ export function DiscoverSidebarResponsive(props: DiscoverSidebarResponsiveProps)
setFieldEditorRef={setFieldEditorRef}
closeFlyout={closeFlyout}
editField={editField}
createNewDataView={createNewDataView}
/>
</div>
</EuiFlyout>
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/discover/public/build_services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { FieldFormatsStart } from '../../field_formats/public';
import { EmbeddableStart } from '../../embeddable/public';

import type { SpacesApi } from '../../../../x-pack/plugins/spaces/public';
import { DataViewEditorStart } from '../../../plugins/data_view_editor/public';

export interface HistoryLocationState {
referrer: string;
Expand Down Expand Up @@ -68,6 +69,7 @@ export interface DiscoverServices {
uiSettings: IUiSettingsClient;
trackUiMetric?: (metricType: UiCounterMetricType, eventName: string | string[]) => void;
dataViewFieldEditor: IndexPatternFieldEditorStart;
dataViewEditor: DataViewEditorStart;
http: HttpStart;
storage: Storage;
spaces?: SpacesApi;
Expand Down Expand Up @@ -109,5 +111,6 @@ export const buildServices = memoize(function (
dataViewFieldEditor: plugins.dataViewFieldEditor,
http: core.http,
spaces: plugins.spaces,
dataViewEditor: plugins.dataViewEditor,
};
});
2 changes: 2 additions & 0 deletions src/plugins/discover/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import type { SpacesPluginStart } from '../../../../x-pack/plugins/spaces/public
import { FieldFormatsStart } from '../../field_formats/public';
import { injectTruncateStyles } from './utils/truncate_styles';
import { DOC_TABLE_LEGACY, TRUNCATE_MAX_HEIGHT } from '../common';
import { DataViewEditorStart } from '../../../plugins/data_view_editor/public';
import { useDiscoverServices } from './utils/use_discover_services';

declare module '../../share/public' {
Expand Down Expand Up @@ -176,6 +177,7 @@ export interface DiscoverSetupPlugins {
* @internal
*/
export interface DiscoverStartPlugins {
dataViewEditor: DataViewEditorStart;
uiActions: UiActionsStart;
embeddable: EmbeddableStart;
navigation: NavigationStart;
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/discover/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
{ "path": "../data_view_field_editor/tsconfig.json"},
{ "path": "../field_formats/tsconfig.json" },
{ "path": "../data_views/tsconfig.json" },
{ "path": "../../../x-pack/plugins/spaces/tsconfig.json" }
{ "path": "../../../x-pack/plugins/spaces/tsconfig.json" },
{ "path": "../data_view_editor/tsconfig.json" }
]
}
Loading