-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Log Explorer][Discover] Use DocViewsRegistry to add flyout Overview …
…tab (#172364) ## 📓 Summary Closes #171716 To apply a new customization that allows consumers of the `DiscoverContainer` to apply ad-hoc flyout tabs, this work refactors and extends the current implementation of the `DocViewsRegistry` and enhances the `flyout` extension point providing a new instance of the base registry for customizations to be applied. Mainly, the changes consist of: ### DocViewsRegistry - Add a required `id` property when adding a `DocView`. Earlier the identifier generation was purely based on the order on the list, with this addition we now rely on unique strings for each doc view. This `id` is also used to store and potentially remove the doc view. - Move the sorting step when adding a new doc view. Previously the sorting was performed on each retrieval, moving this operation at insertion time we can only perform this once. - Rename `addDocView` to `add`. In the context of the `DocViewsRegistry` this name reflects better some native API names such as Set, Map etc. - Add the `removeById` method. It will allow when necessary to remove any doc view that matches the passed id. - Add the `clone` method. Since we don't want to always mutate a registry but we still want to use it as a base, we can clone it and generate a new instance with this method. Used for generating a new registry used by any customization profile. - Removed filtering by the `shouldShow` property. Apart from not having any consumer using this option, we agreed offline that is not ideal to perform filtering on the doc views. ### UnifiedDocViewer plugin - The `UnifiedDocViewer`component now accepts an optional `docViewsRegistry` property. This can either be an instance of `DocViewsRegistry` or a function that will provide a new copy of the default registry created and exposed by the plugin. This is important as we don't want consumers to update the base registry but work on a clone that applies changes only for the current usage. When passed, this registry will be used as a docViews source for the `UnifiedDocViewer` component. When instead is not passed, the source will be the default registry instance. - The plugin contract now exposes the whole registry instance instead of the previous `addDocView ' method. This is necessary so that consumers can clone this registry and work on the new instance using all the features encapsulated in the `DocViewsRegistry` implementation, like in the case described above. ### LogExplorer customization - We moved the customization for the flyout content into a new doc view, which is added to the Discover UnifiedDocViewer only for this app through a new parameter on the `flyout` extension point, the `docViewerRegistry` function which will be forwarded to the UnifiedDocViewer component, following the logic described earlier in this PR. **Example** ```ts customizations.set({ id: 'flyout', docViewsRegistry: (registry) => { registry.add({ id: 'doc_view_log_overview', title: 'Overview', order: 0, component: (props) => { return <FlyoutContent {...props} />; }, }); return registry; }, }); ``` ## 🎥 Demo https://github.com/elastic/kibana/assets/34506779/894234cc-02e1-4880-949e-720f99793823 --------- Co-authored-by: Marco Antonio Ghiani <marcoantonio.ghiani@elastic.co> Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
d22560e
commit b8a23eb
Showing
30 changed files
with
298 additions
and
142 deletions.
There are no files selected for viewing
18 changes: 9 additions & 9 deletions
18
...s/kbn-unified-doc-viewer/src/components/doc_viewer/__snapshots__/doc_viewer.test.tsx.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
89 changes: 89 additions & 0 deletions
89
packages/kbn-unified-doc-viewer/src/services/doc_views_registry.test.tsx
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,89 @@ | ||
/* | ||
* 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 React from 'react'; | ||
import { DocViewsRegistry } from './doc_views_registry'; | ||
|
||
const fnDocView = { | ||
id: 'function-doc-view', | ||
order: 10, | ||
title: 'Render function', | ||
render: jest.fn(), | ||
}; | ||
const componentDocView = { | ||
id: 'component-doc-view', | ||
order: 20, | ||
title: 'React component', | ||
component: () => <div>test</div>, | ||
}; | ||
|
||
describe('DocViewerRegistry', () => { | ||
test('can be initialized from an array of doc views', () => { | ||
const registry = new DocViewsRegistry([fnDocView, componentDocView]); | ||
|
||
expect(registry.getAll()).toHaveLength(2); | ||
}); | ||
|
||
test('can be initialized from another DocViewsRegistry instance', () => { | ||
const registry = new DocViewsRegistry([fnDocView, componentDocView]); | ||
const newRegistry = new DocViewsRegistry(registry); | ||
|
||
expect(registry.getAll()).toHaveLength(2); | ||
expect(newRegistry.getAll()).toHaveLength(2); | ||
expect(registry).not.toBe(newRegistry); | ||
}); | ||
|
||
describe('#add', () => { | ||
test('should add a doc view to the registry in the correct order', () => { | ||
const registry = new DocViewsRegistry([componentDocView]); | ||
|
||
registry.add(fnDocView); | ||
|
||
const docViews = registry.getAll(); | ||
|
||
expect(docViews[0]).toHaveProperty('id', 'function-doc-view'); | ||
expect(docViews[1]).toHaveProperty('id', 'component-doc-view'); | ||
}); | ||
|
||
test('should throw an error when the passed doc view already exists for the given id', () => { | ||
const registry = new DocViewsRegistry([fnDocView]); | ||
|
||
expect(() => registry.add(fnDocView)).toThrow( | ||
'DocViewsRegistry#add: a DocView is already registered with id "function-doc-view".' | ||
); | ||
}); | ||
}); | ||
|
||
describe('#removeById', () => { | ||
test('should remove a doc view given the passed id', () => { | ||
const registry = new DocViewsRegistry([fnDocView, componentDocView]); | ||
|
||
const docViews = registry.getAll(); | ||
|
||
expect(docViews[0]).toHaveProperty('id', 'function-doc-view'); | ||
expect(docViews[1]).toHaveProperty('id', 'component-doc-view'); | ||
|
||
registry.removeById('function-doc-view'); | ||
|
||
expect(registry.getAll()[0]).toHaveProperty('id', 'component-doc-view'); | ||
}); | ||
}); | ||
|
||
describe('#clone', () => { | ||
test('should return a new DocViewRegistry instance starting from the current one', () => { | ||
const registry = new DocViewsRegistry([fnDocView, componentDocView]); | ||
|
||
const clonedRegistry = registry.clone(); | ||
const docViews = clonedRegistry.getAll(); | ||
|
||
expect(docViews[0]).toHaveProperty('id', 'function-doc-view'); | ||
expect(docViews[1]).toHaveProperty('id', 'component-doc-view'); | ||
expect(registry).not.toBe(clonedRegistry); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.