-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[settings] Extract and fix Section Registry #163502
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
Merged
clintandrewhall
merged 3 commits into
elastic:main
from
clintandrewhall:advanced_settings/registry
Aug 14, 2023
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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
56 changes: 56 additions & 0 deletions
56
packages/kbn-management/settings/section_registry/README.mdx
This file contains hidden or 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,56 @@ | ||
| --- | ||
| id: kbn-management/settings/SectionRegistry | ||
| slug: /kbn-management/settings/section-registry/ | ||
| title: Section Registry | ||
| description: A registry which allows a consumer to add sections to Advanced Settings. | ||
| tags: ['management', 'settings'] | ||
| date: 2023-08-04 | ||
| --- | ||
|
|
||
| This registry is fairly straightforward: it allows a consumer to add a section to the Advanced Settings page. This registry would be consumed by a plugin and exposed on the `start` and `setup` contracts: | ||
|
|
||
| ```ts | ||
|
|
||
| const registry = new SectionRegistry(); | ||
|
|
||
| export class PluginBar | ||
| implements Plugin<PluginBarSetup, PluginBarStart, PluginBarSetupDeps, PluginBarStartDeps> | ||
| { | ||
| public setup( | ||
| _core: CoreSetup, | ||
| _setupDeps: PluginFooSetupDeps | ||
| ) { | ||
| return { | ||
| sectionRegistry: sectionRegistry.setup, | ||
| }; | ||
| } | ||
|
|
||
| public start( | ||
| _core: CoreStart, | ||
| _startDeps: PluginFooStartDeps | ||
| ) { | ||
| return { | ||
| sectionRegistry: sectionRegistry.start, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| export class PluginFoo | ||
| implements Plugin<PluginFooSetup, PluginFooStart, PluginFooSetupDeps, PluginFooStartDeps> | ||
| { | ||
| public setup( | ||
| core: CoreSetup, | ||
| { pluginBar: { sectionRegistry } }: PluginFooSetupDeps | ||
| ) { | ||
| const Component = (props: RegistryComponentProps) => <SomeComponent {...props} />; | ||
|
|
||
| const queryMatch = (query: string) => { | ||
| const searchTerm = query.toLowerCase(); | ||
| return SEARCH_TERMS.some((term) => term.indexOf(searchTerm) >= 0); | ||
| }; | ||
|
|
||
| sectionRegistry.setup.addGlobalSection(Component, queryMatch); | ||
| } | ||
| } | ||
|
|
||
| ``` |
This file contains hidden or 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
19 changes: 19 additions & 0 deletions
19
packages/kbn-management/settings/section_registry/jest.config.js
This file contains hidden or 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,19 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| module.exports = { | ||
| preset: '@kbn/test', | ||
| rootDir: '../../../..', | ||
| roots: ['<rootDir>/packages/kbn-management/settings/section_registry'], | ||
| coverageDirectory: | ||
| '<rootDir>/target/kibana-coverage/jest/packages/kbn-management/settings/section_registry', | ||
| coverageReporters: ['text', 'html'], | ||
| collectCoverageFrom: [ | ||
| '<rootDir>/packages/kbn-management/settings/section_registry/**/*.{ts,tsx}', | ||
| ], | ||
| }; |
5 changes: 5 additions & 0 deletions
5
packages/kbn-management/settings/section_registry/kibana.jsonc
This file contains hidden or 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,5 @@ | ||
| { | ||
| "type": "shared-common", | ||
| "id": "@kbn/management-settings-section-registry", | ||
| "owner": "@elastic/appex-sharedux @elastic/platform-deployment-management" | ||
| } |
6 changes: 6 additions & 0 deletions
6
packages/kbn-management/settings/section_registry/package.json
This file contains hidden or 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,6 @@ | ||
| { | ||
| "name": "@kbn/management-settings-section-registry", | ||
| "private": true, | ||
| "version": "1.0.0", | ||
| "license": "SSPL-1.0 OR Elastic License 2.0" | ||
| } |
44 changes: 44 additions & 0 deletions
44
packages/kbn-management/settings/section_registry/section_registry.test.tsx
This file contains hidden or 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,44 @@ | ||
| /* | ||
| * 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 { SectionRegistry } from './section_registry'; | ||
|
|
||
| describe('SectionRegistry', () => { | ||
| let registry = new SectionRegistry(); | ||
|
|
||
| beforeEach(() => { | ||
| registry = new SectionRegistry(); | ||
| }); | ||
|
|
||
| describe('register', () => { | ||
| it('should allow a global component to be registered', () => { | ||
| const Component = () => <div />; | ||
| const queryMatch = () => true; | ||
| registry.setup.addGlobalSection(Component, queryMatch); | ||
|
|
||
| const entries = registry.start.getGlobalSections(); | ||
| expect(entries).toHaveLength(1); | ||
| expect(entries[0].Component).toBe(Component); | ||
| expect(entries[0].queryMatch).toBe(queryMatch); | ||
| expect(registry.start.getSpacesSections()).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('should allow a spaces component to be registered', () => { | ||
| const Component = () => <div />; | ||
| const queryMatch = () => true; | ||
| registry.setup.addSpaceSection(Component, queryMatch); | ||
|
|
||
| const entries = registry.start.getSpacesSections(); | ||
| expect(entries).toHaveLength(1); | ||
| expect(entries[0].Component).toBe(Component); | ||
| expect(entries[0].queryMatch).toBe(queryMatch); | ||
| expect(registry.start.getGlobalSections()).toHaveLength(0); | ||
| }); | ||
| }); | ||
| }); |
90 changes: 90 additions & 0 deletions
90
packages/kbn-management/settings/section_registry/section_registry.ts
This file contains hidden or 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,90 @@ | ||
| /* | ||
| * 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 type { ComponentType } from 'react'; | ||
| import { ToastsStart } from '@kbn/core-notifications-browser'; | ||
| import { UiSettingsScope } from '@kbn/core-ui-settings-common'; | ||
|
|
||
| /** | ||
| * Props provided to a `RegistryComponent`. | ||
| */ | ||
| export interface RegistryComponentProps { | ||
| toasts: ToastsStart; | ||
| enableSaving: Record<UiSettingsScope, boolean>; | ||
| } | ||
|
|
||
| /** | ||
| * A registry entry for a section. | ||
| */ | ||
| export interface RegistryEntry { | ||
| Component: RegistryComponent; | ||
| queryMatch: QueryMatchFn; | ||
| } | ||
|
|
||
| type RegistryComponent = ComponentType<RegistryComponentProps>; | ||
| type PageType = 'space' | 'global'; | ||
| type QueryMatchFn = (term: string) => boolean; | ||
| type Registry = Record<PageType, RegistryEntry[]>; | ||
|
|
||
| /** | ||
| * A registry of sections to add to pages within Advanced Settings. | ||
| */ | ||
| export class SectionRegistry { | ||
|
clintandrewhall marked this conversation as resolved.
Outdated
|
||
| private registry: Registry = { | ||
| space: [], | ||
| global: [], | ||
| }; | ||
|
|
||
| setup = { | ||
| /** | ||
| * Registers a section within the "Space" page. | ||
| * | ||
| * @param Component - A React component to render. | ||
| * @param queryMatch - A function that, given a search term, returns true if the section should be rendered. | ||
| */ | ||
| addSpaceSection: (Component: RegistryComponent, queryMatch: QueryMatchFn) => { | ||
| this.registry.space.push({ Component, queryMatch }); | ||
| }, | ||
|
|
||
| /** | ||
| * Registers a section within the "Global" page. | ||
| * | ||
| * @param Component - A React component to render. | ||
| * @param queryMatch - A function that, given a search term, returns true if the section should be rendered. | ||
| */ | ||
| addGlobalSection: (Component: RegistryComponent, queryMatch: QueryMatchFn) => { | ||
| this.registry.global.push({ Component, queryMatch }); | ||
| }, | ||
| }; | ||
|
|
||
| start = { | ||
| /** | ||
| * Retrieve components registered for the "Space" page. | ||
| */ | ||
| getGlobalSections: (): RegistryEntry[] => { | ||
| return this.registry.global; | ||
| }, | ||
|
|
||
| /** | ||
| * Retrieve components registered for the "Global" page. | ||
| */ | ||
| getSpacesSections: (): RegistryEntry[] => { | ||
| return this.registry.space; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * The `setup` contract provided by a `SectionRegistry`. | ||
| */ | ||
| export type SectionRegistrySetup = SectionRegistry['setup']; | ||
|
clintandrewhall marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * The `start` contract provided by a `SectionRegistry`. | ||
| */ | ||
| export type SectionRegistryStart = SectionRegistry['start']; | ||
22 changes: 22 additions & 0 deletions
22
packages/kbn-management/settings/section_registry/tsconfig.json
This file contains hidden or 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,22 @@ | ||
| { | ||
| "extends": "../../../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "target/types", | ||
| "types": [ | ||
| "jest", | ||
| "node", | ||
| "react" | ||
| ] | ||
| }, | ||
| "include": [ | ||
| "**/*.ts", | ||
| "**/*.tsx", | ||
| ], | ||
| "exclude": [ | ||
| "target/**/*" | ||
| ], | ||
| "kbn_references": [ | ||
| "@kbn/core-notifications-browser", | ||
| "@kbn/core-ui-settings-common", | ||
| ] | ||
| } |
This file contains hidden or 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
3 changes: 0 additions & 3 deletions
3
...dvanced_settings/public/component_registry/__snapshots__/component_registry.test.tsx.snap
This file was deleted.
Oops, something went wrong.
79 changes: 0 additions & 79 deletions
79
src/plugins/advanced_settings/public/component_registry/component_registry.test.tsx
This file was deleted.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.