-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cases] Attach framework registry (#134744)
* Create external reference attachment registry * Pass externalReferenceAttachmentTypeRegistry to cases client * Better types * Show external references user action * Handle unregistered events * Add e2e tests * Fixe fixture plugin naming * Add cases fixture plugin to tsconfig * Fix types * Improvements * Fix types * Fixes * Fix bug * Add unit test
- Loading branch information
Showing
41 changed files
with
974 additions
and
50 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
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
11 changes: 11 additions & 0 deletions
11
x-pack/plugins/cases/public/client/attachment_framework/external_reference_registry.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,11 @@ | ||
/* | ||
* 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 { AttachmentTypeRegistry } from './registry'; | ||
import { ExternalReferenceAttachmentType } from './types'; | ||
|
||
export class ExternalReferenceAttachmentTypeRegistry extends AttachmentTypeRegistry<ExternalReferenceAttachmentType> {} |
86 changes: 86 additions & 0 deletions
86
x-pack/plugins/cases/public/client/attachment_framework/registry.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,86 @@ | ||
/* | ||
* 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 { AttachmentTypeRegistry } from './registry'; | ||
|
||
export const ExpressionComponent: React.FunctionComponent = () => { | ||
return null; | ||
}; | ||
|
||
const getItem = (id: string = 'test') => { | ||
return { id }; | ||
}; | ||
|
||
describe('AttachmentTypeRegistry', () => { | ||
beforeEach(() => jest.resetAllMocks()); | ||
|
||
describe('has()', () => { | ||
it('returns false for unregistered items', () => { | ||
const registry = new AttachmentTypeRegistry(); | ||
|
||
expect(registry.has('test')).toEqual(false); | ||
}); | ||
|
||
it('returns true after registering an item', () => { | ||
const registry = new AttachmentTypeRegistry(); | ||
registry.register(getItem()); | ||
|
||
expect(registry.has('test')); | ||
}); | ||
}); | ||
|
||
describe('register()', () => { | ||
it('able to register items', () => { | ||
const registry = new AttachmentTypeRegistry(); | ||
registry.register(getItem()); | ||
|
||
expect(registry.has('test')).toEqual(true); | ||
}); | ||
|
||
it('throws error if item is already registered', () => { | ||
const registry = new AttachmentTypeRegistry(); | ||
registry.register(getItem('test')); | ||
|
||
expect(() => registry.register(getItem('test'))).toThrowErrorMatchingInlineSnapshot( | ||
`"Attachment type \\"test\\" is already registered."` | ||
); | ||
}); | ||
}); | ||
|
||
describe('get()', () => { | ||
it('returns item', () => { | ||
const registry = new AttachmentTypeRegistry(); | ||
registry.register(getItem()); | ||
const actionType = registry.get('test'); | ||
|
||
expect(actionType).toEqual({ | ||
id: 'test', | ||
}); | ||
}); | ||
|
||
it(`throw error when action type doesn't exist`, () => { | ||
const registry = new AttachmentTypeRegistry(); | ||
expect(() => registry.get('not-exist-item')).toThrowErrorMatchingInlineSnapshot( | ||
`"Attachment type \\"not-exist-item\\" is not registered."` | ||
); | ||
}); | ||
}); | ||
|
||
describe('list()', () => { | ||
it('returns list of items', () => { | ||
const actionTypeRegistry = new AttachmentTypeRegistry(); | ||
actionTypeRegistry.register(getItem()); | ||
const actionTypes = actionTypeRegistry.list(); | ||
|
||
expect(actionTypes).toEqual([ | ||
{ | ||
id: 'test', | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
65 changes: 65 additions & 0 deletions
65
x-pack/plugins/cases/public/client/attachment_framework/registry.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,65 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
|
||
interface BaseAttachmentType { | ||
id: string; | ||
} | ||
|
||
export class AttachmentTypeRegistry<T extends BaseAttachmentType> { | ||
private readonly attachmentTypes: Map<string, T> = new Map(); | ||
|
||
/** | ||
* Returns true if the attachment type registry has the given type registered | ||
*/ | ||
public has(id: string) { | ||
return this.attachmentTypes.has(id); | ||
} | ||
|
||
/** | ||
* Registers an attachment type to the type registry | ||
*/ | ||
public register(attachmentType: T) { | ||
if (this.has(attachmentType.id)) { | ||
throw new Error( | ||
i18n.translate('xpack.cases.typeRegistry.register.duplicateAttachmentTypeErrorMessage', { | ||
defaultMessage: 'Attachment type "{id}" is already registered.', | ||
values: { | ||
id: attachmentType.id, | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
this.attachmentTypes.set(attachmentType.id, attachmentType); | ||
} | ||
|
||
/** | ||
* Returns an attachment type, throw error if not registered | ||
*/ | ||
public get(id: string): T { | ||
const attachmentType = this.attachmentTypes.get(id); | ||
|
||
if (!attachmentType) { | ||
throw new Error( | ||
i18n.translate('xpack.cases.typeRegistry.get.missingActionTypeErrorMessage', { | ||
defaultMessage: 'Attachment type "{id}" is not registered.', | ||
values: { | ||
id, | ||
}, | ||
}) | ||
); | ||
} | ||
|
||
return attachmentType; | ||
} | ||
|
||
public list() { | ||
return Array.from(this.attachmentTypes).map(([id, attachmentType]) => attachmentType); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
x-pack/plugins/cases/public/client/attachment_framework/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,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; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type React from 'react'; | ||
import { EuiCommentProps, IconType } from '@elastic/eui'; | ||
import { CommentRequestExternalReferenceType } from '../../../common/api'; | ||
import { Case } from '../../containers/types'; | ||
|
||
export interface ExternalReferenceAttachmentViewObject { | ||
type?: EuiCommentProps['type']; | ||
timelineIcon?: EuiCommentProps['timelineIcon']; | ||
actions?: EuiCommentProps['actions']; | ||
event?: EuiCommentProps['event']; | ||
children?: React.LazyExoticComponent<React.FC>; | ||
} | ||
|
||
export interface ExternalReferenceAttachmentViewProps { | ||
externalReferenceId: CommentRequestExternalReferenceType['externalReferenceId']; | ||
externalReferenceMetadata: CommentRequestExternalReferenceType['externalReferenceMetadata']; | ||
caseData: Pick<Case, 'id' | 'title'>; | ||
} | ||
|
||
export interface ExternalReferenceAttachmentType { | ||
id: string; | ||
icon: IconType; | ||
displayName: string; | ||
getAttachmentViewObject: ( | ||
props: ExternalReferenceAttachmentViewProps | ||
) => ExternalReferenceAttachmentViewObject; | ||
} | ||
|
||
export interface AttachmentFramework { | ||
registerExternalReference: ( | ||
externalReferenceAttachmentType: ExternalReferenceAttachmentType | ||
) => void; | ||
} |
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.