Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rshen91 committed Oct 7, 2024
1 parent 296a49c commit f0bea6e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/plugins/dashboard/public/dashboard_app/dashboard_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
removeSearchSessionIdFromURL,
} from './url/search_sessions_integration';
import { loadAndRemoveDashboardState, startSyncingExpandedPanelState } from './url/url_utils';
import { DASHBOARD_FILTERS_LOCAL_KEY } from '../services/dashboard_backup_service';

export interface DashboardAppProps {
history: History;
Expand Down Expand Up @@ -135,6 +136,7 @@ export function DashboardApp({
screenshotModeService.getScreenshotContext('layout') === 'print'
? { viewMode: ViewMode.PRINT }
: {}),
filters: JSON.parse(localStorage.getItem(DASHBOARD_FILTERS_LOCAL_KEY) ?? '[]'),
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ export const backupServiceStrings = {
defaultMessage: 'Error encountered while backing up view mode: {message}',
values: { message },
}),
filtersStorageError: (message: string) =>
i18n.translate('dashboard.filtersBackup.error', {
defaultMessage: 'Error encountered while backing up filters: {message}',
values: { message },
}),
getPanelsGetError: (message: string) =>
i18n.translate('dashboard.panelStorageError.getError', {
defaultMessage: 'Error encountered while fetching unsaved changes: {message}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ViewMode } from '@kbn/embeddable-plugin/public';
import { Storage } from '@kbn/kibana-utils-plugin/public';
import { set } from '@kbn/safer-lodash-set';

import { Filter } from '@kbn/es-query';
import type { DashboardContainerInput } from '../../common';
import { backupServiceStrings } from '../dashboard_container/_dashboard_container_strings';
import { UnsavedPanelState } from '../dashboard_container/types';
Expand All @@ -24,6 +25,7 @@ export const DASHBOARD_PANELS_UNSAVED_ID = 'unsavedDashboard';
export const PANELS_CONTROL_GROUP_KEY = 'controlGroup';
const DASHBOARD_PANELS_SESSION_KEY = 'dashboardPanels';
const DASHBOARD_VIEWMODE_LOCAL_KEY = 'dashboardViewMode';
export const DASHBOARD_FILTERS_LOCAL_KEY = 'dashboardFilters';

// this key is named `panels` for BWC reasons, but actually contains the entire dashboard state
const DASHBOARD_STATE_SESSION_KEY = 'dashboardStateManagerPanels';
Expand Down Expand Up @@ -81,6 +83,21 @@ class DashboardBackupService implements DashboardBackupServiceType {
}
};

public getFilters = (): Filter => {
return this.localStorage.get(DASHBOARD_FILTERS_LOCAL_KEY);
};

public storeFilters = (filters: Filter[]) => {
try {
this.localStorage.set(DASHBOARD_FILTERS_LOCAL_KEY, filters);
} catch (e) {
coreServices.notifications.toasts.addDanger({
title: backupServiceStrings.filtersStorageError(e.message),
'data-test-subj': 'dashboardFiltersBackupFailure',
});
}
};

public clearState(id = DASHBOARD_PANELS_UNSAVED_ID) {
try {
const dashboardStateStorage =
Expand Down Expand Up @@ -117,7 +134,9 @@ class DashboardBackupService implements DashboardBackupServiceType {
id
] as UnsavedPanelState | undefined;

return { dashboardState, panels };
const filters = dashboardState?.filters;

return { dashboardState, panels, filters };
} catch (e) {
coreServices.notifications.toasts.addDanger({
title: backupServiceStrings.getPanelsGetError(e.message),
Expand All @@ -135,6 +154,7 @@ class DashboardBackupService implements DashboardBackupServiceType {
const dashboardStateStorage = this.sessionStorage.get(DASHBOARD_STATE_SESSION_KEY) ?? {};
set(dashboardStateStorage, [this.activeSpaceId, id], newState);
this.sessionStorage.set(DASHBOARD_STATE_SESSION_KEY, dashboardStateStorage);
this.storeFilters(dashboardStateStorage[this.activeSpaceId][id]?.filters ?? []);

const panelsStorage = this.sessionStorage.get(DASHBOARD_PANELS_SESSION_KEY) ?? {};
set(panelsStorage, [this.activeSpaceId, id], unsavedPanels);
Expand Down

0 comments on commit f0bea6e

Please sign in to comment.