-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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 navigation] Inject/extract references #164330
Merged
nickpeihl
merged 7 commits into
elastic:navigation-embeddable
from
nickpeihl:navEmbeddable-references
Aug 31, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56d1fe7
Inject/extract references
nickpeihl af699de
Merge remote-tracking branch 'refs/remotes/upstream/navigation-embedd…
nickpeihl eb4426d
Remove TODO
nickpeihl 491ad68
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine 0b74608
Merge remote-tracking branch 'upstream/navigation-embeddable' into na…
nickpeihl 65f7ba1
Add `destinationRefName` property to saved objects
nickpeihl 848daf2
Add support for mailto protocol
nickpeihl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
64 changes: 64 additions & 0 deletions
64
src/plugins/navigation_embeddable/common/embeddable/extract.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,64 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { extract } from './extract'; | ||
|
||
test('Should return original state and empty references with by-reference embeddable state', () => { | ||
const navigationEmbeddableByReferenceInput = { | ||
id: '2192e502-0ec7-4316-82fb-c9bbf78525c4', | ||
type: 'navigation_embeddable', | ||
}; | ||
|
||
expect(extract!(navigationEmbeddableByReferenceInput)).toEqual({ | ||
state: navigationEmbeddableByReferenceInput, | ||
references: [], | ||
}); | ||
}); | ||
|
||
test('Should update state with refNames with by-value embeddable state', () => { | ||
const navigationEmbeddableByValueInput = { | ||
id: '8d62c3f0-c61f-4c09-ac24-9b8ee4320e20', | ||
attributes: { | ||
links: [ | ||
{ | ||
type: 'dashboardLink', | ||
id: 'fc7b8c70-2eb9-40b2-936d-457d1721a438', | ||
destination: 'elastic_agent-1a4e7280-6b5e-11ed-98de-67bdecd21824', | ||
order: 0, | ||
}, | ||
], | ||
layout: 'horizontal', | ||
}, | ||
type: 'navigation_embeddable', | ||
}; | ||
|
||
expect(extract!(navigationEmbeddableByValueInput)).toEqual({ | ||
references: [ | ||
{ | ||
name: 'link_fc7b8c70-2eb9-40b2-936d-457d1721a438_dashboard', | ||
type: 'dashboard', | ||
id: 'elastic_agent-1a4e7280-6b5e-11ed-98de-67bdecd21824', | ||
}, | ||
], | ||
state: { | ||
id: '8d62c3f0-c61f-4c09-ac24-9b8ee4320e20', | ||
attributes: { | ||
links: [ | ||
{ | ||
type: 'dashboardLink', | ||
id: 'fc7b8c70-2eb9-40b2-936d-457d1721a438', | ||
destinationRefName: 'link_fc7b8c70-2eb9-40b2-936d-457d1721a438_dashboard', | ||
order: 0, | ||
}, | ||
], | ||
layout: 'horizontal', | ||
}, | ||
type: 'navigation_embeddable', | ||
}, | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
src/plugins/navigation_embeddable/common/embeddable/extract.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,35 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { EmbeddableRegistryDefinition } from '@kbn/embeddable-plugin/common'; | ||
import type { NavigationEmbeddableAttributes } from '../content_management'; | ||
import { extractReferences } from '../persistable_state'; | ||
import { NavigationEmbeddablePersistableState } from './types'; | ||
|
||
export const extract: EmbeddableRegistryDefinition['extract'] = (state) => { | ||
const typedState = state as NavigationEmbeddablePersistableState; | ||
|
||
// by-reference embeddable | ||
if (!('attributes' in typedState) || typedState.attributes === undefined) { | ||
// No references to extract for by-reference embeddable since all references are stored with by-reference saved object | ||
return { state, references: [] }; | ||
} | ||
|
||
// by-value embeddable | ||
const { attributes, references } = extractReferences({ | ||
attributes: typedState.attributes as unknown as NavigationEmbeddableAttributes, | ||
}); | ||
|
||
return { | ||
state: { | ||
...state, | ||
attributes, | ||
}, | ||
references, | ||
}; | ||
}; |
10 changes: 10 additions & 0 deletions
10
src/plugins/navigation_embeddable/common/embeddable/index.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,10 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { inject } from './inject'; | ||
export { extract } from './extract'; |
69 changes: 69 additions & 0 deletions
69
src/plugins/navigation_embeddable/common/embeddable/inject.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,69 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { inject } from './inject'; | ||
|
||
test('Should return original state with by-reference embeddable state', () => { | ||
const navigationEmbeddableByReferenceInput = { | ||
id: 'ea40fd4e-216c-49a7-917f-f733c8a2c817', | ||
type: 'navigation_embeddable', | ||
}; | ||
|
||
const references = [ | ||
{ | ||
name: 'panel_ea40fd4e-216c-49a7-917f-f733c8a2c817', | ||
type: 'navigation_embeddable', | ||
id: '7f92d7d0-8e5f-11ec-9477-312c8a6de896', | ||
}, | ||
]; | ||
|
||
expect(inject!(navigationEmbeddableByReferenceInput, references)).toEqual( | ||
navigationEmbeddableByReferenceInput | ||
); | ||
}); | ||
|
||
test('Should inject refNames with by-value embeddable state', () => { | ||
const navigationEmbeddableByValueInput = { | ||
id: 'c3937cf9-29be-43df-a4af-a4df742d7d35', | ||
attributes: { | ||
links: [ | ||
{ | ||
type: 'dashboardLink', | ||
id: 'fc7b8c70-2eb9-40b2-936d-457d1721a438', | ||
destinationRefName: 'link_fc7b8c70-2eb9-40b2-936d-457d1721a438_dashboard', | ||
order: 0, | ||
}, | ||
], | ||
layout: 'horizontal', | ||
}, | ||
type: 'navigation_embeddable', | ||
}; | ||
const references = [ | ||
{ | ||
name: 'link_fc7b8c70-2eb9-40b2-936d-457d1721a438_dashboard', | ||
type: 'dashboard', | ||
id: 'elastic_agent-1a4e7280-6b5e-11ed-98de-67bdecd21824', | ||
}, | ||
]; | ||
|
||
expect(inject!(navigationEmbeddableByValueInput, references)).toEqual({ | ||
id: 'c3937cf9-29be-43df-a4af-a4df742d7d35', | ||
attributes: { | ||
links: [ | ||
{ | ||
type: 'dashboardLink', | ||
id: 'fc7b8c70-2eb9-40b2-936d-457d1721a438', | ||
destination: 'elastic_agent-1a4e7280-6b5e-11ed-98de-67bdecd21824', | ||
order: 0, | ||
}, | ||
], | ||
layout: 'horizontal', | ||
}, | ||
type: 'navigation_embeddable', | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
src/plugins/navigation_embeddable/common/embeddable/inject.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,40 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { EmbeddableRegistryDefinition } from '@kbn/embeddable-plugin/common'; | ||
import { NavigationEmbeddableAttributes } from '../content_management'; | ||
import { injectReferences } from '../persistable_state'; | ||
import { NavigationEmbeddablePersistableState } from './types'; | ||
|
||
export const inject: EmbeddableRegistryDefinition['inject'] = (state, references) => { | ||
const typedState = state as NavigationEmbeddablePersistableState; | ||
|
||
// by-reference embeddable | ||
if (!('attributes' in typedState) || typedState.attributes === undefined) { | ||
return typedState; | ||
} | ||
|
||
// by-value embeddable | ||
try { | ||
const { attributes: attributesWithInjectedIds } = injectReferences({ | ||
attributes: typedState.attributes as unknown as NavigationEmbeddableAttributes, | ||
references, | ||
}); | ||
|
||
return { | ||
...typedState, | ||
attributes: attributesWithInjectedIds, | ||
}; | ||
} catch (error) { | ||
// inject exception prevents entire dashboard from display | ||
// Instead of throwing, swallow error and let dashboard display | ||
// Errors will surface in navigation embeddable panel. | ||
// Users can then manually edit links to resolve any problems. | ||
return typedState; | ||
} | ||
}; |
14 changes: 14 additions & 0 deletions
14
src/plugins/navigation_embeddable/common/embeddable/types.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,14 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { EmbeddableStateWithType } from '@kbn/embeddable-plugin/common'; | ||
import { SerializableRecord } from '@kbn/utility-types'; | ||
|
||
export type NavigationEmbeddablePersistableState = EmbeddableStateWithType & { | ||
attributes: SerializableRecord; | ||
}; |
9 changes: 9 additions & 0 deletions
9
src/plugins/navigation_embeddable/common/persistable_state/index.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,9 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export { extractReferences, injectReferences } from './references'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This small change makes the tests so much easier to follow - my brain thanks you, hahahah 🤣