From fddc1952cb65d4edad3498eea675ff7a9bb01d96 Mon Sep 17 00:00:00 2001 From: Corey Robertson Date: Tue, 5 May 2020 18:31:53 -0400 Subject: [PATCH 1/7] wip --- .../canvas/public/services/nav_link.ts | 28 ++++++++++++++++++ x-pack/plugins/canvas/public/application.tsx | 5 ++-- .../canvas/public/components/app/index.js | 6 ++-- x-pack/plugins/canvas/public/plugin.tsx | 13 ++++++++- .../plugins/canvas/public/services/index.ts | 29 +++++++++++++++---- 5 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 x-pack/legacy/plugins/canvas/public/services/nav_link.ts diff --git a/x-pack/legacy/plugins/canvas/public/services/nav_link.ts b/x-pack/legacy/plugins/canvas/public/services/nav_link.ts new file mode 100644 index 0000000000000..7c11679d1baf8 --- /dev/null +++ b/x-pack/legacy/plugins/canvas/public/services/nav_link.ts @@ -0,0 +1,28 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { CanvasServiceFactory } from '.'; +import { CoreStart, CoreSetup, CanvasSetupDeps, CanvasStartDeps } from '../plugin'; + +interface NavLinkService { + updatePath: (path: string) => void; +} + +export const navLinkServiceFactory: CanvasServiceFactory = ( + coreSetup, + coreStart, + setupPlugins, + startPlugins, + appUpdater +) => { + return { + updatePath: (path: string) => { + appUpdater.next(() => ({ + defaultPath: `#${path}`, + })); + }, + }; +}; diff --git a/x-pack/plugins/canvas/public/application.tsx b/x-pack/plugins/canvas/public/application.tsx index 284023e74d137..086b14b26405c 100644 --- a/x-pack/plugins/canvas/public/application.tsx +++ b/x-pack/plugins/canvas/public/application.tsx @@ -88,9 +88,10 @@ export const initializeCanvas = async ( coreStart: CoreStart, setupPlugins: CanvasSetupDeps, startPlugins: CanvasStartDeps, - registries: SetupRegistries + registries: SetupRegistries, + navUpdater: any ) => { - startServices(coreSetup, coreStart, setupPlugins, startPlugins); + startServices(coreSetup, coreStart, setupPlugins, startPlugins, navUpdater); // Create Store const canvasStore = await createStore(coreSetup, setupPlugins); diff --git a/x-pack/plugins/canvas/public/components/app/index.js b/x-pack/plugins/canvas/public/components/app/index.js index de0d4c190eae6..535624e5cdb62 100644 --- a/x-pack/plugins/canvas/public/components/app/index.js +++ b/x-pack/plugins/canvas/public/components/app/index.js @@ -8,6 +8,7 @@ import { connect } from 'react-redux'; import { compose, withProps } from 'recompose'; import { getAppReady, getBasePath } from '../../state/selectors/app'; import { appReady, appError } from '../../state/actions/app'; +import { withKibana } from '../../../../../../../src/plugins/kibana_react/public'; import { App as Component } from './app'; @@ -44,7 +45,8 @@ const mergeProps = (stateProps, dispatchProps, ownProps) => { export const App = compose( connect(mapStateToProps, mapDispatchToProps, mergeProps), - withProps(() => ({ - onRouteChange: () => undefined, + withKibana, + withProps(props => ({ + onRouteChange: props.kibana.services.canvas.navLink.updatePath, })) )(Component); diff --git a/x-pack/plugins/canvas/public/plugin.tsx b/x-pack/plugins/canvas/public/plugin.tsx index ba57d1475bc4f..9f95d97ea0060 100644 --- a/x-pack/plugins/canvas/public/plugin.tsx +++ b/x-pack/plugins/canvas/public/plugin.tsx @@ -4,11 +4,13 @@ * you may not use this file except in compliance with the Elastic License. */ +import { BehaviorSubject } from 'rxjs'; import { CoreSetup, CoreStart, Plugin, AppMountParameters, + AppUpdater, DEFAULT_APP_CATEGORIES, } from '../../../../src/core/public'; import { HomePublicPluginSetup } from '../../../../src/plugins/home/public'; @@ -60,6 +62,7 @@ export type CanvasStart = void; /** @internal */ export class CanvasPlugin implements Plugin { + private appUpdater = new BehaviorSubject(() => ({})); // TODO: Do we want to completely move canvas_plugin_src into it's own plugin? private srcPlugin = new CanvasSrcPlugin(); @@ -74,6 +77,7 @@ export class CanvasPlugin title: 'Canvas', euiIconType: 'canvasApp', order: 3000, + updater$: this.appUpdater, mount: async (params: AppMountParameters) => { // Load application bundle const { renderApp, initializeCanvas, teardownCanvas } = await import('./application'); @@ -81,7 +85,14 @@ export class CanvasPlugin // Get start services const [coreStart, depsStart] = await core.getStartServices(); - const canvasStore = await initializeCanvas(core, coreStart, plugins, depsStart, registries); + const canvasStore = await initializeCanvas( + core, + coreStart, + plugins, + depsStart, + registries, + this.appUpdater + ); const unmount = renderApp(coreStart, depsStart, params, canvasStore); diff --git a/x-pack/plugins/canvas/public/services/index.ts b/x-pack/plugins/canvas/public/services/index.ts index abc46beaa3e64..39d3f792a7061 100644 --- a/x-pack/plugins/canvas/public/services/index.ts +++ b/x-pack/plugins/canvas/public/services/index.ts @@ -8,12 +8,14 @@ import { CoreSetup, CoreStart } from '../../../../../src/core/public'; import { CanvasSetupDeps, CanvasStartDeps } from '../plugin'; import { notifyServiceFactory } from './notify'; import { platformServiceFactory } from './platform'; +import { navLinkServiceFactory } from './nav_link'; export type CanvasServiceFactory = ( coreSetup: CoreSetup, coreStart: CoreStart, canvasSetupPlugins: CanvasSetupDeps, - canvasStartPlugins: CanvasStartDeps + canvasStartPlugins: CanvasStartDeps, + appUpdater: any ) => Service; class CanvasServiceProvider { @@ -28,9 +30,16 @@ class CanvasServiceProvider { coreSetup: CoreSetup, coreStart: CoreStart, canvasSetupPlugins: CanvasSetupDeps, - canvasStartPlugins: CanvasStartDeps + canvasStartPlugins: CanvasStartDeps, + appUpdater: any ) { - this.service = this.factory(coreSetup, coreStart, canvasSetupPlugins, canvasStartPlugins); + this.service = this.factory( + coreSetup, + coreStart, + canvasSetupPlugins, + canvasStartPlugins, + appUpdater + ); } getService(): Service { @@ -51,20 +60,24 @@ export type ServiceFromProvider

= P extends CanvasServiceProvider ? export const services = { notify: new CanvasServiceProvider(notifyServiceFactory), platform: new CanvasServiceProvider(platformServiceFactory), + navLink: new CanvasServiceProvider(navLinkServiceFactory), }; export interface CanvasServices { notify: ServiceFromProvider; + platform: ServiceFromProvider; + navLink: ServiceFromProvider; } export const startServices = ( coreSetup: CoreSetup, coreStart: CoreStart, canvasSetupPlugins: CanvasSetupDeps, - canvasStartPlugins: CanvasStartDeps + canvasStartPlugins: CanvasStartDeps, + appUpdater: any ) => { Object.entries(services).forEach(([key, provider]) => - provider.start(coreSetup, coreStart, canvasSetupPlugins, canvasStartPlugins) + provider.start(coreSetup, coreStart, canvasSetupPlugins, canvasStartPlugins, appUpdater) ); }; @@ -72,4 +85,8 @@ export const stopServices = () => { Object.entries(services).forEach(([key, provider]) => provider.stop()); }; -export const { notify: notifyService, platform: platformService } = services; +export const { + notify: notifyService, + platform: platformService, + navLink: navLinkService, +} = services; From 5d461bf11fc74f20c6ffa93646d306fa17787bee Mon Sep 17 00:00:00 2001 From: Poff Poffenberger Date: Wed, 6 May 2020 17:00:14 -0500 Subject: [PATCH 2/7] Storing last page for canvas in session storage --- x-pack/legacy/plugins/canvas/public/services/nav_link.ts | 3 +++ x-pack/plugins/canvas/common/lib/constants.ts | 2 +- x-pack/plugins/canvas/public/plugin.tsx | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/x-pack/legacy/plugins/canvas/public/services/nav_link.ts b/x-pack/legacy/plugins/canvas/public/services/nav_link.ts index 7c11679d1baf8..ee0e5a3fdfc4c 100644 --- a/x-pack/legacy/plugins/canvas/public/services/nav_link.ts +++ b/x-pack/legacy/plugins/canvas/public/services/nav_link.ts @@ -6,6 +6,7 @@ import { CanvasServiceFactory } from '.'; import { CoreStart, CoreSetup, CanvasSetupDeps, CanvasStartDeps } from '../plugin'; +import { SESSIONSTORAGE_LASTPATH } from '../../common/lib/constants'; interface NavLinkService { updatePath: (path: string) => void; @@ -23,6 +24,8 @@ export const navLinkServiceFactory: CanvasServiceFactory = ( appUpdater.next(() => ({ defaultPath: `#${path}`, })); + + sessionStorage.setItem(SESSIONSTORAGE_LASTPATH, path); }, }; }; diff --git a/x-pack/plugins/canvas/common/lib/constants.ts b/x-pack/plugins/canvas/common/lib/constants.ts index a37dc3fd6a7b3..f2155d9202939 100644 --- a/x-pack/plugins/canvas/common/lib/constants.ts +++ b/x-pack/plugins/canvas/common/lib/constants.ts @@ -18,7 +18,7 @@ export const API_ROUTE_WORKPAD_STRUCTURES = `${API_ROUTE}/workpad-structures`; export const API_ROUTE_CUSTOM_ELEMENT = `${API_ROUTE}/custom-element`; export const LOCALSTORAGE_PREFIX = `kibana.canvas`; export const LOCALSTORAGE_CLIPBOARD = `${LOCALSTORAGE_PREFIX}.clipboard`; -export const LOCALSTORAGE_LASTPAGE = 'canvas:lastpage'; +export const SESSIONSTORAGE_LASTPATH = 'lastPath:canvas'; export const FETCH_TIMEOUT = 30000; // 30 seconds export const CANVAS_USAGE_TYPE = 'canvas'; export const DEFAULT_WORKPAD_CSS = '.canvasPage {\n\n}'; diff --git a/x-pack/plugins/canvas/public/plugin.tsx b/x-pack/plugins/canvas/public/plugin.tsx index 9f95d97ea0060..c51d1e349c4b4 100644 --- a/x-pack/plugins/canvas/public/plugin.tsx +++ b/x-pack/plugins/canvas/public/plugin.tsx @@ -15,6 +15,7 @@ import { } from '../../../../src/core/public'; import { HomePublicPluginSetup } from '../../../../src/plugins/home/public'; import { initLoadingIndicator } from './lib/loading_indicator'; +import { SESSIONSTORAGE_LASTPATH } from '../common/lib/constants'; import { featureCatalogueEntry } from './feature_catalogue_entry'; import { ExpressionsSetup, ExpressionsStart } from '../../../../src/plugins/expressions/public'; import { DataPublicPluginSetup } from '../../../../src/plugins/data/public'; @@ -71,6 +72,14 @@ export class CanvasPlugin this.srcPlugin.setup(core, { canvas: canvasApi }); + // Set the nav link to the last saved url if we have one in storage + const lastUrl = sessionStorage.getItem(SESSIONSTORAGE_LASTPATH); + if (lastUrl) { + this.appUpdater.next(() => ({ + defaultPath: `#${lastUrl}`, + })); + } + core.application.register({ category: DEFAULT_APP_CATEGORIES.kibana, id: 'canvas', From 0442e2d54569488403edfeb95d83baa14fb02c85 Mon Sep 17 00:00:00 2001 From: Poff Poffenberger Date: Wed, 6 May 2020 17:18:56 -0500 Subject: [PATCH 3/7] Fix bad path --- x-pack/{legacy => }/plugins/canvas/public/services/nav_link.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename x-pack/{legacy => }/plugins/canvas/public/services/nav_link.ts (100%) diff --git a/x-pack/legacy/plugins/canvas/public/services/nav_link.ts b/x-pack/plugins/canvas/public/services/nav_link.ts similarity index 100% rename from x-pack/legacy/plugins/canvas/public/services/nav_link.ts rename to x-pack/plugins/canvas/public/services/nav_link.ts From 23b4e682be52e67a7acffd7352893744cbb43b1a Mon Sep 17 00:00:00 2001 From: Poff Poffenberger Date: Wed, 6 May 2020 17:25:49 -0500 Subject: [PATCH 4/7] Fix bad merge --- x-pack/plugins/canvas/public/components/app/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/canvas/public/components/app/index.js b/x-pack/plugins/canvas/public/components/app/index.js index 535624e5cdb62..750132dadb97d 100644 --- a/x-pack/plugins/canvas/public/components/app/index.js +++ b/x-pack/plugins/canvas/public/components/app/index.js @@ -8,7 +8,7 @@ import { connect } from 'react-redux'; import { compose, withProps } from 'recompose'; import { getAppReady, getBasePath } from '../../state/selectors/app'; import { appReady, appError } from '../../state/actions/app'; -import { withKibana } from '../../../../../../../src/plugins/kibana_react/public'; +import { withKibana } from '../../../../../../src/plugins/kibana_react/public'; import { App as Component } from './app'; From 18c82f2c3a1ea787783afcc0a796e92aaa96623c Mon Sep 17 00:00:00 2001 From: Poff Poffenberger Date: Wed, 6 May 2020 18:37:09 -0500 Subject: [PATCH 5/7] Cleanup and adding some types --- x-pack/plugins/canvas/public/application.tsx | 5 +++-- x-pack/plugins/canvas/public/services/index.ts | 7 ++++--- x-pack/plugins/canvas/public/services/nav_link.ts | 1 - 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/canvas/public/application.tsx b/x-pack/plugins/canvas/public/application.tsx index 086b14b26405c..0ec85f9c31f19 100644 --- a/x-pack/plugins/canvas/public/application.tsx +++ b/x-pack/plugins/canvas/public/application.tsx @@ -10,8 +10,9 @@ import ReactDOM from 'react-dom'; import { I18nProvider } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; import { Provider } from 'react-redux'; +import { Observable } from 'rxjs'; -import { AppMountParameters, CoreStart, CoreSetup } from 'kibana/public'; +import { AppMountParameters, CoreStart, CoreSetup, AppUpdater } from 'kibana/public'; import { CanvasStartDeps, CanvasSetupDeps } from './plugin'; // @ts-ignore Untyped local @@ -89,7 +90,7 @@ export const initializeCanvas = async ( setupPlugins: CanvasSetupDeps, startPlugins: CanvasStartDeps, registries: SetupRegistries, - navUpdater: any + navUpdater: Observable ) => { startServices(coreSetup, coreStart, setupPlugins, startPlugins, navUpdater); diff --git a/x-pack/plugins/canvas/public/services/index.ts b/x-pack/plugins/canvas/public/services/index.ts index 39d3f792a7061..85517f457a963 100644 --- a/x-pack/plugins/canvas/public/services/index.ts +++ b/x-pack/plugins/canvas/public/services/index.ts @@ -4,7 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -import { CoreSetup, CoreStart } from '../../../../../src/core/public'; +import { Observable } from 'rxjs'; +import { CoreSetup, CoreStart, AppUpdater } from '../../../../../src/core/public'; import { CanvasSetupDeps, CanvasStartDeps } from '../plugin'; import { notifyServiceFactory } from './notify'; import { platformServiceFactory } from './platform'; @@ -15,7 +16,7 @@ export type CanvasServiceFactory = ( coreStart: CoreStart, canvasSetupPlugins: CanvasSetupDeps, canvasStartPlugins: CanvasStartDeps, - appUpdater: any + appUpdater: Observable ) => Service; class CanvasServiceProvider { @@ -31,7 +32,7 @@ class CanvasServiceProvider { coreStart: CoreStart, canvasSetupPlugins: CanvasSetupDeps, canvasStartPlugins: CanvasStartDeps, - appUpdater: any + appUpdater: Observable ) { this.service = this.factory( coreSetup, diff --git a/x-pack/plugins/canvas/public/services/nav_link.ts b/x-pack/plugins/canvas/public/services/nav_link.ts index ee0e5a3fdfc4c..c1736bcc9748c 100644 --- a/x-pack/plugins/canvas/public/services/nav_link.ts +++ b/x-pack/plugins/canvas/public/services/nav_link.ts @@ -5,7 +5,6 @@ */ import { CanvasServiceFactory } from '.'; -import { CoreStart, CoreSetup, CanvasSetupDeps, CanvasStartDeps } from '../plugin'; import { SESSIONSTORAGE_LASTPATH } from '../../common/lib/constants'; interface NavLinkService { From e711ffa66486930498f728051349a324fff1faa0 Mon Sep 17 00:00:00 2001 From: Poff Poffenberger Date: Thu, 7 May 2020 10:06:42 -0500 Subject: [PATCH 6/7] Fixing types --- x-pack/plugins/canvas/public/application.tsx | 4 ++-- x-pack/plugins/canvas/public/services/index.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/x-pack/plugins/canvas/public/application.tsx b/x-pack/plugins/canvas/public/application.tsx index 0ec85f9c31f19..a69a85cf0b041 100644 --- a/x-pack/plugins/canvas/public/application.tsx +++ b/x-pack/plugins/canvas/public/application.tsx @@ -10,7 +10,7 @@ import ReactDOM from 'react-dom'; import { I18nProvider } from '@kbn/i18n/react'; import { i18n } from '@kbn/i18n'; import { Provider } from 'react-redux'; -import { Observable } from 'rxjs'; +import { BehaviorSubject } from 'rxjs'; import { AppMountParameters, CoreStart, CoreSetup, AppUpdater } from 'kibana/public'; @@ -90,7 +90,7 @@ export const initializeCanvas = async ( setupPlugins: CanvasSetupDeps, startPlugins: CanvasStartDeps, registries: SetupRegistries, - navUpdater: Observable + navUpdater: BehaviorSubject ) => { startServices(coreSetup, coreStart, setupPlugins, startPlugins, navUpdater); diff --git a/x-pack/plugins/canvas/public/services/index.ts b/x-pack/plugins/canvas/public/services/index.ts index 85517f457a963..83ec12f126b0c 100644 --- a/x-pack/plugins/canvas/public/services/index.ts +++ b/x-pack/plugins/canvas/public/services/index.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Observable } from 'rxjs'; +import { BehaviorSubject } from 'rxjs'; import { CoreSetup, CoreStart, AppUpdater } from '../../../../../src/core/public'; import { CanvasSetupDeps, CanvasStartDeps } from '../plugin'; import { notifyServiceFactory } from './notify'; @@ -16,7 +16,7 @@ export type CanvasServiceFactory = ( coreStart: CoreStart, canvasSetupPlugins: CanvasSetupDeps, canvasStartPlugins: CanvasStartDeps, - appUpdater: Observable + appUpdater: BehaviorSubject ) => Service; class CanvasServiceProvider { @@ -32,7 +32,7 @@ class CanvasServiceProvider { coreStart: CoreStart, canvasSetupPlugins: CanvasSetupDeps, canvasStartPlugins: CanvasStartDeps, - appUpdater: Observable + appUpdater: BehaviorSubject ) { this.service = this.factory( coreSetup, From 9ea3762c90c73cbbe8c5a59b0a0b6ed724e594dc Mon Sep 17 00:00:00 2001 From: Poff Poffenberger Date: Thu, 7 May 2020 12:42:45 -0500 Subject: [PATCH 7/7] PR feedback and storage refactor --- x-pack/plugins/canvas/public/application.tsx | 4 +-- x-pack/plugins/canvas/public/lib/clipboard.ts | 17 ++------- .../plugins/canvas/public/lib/get_window.ts | 12 +++++-- x-pack/plugins/canvas/public/lib/storage.ts | 35 +++++++++++++++++++ x-pack/plugins/canvas/public/plugin.tsx | 3 +- .../plugins/canvas/public/services/index.ts | 2 +- .../canvas/public/services/nav_link.ts | 3 +- 7 files changed, 55 insertions(+), 21 deletions(-) create mode 100644 x-pack/plugins/canvas/public/lib/storage.ts diff --git a/x-pack/plugins/canvas/public/application.tsx b/x-pack/plugins/canvas/public/application.tsx index a69a85cf0b041..9c2aa821be2d5 100644 --- a/x-pack/plugins/canvas/public/application.tsx +++ b/x-pack/plugins/canvas/public/application.tsx @@ -90,9 +90,9 @@ export const initializeCanvas = async ( setupPlugins: CanvasSetupDeps, startPlugins: CanvasStartDeps, registries: SetupRegistries, - navUpdater: BehaviorSubject + appUpdater: BehaviorSubject ) => { - startServices(coreSetup, coreStart, setupPlugins, startPlugins, navUpdater); + startServices(coreSetup, coreStart, setupPlugins, startPlugins, appUpdater); // Create Store const canvasStore = await createStore(coreSetup, setupPlugins); diff --git a/x-pack/plugins/canvas/public/lib/clipboard.ts b/x-pack/plugins/canvas/public/lib/clipboard.ts index 11755807aa533..cb940fd064a47 100644 --- a/x-pack/plugins/canvas/public/lib/clipboard.ts +++ b/x-pack/plugins/canvas/public/lib/clipboard.ts @@ -4,22 +4,11 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Storage } from '../../../../../src/plugins/kibana_utils/public'; import { LOCALSTORAGE_CLIPBOARD } from '../../common/lib/constants'; -import { getWindow } from './get_window'; - -let storage: Storage; - -const getStorage = (): Storage => { - if (!storage) { - storage = new Storage(getWindow().localStorage); - } - - return storage; -}; +import { getLocalStorage } from './storage'; export const setClipboardData = (data: any) => { - getStorage().set(LOCALSTORAGE_CLIPBOARD, JSON.stringify(data)); + getLocalStorage().set(LOCALSTORAGE_CLIPBOARD, JSON.stringify(data)); }; -export const getClipboardData = () => getStorage().get(LOCALSTORAGE_CLIPBOARD); +export const getClipboardData = () => getLocalStorage().get(LOCALSTORAGE_CLIPBOARD); diff --git a/x-pack/plugins/canvas/public/lib/get_window.ts b/x-pack/plugins/canvas/public/lib/get_window.ts index 42c632f4a514f..c8fb035d4d33f 100644 --- a/x-pack/plugins/canvas/public/lib/get_window.ts +++ b/x-pack/plugins/canvas/public/lib/get_window.ts @@ -5,10 +5,18 @@ */ // return window if it exists, otherwise just return an object literal -const windowObj = { location: null, localStorage: {} as Window['localStorage'] }; +const windowObj = { + location: null, + localStorage: {} as Window['localStorage'], + sessionStorage: {} as Window['sessionStorage'], +}; export const getWindow = (): | Window - | { location: Location | null; localStorage: Window['localStorage'] } => { + | { + location: Location | null; + localStorage: Window['localStorage']; + sessionStorage: Window['sessionStorage']; + } => { return typeof window === 'undefined' ? windowObj : window; }; diff --git a/x-pack/plugins/canvas/public/lib/storage.ts b/x-pack/plugins/canvas/public/lib/storage.ts new file mode 100644 index 0000000000000..47c8cc741eaf3 --- /dev/null +++ b/x-pack/plugins/canvas/public/lib/storage.ts @@ -0,0 +1,35 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { Storage } from '../../../../../src/plugins/kibana_utils/public'; +import { getWindow } from './get_window'; + +export enum StorageType { + Local = 'localStorage', + Session = 'sessionStorage', +} + +const storages: { + [x in StorageType]: Storage | null; +} = { + [StorageType.Local]: null, + [StorageType.Session]: null, +}; + +const getStorage = (type: StorageType): Storage => { + const storage = storages[type] || new Storage(getWindow()[type]); + storages[type] = storage; + + return storage; +}; + +export const getLocalStorage = (): Storage => { + return getStorage(StorageType.Local); +}; + +export const getSessionStorage = (): Storage => { + return getStorage(StorageType.Session); +}; diff --git a/x-pack/plugins/canvas/public/plugin.tsx b/x-pack/plugins/canvas/public/plugin.tsx index c51d1e349c4b4..c2192818e528b 100644 --- a/x-pack/plugins/canvas/public/plugin.tsx +++ b/x-pack/plugins/canvas/public/plugin.tsx @@ -15,6 +15,7 @@ import { } from '../../../../src/core/public'; import { HomePublicPluginSetup } from '../../../../src/plugins/home/public'; import { initLoadingIndicator } from './lib/loading_indicator'; +import { getSessionStorage } from './lib/storage'; import { SESSIONSTORAGE_LASTPATH } from '../common/lib/constants'; import { featureCatalogueEntry } from './feature_catalogue_entry'; import { ExpressionsSetup, ExpressionsStart } from '../../../../src/plugins/expressions/public'; @@ -73,7 +74,7 @@ export class CanvasPlugin this.srcPlugin.setup(core, { canvas: canvasApi }); // Set the nav link to the last saved url if we have one in storage - const lastUrl = sessionStorage.getItem(SESSIONSTORAGE_LASTPATH); + const lastUrl = getSessionStorage().get(SESSIONSTORAGE_LASTPATH); if (lastUrl) { this.appUpdater.next(() => ({ defaultPath: `#${lastUrl}`, diff --git a/x-pack/plugins/canvas/public/services/index.ts b/x-pack/plugins/canvas/public/services/index.ts index 83ec12f126b0c..42176f953c331 100644 --- a/x-pack/plugins/canvas/public/services/index.ts +++ b/x-pack/plugins/canvas/public/services/index.ts @@ -75,7 +75,7 @@ export const startServices = ( coreStart: CoreStart, canvasSetupPlugins: CanvasSetupDeps, canvasStartPlugins: CanvasStartDeps, - appUpdater: any + appUpdater: BehaviorSubject ) => { Object.entries(services).forEach(([key, provider]) => provider.start(coreSetup, coreStart, canvasSetupPlugins, canvasStartPlugins, appUpdater) diff --git a/x-pack/plugins/canvas/public/services/nav_link.ts b/x-pack/plugins/canvas/public/services/nav_link.ts index c1736bcc9748c..5061498458006 100644 --- a/x-pack/plugins/canvas/public/services/nav_link.ts +++ b/x-pack/plugins/canvas/public/services/nav_link.ts @@ -6,6 +6,7 @@ import { CanvasServiceFactory } from '.'; import { SESSIONSTORAGE_LASTPATH } from '../../common/lib/constants'; +import { getSessionStorage } from '../lib/storage'; interface NavLinkService { updatePath: (path: string) => void; @@ -24,7 +25,7 @@ export const navLinkServiceFactory: CanvasServiceFactory = ( defaultPath: `#${path}`, })); - sessionStorage.setItem(SESSIONSTORAGE_LASTPATH, path); + getSessionStorage().set(SESSIONSTORAGE_LASTPATH, path); }, }; };