Skip to content

Commit

Permalink
[dashboard] Do not reset panel to undefined or empty last saved state
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Dec 5, 2024
1 parent 2ae1dd4 commit f5b6dd5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import { waitFor } from '@testing-library/react';
describe('childrenUnsavedChanges$', () => {
const child1Api = {
unsavedChanges: new BehaviorSubject<object | undefined>(undefined),
resetUnsavedChanges: () => undefined,
resetUnsavedChanges: () => true,
};
const child2Api = {
unsavedChanges: new BehaviorSubject<object | undefined>(undefined),
resetUnsavedChanges: () => undefined,
resetUnsavedChanges: () => true,
};
const children$ = new BehaviorSubject<{ [key: string]: unknown }>({});
const onFireMock = jest.fn();
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('childrenUnsavedChanges$', () => {
...children$.value,
child3: {
unsavedChanges: new BehaviorSubject<object | undefined>({ key1: 'modified value' }),
resetUnsavedChanges: () => undefined,
resetUnsavedChanges: () => true,
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,19 @@ export const initializeUnsavedChanges = <RuntimeState extends {} = {}>(
unsavedChanges,
resetUnsavedChanges: () => {
const lastSaved = lastSavedState$.getValue();

// Do not reset to undefined or empty last saved state
// Temporary fix for https://github.com/elastic/kibana/issues/201627
// TODO remove when architecture fix resolves issue.
if (comparatorKeys.length && (!lastSaved || Object.keys(lastSaved).length === 0)) {
return false;
}

for (const key of comparatorKeys) {
const setter = comparators[key][1]; // setter function is the 1st element of the tuple
setter(lastSaved?.[key] as RuntimeState[typeof key]);
}
return true;
},
snapshotRuntimeState,
} as PublishesUnsavedChanges<RuntimeState> & HasSnapshottableState<RuntimeState>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PublishingSubject } from '../publishing_subject';

export interface PublishesUnsavedChanges<Runtime extends object = object> {
unsavedChanges: PublishingSubject<Partial<Runtime> | undefined>;
resetUnsavedChanges: () => void;
resetUnsavedChanges: () => boolean;
}

export const apiPublishesUnsavedChanges = (api: unknown): api is PublishesUnsavedChanges => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ import {
} from './dashboard_container_factory';
import { InitialComponentState, getDashboardApi } from '../../dashboard_api/get_dashboard_api';
import type { DashboardCreationOptions } from '../..';
import { i18n } from '@kbn/i18n';

export interface InheritedChildInput {
filters: Filter[];
Expand Down Expand Up @@ -957,7 +958,14 @@ export class DashboardContainer
for (const panelId of Object.keys(currentChildren)) {
if (this.getInput().panels[panelId]) {
const child = currentChildren[panelId];
if (apiPublishesUnsavedChanges(child)) child.resetUnsavedChanges();
if (apiPublishesUnsavedChanges(child)) {
const success = child.resetUnsavedChanges();
if (!success) {
coreServices.notifications.toasts.addWarning(i18n.translate('dashboard.reset.panelError', {
defaultMessage: 'Unable to reset panel changes',
}));
}
}
} else {
// if reset resulted in panel removal, we need to update the list of children
delete currentChildren[panelId];
Expand Down

0 comments on commit f5b6dd5

Please sign in to comment.