diff --git a/x-pack/plugins/lens/public/app_plugin/app.tsx b/x-pack/plugins/lens/public/app_plugin/app.tsx index 99f933361d40a..283d29e125683 100644 --- a/x-pack/plugins/lens/public/app_plugin/app.tsx +++ b/x-pack/plugins/lens/public/app_plugin/app.tsx @@ -10,8 +10,10 @@ import './app.scss'; import React, { useState, useEffect, useCallback, useMemo } from 'react'; import { i18n } from '@kbn/i18n'; import { EuiBreadcrumb } from '@elastic/eui'; +import useLocalStorage from 'react-use/lib/useLocalStorage'; import { createKbnUrlStateStorage, + Storage, withNotifyOnErrors, } from '../../../../../src/plugins/kibana_utils/public'; import { useKibana } from '../../../../../src/plugins/kibana_react/public'; @@ -38,6 +40,7 @@ import { LensInspector } from '../lens_inspector_service'; import { getEditPath } from '../../common'; import { isLensEqual } from './lens_document_equality'; import { disableAutoApply } from '../state_management/lens_slice'; +import { readFromStorage, writeToStorage } from '../settings_storage'; export type SaveProps = Omit & { returnToOrigin: boolean; @@ -296,10 +299,14 @@ export function App({ ); const autoApplyEnabled = !Boolean(appliedState); - const toggleAutoApply = useCallback( - () => dispatch(autoApplyEnabled ? disableAutoApply() : enableAutoApply()), - [dispatch, autoApplyEnabled] - ); + const toggleAutoApply = useCallback(() => { + writeToStorage( + new Storage(localStorage), + 'autoApplyDisabled', + String(typeof appliedState === 'undefined') + ); + dispatch(autoApplyEnabled ? disableAutoApply() : enableAutoApply()); + }, [dispatch, appliedState, autoApplyEnabled]); return ( <> diff --git a/x-pack/plugins/lens/public/settings_storage.tsx b/x-pack/plugins/lens/public/settings_storage.tsx index fa59bff166c30..ebe812915242e 100644 --- a/x-pack/plugins/lens/public/settings_storage.tsx +++ b/x-pack/plugins/lens/public/settings_storage.tsx @@ -14,5 +14,5 @@ export const readFromStorage = (storage: IStorageWrapper, key: string) => { return data && data[key]; }; export const writeToStorage = (storage: IStorageWrapper, key: string, value: string) => { - storage.set(STORAGE_KEY, { [key]: value }); + storage.set(STORAGE_KEY, { ...storage.get(STORAGE_KEY), [key]: value }); }; diff --git a/x-pack/plugins/lens/public/state_management/init_middleware/load_initial.ts b/x-pack/plugins/lens/public/state_management/init_middleware/load_initial.ts index 372d08017ee2a..f649fef25d718 100644 --- a/x-pack/plugins/lens/public/state_management/init_middleware/load_initial.ts +++ b/x-pack/plugins/lens/public/state_management/init_middleware/load_initial.ts @@ -9,7 +9,7 @@ import { MiddlewareAPI } from '@reduxjs/toolkit'; import { i18n } from '@kbn/i18n'; import { History } from 'history'; import { setState, initEmpty, LensStoreDeps } from '..'; -import { getPreloadedState } from '../lens_slice'; +import { disableAutoApply, getPreloadedState } from '../lens_slice'; import { SharingSavedObjectProps } from '../../types'; import { LensEmbeddableInput, LensByReferenceInput } from '../../embeddable/embeddable'; import { getInitialDatasourceId } from '../../utils'; @@ -17,6 +17,8 @@ import { initializeDatasources } from '../../editor_frame_service/editor_frame'; import { LensAppServices } from '../../app_plugin/types'; import { getEditPath, getFullPath, LENS_EMBEDDABLE_TYPE } from '../../../common/constants'; import { Document } from '../../persistence'; +import { readFromStorage } from '../../settings_storage'; +import { Storage } from '../../../../../../src/plugins/kibana_utils/public'; export const getPersisted = async ({ initialInput, @@ -209,6 +211,12 @@ export function loadInitial( isLoading: false, }) ); + + const autoApplyDisabled = + readFromStorage(new Storage(localStorage), 'autoApplyDisabled') === 'true'; + if (autoApplyDisabled) { + store.dispatch(disableAutoApply()); + } }) .catch((e: { message: string }) => notifications.toasts.addDanger({