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

[UiActions][Drilldowns] Fix actions sorting in context menu #77162

Merged
merged 2 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plugins/data/public/actions/apply_filter_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function createFilterAction(
return createAction<typeof ACTION_GLOBAL_APPLY_FILTER>({
type: ACTION_GLOBAL_APPLY_FILTER,
id: ACTION_GLOBAL_APPLY_FILTER,
order: 100,
getIconType: () => 'filter',
getDisplayName: () => {
return i18n.translate('data.filter.applyFilterActionTitle', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function createFilterAction(): ActionByType<typeof ACTION_APPLY_FILTER> {
return createAction<typeof ACTION_APPLY_FILTER>({
type: ACTION_APPLY_FILTER,
id: ACTION_APPLY_FILTER,
order: 100,
getIconType: () => 'filter',
getDisplayName: () => {
return i18n.translate('embeddableApi.actions.applyFilterActionTitle', {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { buildContextMenuForActions } from './build_eui_context_menu_panels';
import { Action, createAction } from '../actions';

const createTestAction = ({
type,
dispayName,
order,
}: {
type: string;
dispayName: string;
order: number;
}) =>
createAction({
type: type as any, // mapping doesn't matter for this test
getDisplayName: () => dispayName,
order,
execute: async () => {},
});

test('contextMenu actions sorting: order, type, displayName', async () => {
const actions: Action[] = [
createTestAction({
order: 100,
type: '1',
dispayName: 'a',
}),
createTestAction({
order: 100,
type: '1',
dispayName: 'b',
}),
createTestAction({
order: 0,
type: '2',
dispayName: 'c',
}),
createTestAction({
order: 0,
type: '2',
dispayName: 'd',
}),
createTestAction({
order: 0,
type: '3',
dispayName: 'aa',
}),
].sort(() => 0.5 - Math.random());

const result = await buildContextMenuForActions({
actions: actions.map((action) => ({ action, context: {}, trigger: '' as any })),
});

expect(result.items?.map((item) => item.name as string)).toMatchInlineSnapshot(`
Array [
"a",
"b",
"c",
"d",
"aa",
]
`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import * as React from 'react';
import { EuiContextMenuPanelDescriptor, EuiContextMenuPanelItemDescriptor } from '@elastic/eui';
import _ from 'lodash';
import sortBy from 'lodash/sortBy';
import { i18n } from '@kbn/i18n';
import { uiToReactComponent } from '../../../kibana_react/public';
import { Action } from '../actions';
Expand All @@ -46,11 +47,11 @@ interface ActionWithContext<Context extends BaseContext = BaseContext> {
export async function buildContextMenuForActions({
actions,
title = defaultTitle,
closeMenu,
closeMenu = () => {},
}: {
actions: ActionWithContext[];
title?: string;
closeMenu: () => void;
closeMenu?: () => void;
}): Promise<EuiContextMenuPanelDescriptor> {
const menuItems = await buildEuiContextMenuPanelItems({
actions,
Expand All @@ -74,6 +75,13 @@ async function buildEuiContextMenuPanelItems({
actions: ActionWithContext[];
closeMenu: () => void;
}) {
actions = sortBy(
actions,
(a) => -1 * (a.action.order ?? 0),
(a) => a.action.type,
(a) => a.action.getDisplayName({ ...a.context, trigger: a.trigger })
);

const items: EuiContextMenuPanelItemDescriptor[] = new Array(actions.length);
const promises = actions.map(async ({ action, context, trigger }, index) => {
const isCompatible = await action.isCompatible({
Expand Down