Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discover] Make use of stateContainer action onDataViewEdited #199588

Merged
merged 3 commits into from
Nov 11, 2024
Merged
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
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') => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a little cleanup since the second param is not in use

? 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]
);
Comment on lines -126 to -141
Copy link
Member Author

@kertal kertal Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed for code-deduplication, because this function is provided by our state container, but it seems it never has been used. so we're switching to make use of this part of the code:

const onDataViewEdited = async (editedDataView: DataView) => {

This has another advantage, there's unit test code coverage for the state container function


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