Skip to content

Commit

Permalink
[8.x] [Discover] Make use of stateContainer action onDataViewEdited (e…
Browse files Browse the repository at this point in the history
…lastic#199588) (elastic#199641)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Discover] Make use of stateContainer action onDataViewEdited
(elastic#199588)](elastic#199588)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Matthias
Wilhelm","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-11T13:00:08Z","message":"[Discover]
Make use of stateContainer action onDataViewEdited
(elastic#199588)\n\nDuplicating code that's triggered when a dataview is being
edited","sha":"8136bcc0e43a682dfa4484f112a287210936f817","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Discover","release_note:skip","v9.0.0","Team:DataDiscovery","backport:prev-minor"],"title":"[Discover]
Make use of stateContainer action
onDataViewEdited","number":199588,"url":"https://github.com/elastic/kibana/pull/199588","mergeCommit":{"message":"[Discover]
Make use of stateContainer action onDataViewEdited
(elastic#199588)\n\nDuplicating code that's triggered when a dataview is being
edited","sha":"8136bcc0e43a682dfa4484f112a287210936f817"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/199588","number":199588,"mergeCommit":{"message":"[Discover]
Make use of stateContainer action onDataViewEdited
(elastic#199588)\n\nDuplicating code that's triggered when a dataview is being
edited","sha":"8136bcc0e43a682dfa4484f112a287210936f817"}}]}]
BACKPORT-->

Co-authored-by: Matthias Wilhelm <[email protected]>
  • Loading branch information
kibanamachine and kertal authored Nov 11, 2024
1 parent 3b5e981 commit 8dfe8f5
Showing 1 changed file with 7 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/

import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import { type DataView, DataViewType } from '@kbn/data-views-plugin/public';
import { DataViewPickerProps } from '@kbn/unified-search-plugin/public';
import { DataViewType } from '@kbn/data-views-plugin/public';
import type { DataViewPickerProps } from '@kbn/unified-search-plugin/public';
import { ENABLE_ESQL } from '@kbn/esql-utils';
import { TextBasedLanguages } from '@kbn/esql-utils';
import { DiscoverFlyouts, dismissAllFlyoutsExceptFor } from '@kbn/discover-utils';
Expand All @@ -20,7 +20,6 @@ import { useDiscoverServices } from '../../../../hooks/use_discover_services';
import type { DiscoverStateContainer } from '../../state_management/discover_state';
import { onSaveSearch } from './on_save_search';
import { useDiscoverCustomization } from '../../../../customizations';
import { addLog } from '../../../../utils/add_log';
import { useAppStateSelector } from '../../state_management/discover_app_state_container';
import { useDiscoverTopNav } from './use_discover_topnav';
import { useIsEsqlMode } from '../../hooks/use_is_esql_mode';
Expand All @@ -47,15 +46,8 @@ export const DiscoverTopNav = ({
onCancelClick,
}: DiscoverTopNavProps) => {
const services = useDiscoverServices();
const {
dataViewEditor,
navigation,
dataViewFieldEditor,
data,
uiSettings,
dataViews,
setHeaderActionMenu,
} = services;
const { dataViewEditor, navigation, dataViewFieldEditor, data, uiSettings, setHeaderActionMenu } =
services;
const query = useAppStateSelector((state) => state.query);
const adHocDataViews = useInternalStateSelector((state) => state.adHocDataViews);
const dataView = useInternalStateSelector((state) => state.dataView!);
Expand Down Expand Up @@ -93,7 +85,7 @@ export const DiscoverTopNav = ({
const editField = useMemo(
() =>
canEditDataView
? async (fieldName?: string, uiAction: 'edit' | 'add' = 'edit') => {
? async (fieldName?: string) => {
if (dataView?.id) {
const dataViewInstance = await data.dataViews.get(dataView.id);
closeFieldEditor.current = await dataViewFieldEditor.openEditor({
Expand All @@ -112,7 +104,7 @@ export const DiscoverTopNav = ({
);

const addField = useMemo(
() => (canEditDataView && editField ? () => editField(undefined, 'add') : undefined),
() => (canEditDataView && editField ? () => editField() : undefined),
[editField, canEditDataView]
);

Expand All @@ -123,23 +115,6 @@ export const DiscoverTopNav = ({
});
}, [dataViewEditor, stateContainer]);

const onEditDataView = useCallback(
async (editedDataView: DataView) => {
if (editedDataView.isPersisted()) {
// Clear the current data view from the cache and create a new instance
// of it, ensuring we have a new object reference to trigger a re-render
dataViews.clearInstanceCache(editedDataView.id);
stateContainer.actions.setDataView(await dataViews.create(editedDataView.toSpec(), true));
} else {
await stateContainer.actions.updateAdHocDataViewId();
}
stateContainer.actions.loadDataViewList();
addLog('[DiscoverTopNav] onEditDataView triggers data fetching');
stateContainer.dataState.fetch();
},
[dataViews, stateContainer.actions, stateContainer.dataState]
);

const updateSavedQueryId = (newSavedQueryId: string | undefined) => {
const { appState } = stateContainer;
if (newSavedQueryId) {
Expand Down Expand Up @@ -223,14 +198,13 @@ export const DiscoverTopNav = ({
textBasedLanguages: supportedTextBasedLanguages,
adHocDataViews,
savedDataViews,
onEditDataView,
onEditDataView: stateContainer.actions.onDataViewEdited,
};
}, [
adHocDataViews,
addField,
createNewDataView,
dataView,
onEditDataView,
savedDataViews,
stateContainer,
uiSettings,
Expand Down

0 comments on commit 8dfe8f5

Please sign in to comment.