Skip to content

Commit

Permalink
Make Dashboard Unsaved Changes Space Specific (elastic#92680)
Browse files Browse the repository at this point in the history
* Made dashboard unsaved changes space specific
# Conflicts:
#	api_docs/dashboard.json
  • Loading branch information
ThomThomson committed Mar 4, 2021
1 parent b73456d commit 5f48de9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 27 deletions.
36 changes: 18 additions & 18 deletions api_docs/dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -1389,15 +1389,15 @@
"description": [],
"source": {
"path": "src/plugins/dashboard/public/plugin.tsx",
"lineNumber": 89,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L89"
"lineNumber": 90,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L90"
}
}
],
"source": {
"path": "src/plugins/dashboard/public/plugin.tsx",
"lineNumber": 88,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L88"
"lineNumber": 89,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L89"
},
"initialIsOpen": false
},
Expand Down Expand Up @@ -1939,8 +1939,8 @@
"description": [],
"source": {
"path": "src/plugins/dashboard/public/plugin.tsx",
"lineNumber": 86,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L86"
"lineNumber": 87,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L87"
},
"signature": [
"UrlGeneratorContract<\"DASHBOARD_APP_URL_GENERATOR\">"
Expand Down Expand Up @@ -2103,8 +2103,8 @@
"description": [],
"source": {
"path": "src/plugins/dashboard/public/plugin.tsx",
"lineNumber": 118,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L118"
"lineNumber": 120,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L120"
},
"signature": [
"void"
Expand All @@ -2127,8 +2127,8 @@
"description": [],
"source": {
"path": "src/plugins/dashboard/public/plugin.tsx",
"lineNumber": 121,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L121"
"lineNumber": 123,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L123"
},
"signature": [
"() => ",
Expand All @@ -2149,8 +2149,8 @@
"description": [],
"source": {
"path": "src/plugins/dashboard/public/plugin.tsx",
"lineNumber": 122,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L122"
"lineNumber": 124,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L124"
},
"signature": [
{
Expand All @@ -2171,8 +2171,8 @@
"description": [],
"source": {
"path": "src/plugins/dashboard/public/plugin.tsx",
"lineNumber": 123,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L123"
"lineNumber": 125,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L125"
},
"signature": [
{
Expand All @@ -2192,8 +2192,8 @@
"description": [],
"source": {
"path": "src/plugins/dashboard/public/plugin.tsx",
"lineNumber": 124,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L124"
"lineNumber": 126,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L126"
},
"signature": [
"React.FC<Props>"
Expand All @@ -2202,8 +2202,8 @@
],
"source": {
"path": "src/plugins/dashboard/public/plugin.tsx",
"lineNumber": 120,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L120"
"lineNumber": 122,
"link": "https://github.com/elastic/kibana/tree/7.xsrc/plugins/dashboard/public/plugin.tsx#L122"
},
"lifecycle": "start",
"initialIsOpen": true
Expand Down
1 change: 1 addition & 0 deletions src/plugins/dashboard/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
],
"optionalPlugins": [
"home",
"spacesOss",
"savedObjectsTaggingOss",
"usageCollection"],
"server": true,
Expand Down
10 changes: 9 additions & 1 deletion src/plugins/dashboard/public/application/dashboard_router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { parse, ParsedQuery } from 'query-string';
import { render, unmountComponentAtNode } from 'react-dom';
import { Switch, Route, RouteComponentProps, HashRouter, Redirect } from 'react-router-dom';

import { first } from 'rxjs/operators';
import { DashboardListing } from './listing';
import { DashboardApp } from './dashboard_app';
import { addHelpMenuToAppChrome, DashboardPanelStorage } from './lib';
Expand Down Expand Up @@ -47,6 +48,7 @@ export const dashboardUrlParams = {
export interface DashboardMountProps {
appUnMounted: () => void;
restorePreviousUrl: () => void;

scopedHistory: ScopedHistory<unknown>;
element: AppMountParameters['element'];
initializerContext: PluginInitializerContext;
Expand Down Expand Up @@ -80,6 +82,9 @@ export async function mountApp({
savedObjectsTaggingOss,
} = pluginsStart;

const spacesApi = pluginsStart.spacesOss?.isSpacesAvailable ? pluginsStart.spacesOss : undefined;
const activeSpaceId = spacesApi && (await spacesApi.activeSpace$.pipe(first()).toPromise())?.id;

const dashboardServices: DashboardAppServices = {
navigation,
onAppLeave,
Expand All @@ -99,7 +104,10 @@ export async function mountApp({
indexPatterns: dataStart.indexPatterns,
savedQueryService: dataStart.query.savedQueries,
savedObjectsClient: coreStart.savedObjects.client,
dashboardPanelStorage: new DashboardPanelStorage(core.notifications.toasts),
dashboardPanelStorage: new DashboardPanelStorage(
core.notifications.toasts,
activeSpaceId || 'default'
),
savedDashboards: dashboardStart.getSavedDashboardLoader(),
savedObjectsTagging: savedObjectsTaggingOss?.getTaggingApi(),
allowByValueEmbeddables: initializerContext.config.get<DashboardFeatureFlagConfig>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import { set } from '@elastic/safer-lodash-set';
import { Storage } from '../../services/kibana_utils';
import { NotificationsStart } from '../../services/core';
import { panelStorageErrorStrings } from '../../dashboard_strings';
Expand All @@ -17,16 +18,17 @@ const DASHBOARD_PANELS_SESSION_KEY = 'dashboardStateManagerPanels';
export class DashboardPanelStorage {
private sessionStorage: Storage;

constructor(private toasts: NotificationsStart['toasts']) {
constructor(private toasts: NotificationsStart['toasts'], private activeSpaceId: string) {
this.sessionStorage = new Storage(sessionStorage);
}

public clearPanels(id = DASHBOARD_PANELS_UNSAVED_ID) {
try {
const sessionStoragePanels = this.sessionStorage.get(DASHBOARD_PANELS_SESSION_KEY) || {};
if (sessionStoragePanels[id]) {
delete sessionStoragePanels[id];
this.sessionStorage.set(DASHBOARD_PANELS_SESSION_KEY, sessionStoragePanels);
const sessionStorage = this.sessionStorage.get(DASHBOARD_PANELS_SESSION_KEY);
const sessionStorageForSpace = sessionStorage?.[this.activeSpaceId] || {};
if (sessionStorageForSpace[id]) {
delete sessionStorageForSpace[id];
this.sessionStorage.set(DASHBOARD_PANELS_SESSION_KEY, sessionStorage);
}
} catch (e) {
this.toasts.addDanger({
Expand All @@ -38,7 +40,7 @@ export class DashboardPanelStorage {

public getPanels(id = DASHBOARD_PANELS_UNSAVED_ID): SavedDashboardPanel[] | undefined {
try {
return this.sessionStorage.get(DASHBOARD_PANELS_SESSION_KEY)?.[id];
return this.sessionStorage.get(DASHBOARD_PANELS_SESSION_KEY)?.[this.activeSpaceId]?.[id];
} catch (e) {
this.toasts.addDanger({
title: panelStorageErrorStrings.getPanelsGetError(e.message),
Expand All @@ -50,7 +52,7 @@ export class DashboardPanelStorage {
public setPanels(id = DASHBOARD_PANELS_UNSAVED_ID, newPanels: SavedDashboardPanel[]) {
try {
const sessionStoragePanels = this.sessionStorage.get(DASHBOARD_PANELS_SESSION_KEY) || {};
sessionStoragePanels[id] = newPanels;
set(sessionStoragePanels, [this.activeSpaceId, id], newPanels);
this.sessionStorage.set(DASHBOARD_PANELS_SESSION_KEY, sessionStoragePanels);
} catch (e) {
this.toasts.addDanger({
Expand All @@ -62,7 +64,9 @@ export class DashboardPanelStorage {

public getDashboardIdsWithUnsavedChanges() {
try {
return Object.keys(this.sessionStorage.get(DASHBOARD_PANELS_SESSION_KEY) || {});
return Object.keys(
this.sessionStorage.get(DASHBOARD_PANELS_SESSION_KEY)?.[this.activeSpaceId] || {}
);
} catch (e) {
this.toasts.addDanger({
title: panelStorageErrorStrings.getPanelsGetError(e.message),
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/dashboard/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import { UrlGeneratorState } from '../../share/public';
import { ExportCSVAction } from './application/actions/export_csv_action';
import { dashboardFeatureCatalog } from './dashboard_strings';
import { replaceUrlHashQuery } from '../../kibana_utils/public';
import { SpacesOssPluginStart } from './services/spaces';

declare module '../../share/public' {
export interface UrlGeneratorStateMapping {
Expand Down Expand Up @@ -113,6 +114,7 @@ export interface DashboardStartDependencies {
savedObjects: SavedObjectsStart;
presentationUtil: PresentationUtilPluginStart;
savedObjectsTaggingOss?: SavedObjectTaggingOssPluginStart;
spacesOss?: SpacesOssPluginStart;
}

export type DashboardSetup = void;
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/dashboard/public/services/spaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export { SpacesOssPluginStart } from '../../../spaces_oss/public';
1 change: 1 addition & 0 deletions src/plugins/dashboard/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
{ "path": "../saved_objects_tagging_oss/tsconfig.json" },
{ "path": "../saved_objects/tsconfig.json" },
{ "path": "../ui_actions/tsconfig.json" },
{ "path": "../spaces_oss/tsconfig.json" },
]
}

0 comments on commit 5f48de9

Please sign in to comment.