Skip to content

Commit

Permalink
[Lens] warn user before leaving editor after creating new library ann…
Browse files Browse the repository at this point in the history
…otation group (#161307)
  • Loading branch information
drewdaemon authored Jul 7, 2023
1 parent 2774812 commit e9e5c3c
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ export function LayerPanels(
setNextFocusedLayerId(layerId);
};

const registerLibraryAnnotationGroupFunction = useCallback(
(groupInfo) => dispatchLens(registerLibraryAnnotationGroup(groupInfo)),
[dispatchLens]
);

const hideAddLayerButton = query && isOfAggregateQueryType(query);

return (
Expand All @@ -261,6 +266,7 @@ export function LayerPanels(
!hidden && (
<LayerPanel
{...props}
registerLibraryAnnotationGroup={registerLibraryAnnotationGroupFunction}
dimensionGroups={groups}
activeVisualization={activeVisualization}
registerNewLayerRef={registerNewLayerRef}
Expand Down Expand Up @@ -369,8 +375,7 @@ export function LayerPanels(
);
}
},
registerLibraryAnnotationGroup: (groupInfo) =>
dispatchLens(registerLibraryAnnotationGroup(groupInfo)),
registerLibraryAnnotationGroup: registerLibraryAnnotationGroupFunction,
})}
</EuiForm>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ describe('LayerPanel', () => {
toggleFullscreen: jest.fn(),
onEmptyDimensionAdd: jest.fn(),
onChangeIndexPattern: jest.fn(),
registerLibraryAnnotationGroup: jest.fn(),
indexPatternService: createIndexPatternServiceMock(),
getUserMessages: () => [],
displayLayerSettings: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
VisualizationDimensionGroupConfig,
UserMessagesGetter,
AddLayerFunction,
RegisterLibraryAnnotationGroupFunction,
} from '../../../types';
import { LayerSettings } from './layer_settings';
import { LayerPanelProps, ActiveDimensionState } from './types';
Expand Down Expand Up @@ -63,6 +64,7 @@ export function LayerPanel(
layerIndex: number;
isOnlyLayer: boolean;
addLayer: AddLayerFunction;
registerLibraryAnnotationGroup: RegisterLibraryAnnotationGroupFunction;
updateVisualization: StateSetter<unknown>;
updateDatasource: (
datasourceId: string | undefined,
Expand Down Expand Up @@ -352,6 +354,7 @@ export function LayerPanel(
layerId,
visualizationState,
updateVisualization,
props.registerLibraryAnnotationGroup,
isSaveable
)
.map((action) => ({
Expand Down Expand Up @@ -385,18 +388,19 @@ export function LayerPanel(
].filter((i) => i.isCompatible),
[
activeVisualization,
layerId,
visualizationState,
updateVisualization,
props.registerLibraryAnnotationGroup,
isSaveable,
core,
layerIndex,
isOnlyLayer,
isTextBasedLanguage,
visualizationLayerSettings,
layerDatasource?.renderLayerSettings,
layerId,
layerIndex,
onCloneLayer,
onRemoveLayer,
updateVisualization,
visualizationLayerSettings,
visualizationState,
isSaveable,
]
);
const layerActionsFlyoutRef = useRef<HTMLDivElement | null>(null);
Expand Down
11 changes: 7 additions & 4 deletions x-pack/plugins/lens/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,11 @@ export interface VisualizationLayerDescription {
autoTimeField?: boolean;
}>;
}

export type RegisterLibraryAnnotationGroupFunction = (groupInfo: {
id: string;
group: EventAnnotationGroupConfig;
}) => void;
export interface Visualization<T = unknown, P = T, ExtraAppendLayerArg = unknown> {
/** Plugin ID, such as "lnsXY" */
id: string;
Expand Down Expand Up @@ -1115,6 +1120,7 @@ export interface Visualization<T = unknown, P = T, ExtraAppendLayerArg = unknown
layerId: string,
state: T,
setState: StateSetter<T>,
registerLibraryAnnotationGroup: RegisterLibraryAnnotationGroupFunction,
isSaveable?: boolean
) => LayerAction[];

Expand Down Expand Up @@ -1251,10 +1257,7 @@ export interface Visualization<T = unknown, P = T, ExtraAppendLayerArg = unknown
supportedLayers: VisualizationLayerDescription[];
addLayer: AddLayerFunction;
ensureIndexPattern: (specOrId: DataViewSpec | string) => Promise<void>;
registerLibraryAnnotationGroup: (groupInfo: {
id: string;
group: EventAnnotationGroupConfig;
}) => void;
registerLibraryAnnotationGroup: RegisterLibraryAnnotationGroupFunction;
}) => JSX.Element | null;
/**
* Creates map of columns ids and unique lables. Used only for noDatasource layers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import type { ThemeServiceStart } from '@kbn/core/public';
import { DataViewsContract } from '@kbn/data-views-plugin/public';
import { VISUALIZE_APP_NAME } from '@kbn/visualizations-plugin/common/constants';
import { ANNOTATIONS_LISTING_VIEW_ID } from '@kbn/event-annotation-plugin/common';
import type { LayerAction, StateSetter } from '../../../../types';
import type {
LayerAction,
RegisterLibraryAnnotationGroupFunction,
StateSetter,
} from '../../../../types';
import { XYState, XYAnnotationLayerConfig } from '../../types';
import { getUnlinkLayerAction } from './unlink_action';
import { getSaveLayerAction } from './save_action';
Expand All @@ -23,6 +27,7 @@ export const createAnnotationActions = ({
state,
layer,
setState,
registerLibraryAnnotationGroup,
core,
isSaveable,
eventAnnotationService,
Expand All @@ -33,6 +38,7 @@ export const createAnnotationActions = ({
state: XYState;
layer: XYAnnotationLayerConfig;
setState: StateSetter<XYState, unknown>;
registerLibraryAnnotationGroup: RegisterLibraryAnnotationGroupFunction;
core: CoreStart;
isSaveable?: boolean;
eventAnnotationService: EventAnnotationServiceType;
Expand All @@ -52,6 +58,7 @@ export const createAnnotationActions = ({
state,
layer,
setState,
registerLibraryAnnotationGroup,
eventAnnotationService,
toasts: core.notifications.toasts,
savedObjectsTagging,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
} from '../../types';
import { onSave, SaveModal } from './save_action';
import { shallowWithIntl } from '@kbn/test-jest-helpers';
import { PointInTimeEventAnnotationConfig } from '@kbn/event-annotation-plugin/common';
import {
EventAnnotationGroupConfig,
PointInTimeEventAnnotationConfig,
} from '@kbn/event-annotation-plugin/common';
import { SavedObjectSaveModal } from '@kbn/saved-objects-plugin/public';
import { taggingApiMock } from '@kbn/saved-objects-tagging-plugin/public/mocks';
import { dataViewPluginMocks } from '@kbn/data-views-plugin/public/mocks';
Expand Down Expand Up @@ -145,6 +148,7 @@ describe('annotation group save action', () => {
layers: [{ layerId } as XYAnnotationLayerConfig],
} as XYState,
layer: byValueLayer,
registerLibraryAnnotationGroup: jest.fn(),
setState: jest.fn(),
eventAnnotationService: {
createAnnotationGroup: jest.fn(() => Promise.resolve({ id: savedId })),
Expand Down Expand Up @@ -178,13 +182,22 @@ describe('annotation group save action', () => {
test('successful initial save', async () => {
await onSave(props);

expect(props.eventAnnotationService.createAnnotationGroup).toHaveBeenCalledWith({
const expectedConfig: EventAnnotationGroupConfig = {
annotations: props.layer.annotations,
indexPatternId: props.layer.indexPatternId,
ignoreGlobalFilters: props.layer.ignoreGlobalFilters,
title: props.modalOnSaveProps.newTitle,
description: props.modalOnSaveProps.newDescription,
tags: props.modalOnSaveProps.newTags,
};

expect(props.eventAnnotationService.createAnnotationGroup).toHaveBeenCalledWith(
expectedConfig
);

expect(props.registerLibraryAnnotationGroup).toHaveBeenCalledWith({
id: savedId,
group: expectedConfig,
});

expect(props.modalOnSaveProps.closeModal).toHaveBeenCalled();
Expand All @@ -206,14 +219,23 @@ describe('annotation group save action', () => {

await onSave(props);

expect(props.eventAnnotationService.createAnnotationGroup).toHaveBeenCalledWith({
const expectedConfig: EventAnnotationGroupConfig = {
annotations: props.layer.annotations,
indexPatternId: props.layer.indexPatternId,
ignoreGlobalFilters: props.layer.ignoreGlobalFilters,
title: props.modalOnSaveProps.newTitle,
description: props.modalOnSaveProps.newDescription,
tags: props.modalOnSaveProps.newTags,
dataViewSpec,
};

expect(props.eventAnnotationService.createAnnotationGroup).toHaveBeenCalledWith(
expectedConfig
);

expect(props.registerLibraryAnnotationGroup).toHaveBeenCalledWith({
id: savedId,
group: expectedConfig,
});

expect(props.modalOnSaveProps.closeModal).toHaveBeenCalled();
Expand Down Expand Up @@ -241,6 +263,8 @@ describe('annotation group save action', () => {

expect(props.toasts.addError).toHaveBeenCalledTimes(1);

expect(props.registerLibraryAnnotationGroup).not.toHaveBeenCalled();

expect(props.modalOnSaveProps.closeModal).not.toHaveBeenCalled();

expect(props.setState).not.toHaveBeenCalled();
Expand Down Expand Up @@ -307,13 +331,22 @@ describe('annotation group save action', () => {

expect(props.eventAnnotationService.updateAnnotationGroup).not.toHaveBeenCalled();

expect(props.eventAnnotationService.createAnnotationGroup).toHaveBeenCalledWith({
const expectedConfig: EventAnnotationGroupConfig = {
annotations: props.layer.annotations,
indexPatternId: props.layer.indexPatternId,
ignoreGlobalFilters: props.layer.ignoreGlobalFilters,
title: props.modalOnSaveProps.newTitle,
description: props.modalOnSaveProps.newDescription,
tags: props.modalOnSaveProps.newTags,
};

expect(props.eventAnnotationService.createAnnotationGroup).toHaveBeenCalledWith(
expectedConfig
);

expect(props.registerLibraryAnnotationGroup).toHaveBeenCalledWith({
id: savedId,
group: expectedConfig,
});

expect(props.modalOnSaveProps.closeModal).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import { EventAnnotationGroupConfig } from '@kbn/event-annotation-plugin/common'
import { EuiIcon, EuiLink } from '@elastic/eui';
import { type SavedObjectTaggingPluginStart } from '@kbn/saved-objects-tagging-plugin/public';
import { DataViewsContract } from '@kbn/data-views-plugin/public';
import type { LayerAction, StateSetter } from '../../../../types';
import type {
LayerAction,
RegisterLibraryAnnotationGroupFunction,
StateSetter,
} from '../../../../types';
import { XYByReferenceAnnotationLayerConfig, XYAnnotationLayerConfig, XYState } from '../../types';
import { isByReferenceAnnotationsLayer } from '../../visualization_helpers';

Expand Down Expand Up @@ -160,6 +164,7 @@ export const onSave = async ({
state,
layer,
setState,
registerLibraryAnnotationGroup,
eventAnnotationService,
toasts,
modalOnSaveProps: {
Expand All @@ -177,6 +182,10 @@ export const onSave = async ({
state: XYState;
layer: XYAnnotationLayerConfig;
setState: StateSetter<XYState, unknown>;
registerLibraryAnnotationGroup: (props: {
id: string;
group: EventAnnotationGroupConfig;
}) => void;
eventAnnotationService: EventAnnotationServiceType;
toasts: ToastsStart;
modalOnSaveProps: ModalOnSaveProps;
Expand All @@ -202,6 +211,12 @@ export const onSave = async ({
eventAnnotationService,
dataViews
);

// add new group to state
registerLibraryAnnotationGroup({
id: savedInfo.id,
group: savedInfo.config,
});
} catch (err) {
toasts.addError(err, {
title: i18n.translate(
Expand Down Expand Up @@ -275,6 +290,7 @@ export const getSaveLayerAction = ({
state,
layer,
setState,
registerLibraryAnnotationGroup,
eventAnnotationService,
toasts,
savedObjectsTagging,
Expand All @@ -285,6 +301,7 @@ export const getSaveLayerAction = ({
state: XYState;
layer: XYAnnotationLayerConfig;
setState: StateSetter<XYState, unknown>;
registerLibraryAnnotationGroup: RegisterLibraryAnnotationGroupFunction;
eventAnnotationService: EventAnnotationServiceType;
toasts: ToastsStart;
savedObjectsTagging?: SavedObjectTaggingPluginStart;
Expand Down Expand Up @@ -320,6 +337,7 @@ export const getSaveLayerAction = ({
state,
layer,
setState,
registerLibraryAnnotationGroup,
eventAnnotationService,
toasts,
modalOnSaveProps: props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3633,7 +3633,7 @@ describe('xy_visualization', () => {
describe('getSupportedActionsForLayer', () => {
it('should return no actions for a data layer', () => {
expect(
xyVisualization.getSupportedActionsForLayer?.('first', exampleState(), jest.fn())
xyVisualization.getSupportedActionsForLayer?.('first', exampleState(), jest.fn(), jest.fn())
).toHaveLength(0);
});

Expand All @@ -3655,6 +3655,7 @@ describe('xy_visualization', () => {
...baseState,
layers: [annotationLayer],
},
jest.fn(),
jest.fn()
)
).toEqual([]);
Expand Down Expand Up @@ -3688,6 +3689,7 @@ describe('xy_visualization', () => {
layers: [annotationLayer],
},
jest.fn(),
jest.fn(),
true
)
).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -3736,6 +3738,7 @@ describe('xy_visualization', () => {
layers: [annotationLayer],
},
jest.fn(),
jest.fn(),
false
)
.some((action) => action['data-test-subj'] === 'lnsXY_annotationLayer_saveToLibrary')
Expand All @@ -3758,6 +3761,7 @@ describe('xy_visualization', () => {
],
},
jest.fn(),
jest.fn(),
false
)
.find((action) => action['data-test-subj'] === 'lnsXY_annotationLayer_revertChanges')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,13 @@ export const getXyVisualization = ({
];
},

getSupportedActionsForLayer(layerId, state, setState, isSaveable) {
getSupportedActionsForLayer(
layerId,
state,
setState,
registerLibraryAnnotationGroup,
isSaveable
) {
const layerIndex = state.layers.findIndex((l) => l.layerId === layerId);
const layer = state.layers[layerIndex];
const actions = [];
Expand All @@ -308,6 +314,7 @@ export const getXyVisualization = ({
state,
layer,
setState,
registerLibraryAnnotationGroup,
core,
isSaveable,
eventAnnotationService,
Expand Down

0 comments on commit e9e5c3c

Please sign in to comment.