Skip to content

Commit

Permalink
[Discover] Make use of stateContainer action onDataViewEdited (elasti…
Browse files Browse the repository at this point in the history
…c#199588)

Duplicating code that's triggered when a dataview is being edited

(cherry picked from commit 8136bcc)
  • Loading branch information
kertal committed Nov 11, 2024
1 parent 54c8329 commit cb1e5b2
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 cb1e5b2

Please sign in to comment.