Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dashboard] Do not reset panel to undefined or empty last saved state #203158

Merged
merged 8 commits into from
Dec 10, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export function getPageApi() {
children$.next(children);
}
newPanels = {};
return true;
},
timeRange$,
unsavedChanges: unsavedChanges$ as PublishingSubject<object | undefined>,
Expand Down
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should remove this typing after the architectural fix is in? I think this should be temporary because in theory, the reset function shouldn't be capable of failing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. This typing fix is only temporary.

}

export const apiPublishesUnsavedChanges = (api: unknown): api is PublishesUnsavedChanges => {
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/controls/public/controls/mocks/control_mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export const getMockedBuildApi =
uuid,
parentApi: controlGroupApi ?? getMockedControlGroupApi(),
unsavedChanges: new BehaviorSubject<Partial<StateType> | undefined>(undefined),
resetUnsavedChanges: () => {},
resetUnsavedChanges: () => {
return true;
},
type: factory.type,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ describe('TimesliderControlApi', () => {
uuid,
parentApi: controlGroupApi,
unsavedChanges: new BehaviorSubject<Partial<TimesliderControlState> | undefined>(undefined),
resetUnsavedChanges: () => {},
resetUnsavedChanges: () => {
return true;
},
type: factory.type,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import { KibanaRenderContextProvider } from '@kbn/react-kibana-context-render';
import { LocatorPublic } from '@kbn/share-plugin/common';
import { ExitFullScreenButtonKibanaProvider } from '@kbn/shared-ux-button-exit-full-screen';

import { i18n } from '@kbn/i18n';
import { DASHBOARD_CONTAINER_TYPE, DashboardApi, DashboardLocatorParams } from '../..';
import type { DashboardAttributes } from '../../../server/content_management';
import { DashboardContainerInput, DashboardPanelMap, DashboardPanelState } from '../../../common';
Expand Down Expand Up @@ -957,7 +958,16 @@ 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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to have a warning in this case. I wonder if we should clarify that the user needs to start a new session (open a new tab etc) to get their last saved state back?

Copy link
Contributor Author

@nreese nreese Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not want to complicate the error message. Its complex because if its a by-ref panel then a new session will not revert the changes because the by-ref saved object has already been saved

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because the by-ref saved object has already been saved

Yet another example of why we need to fix this architecturally, as save and return on a by reference panel shouldn't even show unsaved changes on the Dashboard.

++ to not complicating the error message. Ideally this isn't seen by users at all.

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