-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5301403
commit 05eae3b
Showing
4 changed files
with
123 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
x-pack/plugins/apm/public/components/shared/transaction_action_menu/sections_helper.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { getNonEmptySections } from './sections_helper'; | ||
|
||
describe('getNonEmptySections', () => { | ||
it('returns empty when no section is available', () => { | ||
expect(getNonEmptySections({})).toEqual([]); | ||
}); | ||
it("returns empty when section doesn't have actions", () => { | ||
expect( | ||
getNonEmptySections({ | ||
foo: [ | ||
{ | ||
key: 'foo', | ||
title: 'Foo', | ||
subtitle: 'Foo bar', | ||
actions: [], | ||
}, | ||
], | ||
}) | ||
).toEqual([]); | ||
}); | ||
|
||
it('returns only sections with actions with condition true', () => { | ||
expect( | ||
getNonEmptySections({ | ||
foo: [ | ||
{ | ||
key: 'foo', | ||
title: 'Foo', | ||
subtitle: 'Foo bar', | ||
actions: [], | ||
}, | ||
], | ||
bar: [ | ||
{ | ||
key: 'bar', | ||
title: 'Bar', | ||
subtitle: 'Bar foo', | ||
actions: [ | ||
{ | ||
key: 'bar_action', | ||
label: 'Bar Action', | ||
condition: true, | ||
}, | ||
{ | ||
key: 'bar_action_2', | ||
label: 'Bar Action 2', | ||
condition: false, | ||
}, | ||
], | ||
}, | ||
], | ||
}) | ||
).toEqual([ | ||
[ | ||
{ | ||
key: 'bar', | ||
title: 'Bar', | ||
subtitle: 'Bar foo', | ||
actions: [ | ||
{ | ||
key: 'bar_action', | ||
label: 'Bar Action', | ||
condition: true, | ||
}, | ||
], | ||
}, | ||
], | ||
]); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
x-pack/plugins/apm/public/components/shared/transaction_action_menu/sections_helper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { isEmpty } from 'lodash'; | ||
|
||
export interface Action { | ||
key: string; | ||
label: string; | ||
href?: string; | ||
onClick?: () => void; | ||
condition: boolean; | ||
} | ||
|
||
interface Section { | ||
key: string; | ||
title?: string; | ||
subtitle?: string; | ||
actions: Action[]; | ||
} | ||
|
||
export type SectionRecord = Record<string, Section[]>; | ||
|
||
/** Filter out actions that shouldnt be shown and sections without any actions. */ | ||
export function getNonEmptySections(sectionRecord: SectionRecord) { | ||
return Object.values(sectionRecord) | ||
.map((sections) => | ||
sections | ||
.map((section) => ({ | ||
...section, | ||
actions: section.actions.filter((action) => action.condition), | ||
})) | ||
.filter((section) => !isEmpty(section.actions)) | ||
) | ||
.filter((sections) => !isEmpty(sections)); | ||
} |