Skip to content

Commit 35f1f24

Browse files
authored
[Discover] Persist query mode to local storage (elastic#250388)
## Summary Closes elastic#200765 When changing between classic and ES|QL mode we persist it in local storage so the next default session uses that preference. --- Before (Example with ESQL) https://github.com/user-attachments/assets/a689c0f9-ff69-4bcb-a8da-108d4693cb56 After (Example with ESQL) https://github.com/user-attachments/assets/bebc03db-a860-4ec9-9843-5e1f0e53f585 ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) - [x] Review the [backport guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing) and apply applicable `backport:*` labels.
1 parent 8fd2beb commit 35f1f24

20 files changed

Lines changed: 446 additions & 4 deletions

File tree

.buildkite/ftr_platform_stateful_configs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ enabled:
9191
- src/platform/test/functional/apps/discover/group10/config.ts
9292
- src/platform/test/functional/apps/discover/context_awareness/config.ts
9393
- src/platform/test/functional/apps/discover/observability/config.ts
94+
- src/platform/test/functional/apps/discover/query_mode/config.ts
9495
- src/platform/test/functional/apps/discover/tabs/config.ts
9596
- src/platform/test/functional/apps/discover/tabs2/config.ts
9697
- src/platform/test/functional/apps/discover/tabs3/config.ts

src/platform/plugins/shared/discover/common/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export const getDefaultRowsPerPage = (uiSettings: IUiSettingsClient): number =>
2626
// local storage key for the ES|QL to Dataviews transition modal
2727
export const ESQL_TRANSITION_MODAL_KEY = 'data.textLangTransitionModal';
2828

29+
// local storage key for the query mode when starting a new discover session
30+
export const DISCOVER_QUERY_MODE_KEY = 'discover.defaultQueryMode';
31+
2932
/**
3033
* The id value used to indicate that a link should open in a new Discover tab.
3134
* It will be used in the `_tab` URL param to indicate that a new tab should be created.

src/platform/plugins/shared/discover/public/application/main/state_management/redux/actions/initialize_single_tab.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export const initializeSingleTab = createInternalStateAsyncThunk(
232232
// then get an updated copy of the saved search with the applied initial state
233233
const initialAppState = getInitialAppState({
234234
initialUrlState: urlAppState,
235+
hasGlobalState: Object.keys(urlGlobalState || {}).length > 0,
235236
persistedTab,
236237
dataView,
237238
services,

src/platform/plugins/shared/discover/public/application/main/state_management/redux/actions/tab_state.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import {
2626
internalStateSlice,
2727
type InternalStateThunkActionCreator,
2828
type TabActionPayload,
29+
transitionedFromEsqlToDataView,
30+
transitionedFromDataViewToEsql,
2931
} from '../internal_state';
3032
import { selectTab } from '../selectors';
3133
import { selectTabRuntimeState } from '../runtime_state';
@@ -206,6 +208,8 @@ export const transitionFromESQLToDataView: InternalStateThunkActionCreator<
206208
},
207209
})
208210
);
211+
212+
dispatch(transitionedFromEsqlToDataView({ tabId }));
209213
};
210214

211215
const clearTimeFieldFromSort = (
@@ -251,6 +255,8 @@ export const transitionFromDataViewToESQL: InternalStateThunkActionCreator<
251255

252256
// clears pinned filters
253257
dispatch(updateGlobalState({ tabId, globalState: { filters: [] } }));
258+
259+
dispatch(transitionedFromDataViewToEsql({ tabId }));
254260
};
255261

256262
/**

src/platform/plugins/shared/discover/public/application/main/state_management/redux/internal_state.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import type { IKbnUrlStateStorage } from '@kbn/kibana-utils-plugin/public';
2929
import type { ESQLControlVariable } from '@kbn/esql-types';
3030
import type { DiscoverSession } from '@kbn/saved-search-plugin/common';
3131
import { isOfAggregateQueryType } from '@kbn/es-query';
32+
import { DISCOVER_QUERY_MODE_KEY } from '../../../../../common/constants';
3233
import type { DiscoverCustomizationContext } from '../../../../customizations';
3334
import type { DiscoverServices } from '../../../../build_services';
3435
import { type RuntimeStateManager, selectTabRuntimeInternalState } from './runtime_state';
@@ -446,6 +447,14 @@ export const syncLocallyPersistedTabState = createAction<TabActionPayload>(
446447

447448
export const discardFlyoutsOnTabChange = createAction('internalState/discardFlyoutsOnTabChange');
448449

450+
export const transitionedFromEsqlToDataView = createAction<TabActionPayload>(
451+
'internalState/transitionedFromEsqlToDataView'
452+
);
453+
454+
export const transitionedFromDataViewToEsql = createAction<TabActionPayload>(
455+
'internalState/transitionedFromDataViewToEsql'
456+
);
457+
449458
type InternalStateListenerEffect<
450459
TActionCreator extends PayloadActionCreator<TPayload>,
451460
TPayload = TActionCreator extends PayloadActionCreator<infer T> ? T : never
@@ -510,6 +519,28 @@ const createMiddleware = (options: InternalStateDependencies) => {
510519
},
511520
});
512521

522+
// This pair of listeners updates the default query mode based on the last used query type (ES|QL vs Data View), we use
523+
// this so new discover sessions use that query mode as a default.
524+
//
525+
// NOTE: In the short term we will add a feature flag to default to ES|QL when there is no existing preference saved.
526+
// Right now we use classic - this means that users will have to switch to ES|QL manually the first time if they already
527+
// had classic stored as their last used mode.
528+
startListening({
529+
actionCreator: transitionedFromDataViewToEsql,
530+
effect: (action, listenerApi) => {
531+
const { services } = listenerApi.extra;
532+
services.storage.set(DISCOVER_QUERY_MODE_KEY, 'esql');
533+
},
534+
});
535+
536+
startListening({
537+
actionCreator: transitionedFromEsqlToDataView,
538+
effect: (action, listenerApi) => {
539+
const { services } = listenerApi.extra;
540+
services.storage.set(DISCOVER_QUERY_MODE_KEY, 'classic');
541+
},
542+
});
543+
513544
return listenerMiddleware.middleware;
514545
};
515546

0 commit comments

Comments
 (0)