-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[EDR Workflows][Artifact transfer 2] Import UI #247976
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
gergoabraham
merged 13 commits into
elastic:main
from
gergoabraham:artifact-transfer-2-import-ui
Mar 3, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e2cf5fc
[ui] add import flyout
gergoabraham 9b3980d
[ui] a bit less verbose import button
gergoabraham 9dc24a6
[ui] add Import button on empty list screen
gergoabraham d0d7271
[ui] display Import flyout in the url
gergoabraham 993a05a
[ui] add Import button to policy details artifact tab empty pages
gergoabraham 6e932dd
Merge branch 'main' into artifact-transfer-2-import-ui
gergoabraham 23612b1
Merge branch 'main' into artifact-transfer-2-import-ui
gergoabraham f1ee4a2
Merge branch 'main' into artifact-transfer-2-import-ui
gergoabraham b449a72
Merge branch 'main' into artifact-transfer-2-import-ui
gergoabraham f030325
remove unnecessary useCallback memoization
gergoabraham adc6f79
add more detailed success message
gergoabraham f0ec59d
add text to import flyout
gergoabraham c2432cd
Merge branch 'main' into artifact-transfer-2-import-ui
gergoabraham 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
144 changes: 144 additions & 0 deletions
144
...ublic/management/components/artifact_list_page/components/artifact_import_flyout.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,144 @@ | ||
| /* | ||
| * 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 React from 'react'; | ||
| import type { ArtifactListPageProps } from '../artifact_list_page'; | ||
| import { waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import type { HttpFetchOptionsWithPath } from '@kbn/core/public'; | ||
| import { | ||
| createAppRootMockRenderer, | ||
| type AppContextTestRender, | ||
| } from '../../../../common/mock/endpoint'; | ||
| import { trustedAppsAllHttpMocks } from '../../../mocks'; | ||
| import { getDeferred } from '../../../mocks/utils'; | ||
| import { TrustedAppsApiClient } from '../../../pages/trusted_apps/service'; | ||
| import type { ArtifactImportFlyoutProps } from './artifact_import_flyout'; | ||
| import { ArtifactImportFlyout } from './artifact_import_flyout'; | ||
| import { artifactListPageLabels } from '../translations'; | ||
| import { getArtifactImportFlyoutUiMocks } from '../mocks'; | ||
|
|
||
| describe('When the flyout is opened in the ArtifactListPage component', () => { | ||
| let render: ( | ||
| props?: Partial<ArtifactListPageProps> | ||
| ) => Promise<ReturnType<AppContextTestRender['render']>>; | ||
| let renderResult: ReturnType<AppContextTestRender['render']>; | ||
| let coreStart: AppContextTestRender['coreStart']; | ||
| let mockedTrustedAppApi: ReturnType<typeof trustedAppsAllHttpMocks>; | ||
| let props: ArtifactImportFlyoutProps; | ||
| let ui: ReturnType<typeof getArtifactImportFlyoutUiMocks>; | ||
|
|
||
| beforeEach(() => { | ||
| const mockedContext = createAppRootMockRenderer(); | ||
| ({ coreStart } = mockedContext); | ||
|
|
||
| mockedTrustedAppApi = trustedAppsAllHttpMocks(coreStart.http); | ||
|
|
||
| props = { | ||
| labels: artifactListPageLabels, | ||
| apiClient: new TrustedAppsApiClient(coreStart.http), | ||
| onCancel: jest.fn(), | ||
| onSuccess: jest.fn(), | ||
| }; | ||
|
|
||
| render = async () => { | ||
| renderResult = mockedContext.render( | ||
| <ArtifactImportFlyout {...props} data-test-subj="testFlyout" /> | ||
| ); | ||
| ui = getArtifactImportFlyoutUiMocks(renderResult, 'testFlyout'); | ||
|
|
||
| await waitFor(async () => expect(renderResult.getByTestId('testFlyout')).toBeInTheDocument()); | ||
|
|
||
| return renderResult; | ||
| }; | ||
| }); | ||
|
|
||
| it('should display `Cancel` button enabled', async () => { | ||
| await render(); | ||
|
|
||
| expect(ui.getCancelButton()).toBeEnabled(); | ||
| }); | ||
|
|
||
| it('should call onCancel when `Cancel` button is clicked', async () => { | ||
| await render(); | ||
|
|
||
| await userEvent.click(ui.getCancelButton()); | ||
|
|
||
| expect(props.onCancel).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should display `Import` button disabled', async () => { | ||
| await render(); | ||
|
|
||
| expect(ui.getImportButton()).toBeDisabled(); | ||
| }); | ||
|
|
||
| it('should enable `Import` button when a file is selected', async () => { | ||
| await render(); | ||
|
|
||
| await ui.uploadFile(); | ||
|
|
||
| expect(ui.getImportButton()).toBeEnabled(); | ||
| }); | ||
|
|
||
| it('should call the import API when `Import` button is clicked', async () => { | ||
| await render(); | ||
|
|
||
| await ui.uploadFile(); | ||
| await userEvent.click(ui.getImportButton()); | ||
|
|
||
| expect(mockedTrustedAppApi.responseProvider.trustedAppImportList).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| version: '2023-10-31', | ||
| query: { overwrite: false } as HttpFetchOptionsWithPath['query'], | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('should disable `Import` button while the import is in progress', async () => { | ||
| const deferrable = getDeferred(); | ||
| mockedTrustedAppApi.responseProvider.trustedAppImportList.mockDelay.mockReturnValue( | ||
| deferrable.promise | ||
| ); | ||
|
|
||
| await render(); | ||
|
|
||
| await ui.uploadFile(); | ||
| await userEvent.click(ui.getImportButton()); | ||
|
|
||
| expect(ui.getImportButton()).toBeDisabled(); | ||
| }); | ||
|
|
||
| it('should show a success toast and call `onSuccess` after a successful import', async () => { | ||
| await render(); | ||
|
|
||
| await ui.uploadFile(); | ||
| await userEvent.click(ui.getImportButton()); | ||
|
|
||
| expect(coreStart.notifications.toasts.addSuccess).toHaveBeenCalledWith({ | ||
| text: '2 items imported', | ||
| title: 'Artifact list imported successfully', | ||
| }); | ||
| expect(props.onSuccess).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should show an error toast if the import API fails', async () => { | ||
| mockedTrustedAppApi.responseProvider.trustedAppImportList.mockImplementation(() => { | ||
| throw new Error('Import failed'); | ||
| }); | ||
|
|
||
| await render(); | ||
|
|
||
| await ui.uploadFile(); | ||
| await userEvent.click(ui.getImportButton()); | ||
|
|
||
| expect(coreStart.notifications.toasts.addError).toHaveBeenCalledWith( | ||
| expect.objectContaining(new Error('Import failed')), | ||
| { title: 'Artifact list import failed' } | ||
| ); | ||
| }); | ||
| }); |
135 changes: 135 additions & 0 deletions
135
...ion/public/management/components/artifact_list_page/components/artifact_import_flyout.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,135 @@ | ||
| /* | ||
| * 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 { EuiFilePickerProps } from '@elastic/eui'; | ||
| import { | ||
| EuiButton, | ||
| EuiButtonEmpty, | ||
| EuiFilePicker, | ||
| EuiFlexGroup, | ||
| EuiFlyout, | ||
| EuiFlyoutBody, | ||
| EuiFlyoutFooter, | ||
| EuiFlyoutHeader, | ||
| EuiSpacer, | ||
| EuiText, | ||
| EuiTitle, | ||
| useGeneratedHtmlId, | ||
| } from '@elastic/eui'; | ||
| import React, { useCallback } from 'react'; | ||
| import { useToasts } from '../../../../common/lib/kibana'; | ||
| import type { ArtifactListPageLabels } from '../translations'; | ||
| import { useImportArtifactList } from '../../../hooks/artifacts/use_import_artifact_list'; | ||
| import type { ExceptionsListApiClient } from '../../../services/exceptions_list/exceptions_list_api_client'; | ||
| import { useTestIdGenerator } from '../../../hooks/use_test_id_generator'; | ||
|
|
||
| export interface ArtifactImportFlyoutProps { | ||
| onCancel: () => void; | ||
| onSuccess: () => void; | ||
| apiClient: ExceptionsListApiClient; | ||
| labels: ArtifactListPageLabels; | ||
| 'data-test-subj'?: string; | ||
| } | ||
|
|
||
| export const ArtifactImportFlyout: React.FC<ArtifactImportFlyoutProps> = ({ | ||
| onCancel, | ||
| onSuccess, | ||
| apiClient, | ||
| labels, | ||
| 'data-test-subj': dataTestSubj = 'artifactImportFlyout', | ||
| }) => { | ||
| const toasts = useToasts(); | ||
| const getTestId = useTestIdGenerator(dataTestSubj); | ||
|
|
||
| const [file, setFile] = React.useState<File | null>(null); | ||
|
|
||
| const { isLoading, mutate } = useImportArtifactList(apiClient); | ||
|
|
||
| const handleOnSubmit = useCallback(() => { | ||
| if (file !== null) { | ||
| mutate( | ||
| { file }, | ||
| { | ||
| onError: (error) => { | ||
| toasts.addError(error, { title: labels.pageImportErrorToastTitle }); | ||
| }, | ||
| onSuccess: (response) => { | ||
| toasts.addSuccess({ | ||
| title: labels.pageImportSuccessToastTitle, | ||
| text: labels.getPageImportSuccessToastText?.( | ||
| response.success_count_exception_list_items | ||
| ), | ||
| }); | ||
| onSuccess(); | ||
| }, | ||
| } | ||
| ); | ||
| } | ||
| }, [file, labels, mutate, onSuccess, toasts]); | ||
|
|
||
| const handleOnFileChange: EuiFilePickerProps['onChange'] = useCallback( | ||
| (files: FileList | null) => { | ||
| if (files && files.length > 0) { | ||
| setFile(files[0]); | ||
| } else { | ||
| setFile(null); | ||
| } | ||
| }, | ||
| [] | ||
| ); | ||
|
|
||
| const importEndpointArtifactListFlyoutTitleId = useGeneratedHtmlId({ | ||
| prefix: 'importEndpointArtifactListFlyoutTitleId', | ||
| }); | ||
|
|
||
| return ( | ||
| <EuiFlyout | ||
| ownFocus | ||
| size="s" | ||
| onClose={onCancel} | ||
| aria-labelledby={importEndpointArtifactListFlyoutTitleId} | ||
| data-test-subj={getTestId()} | ||
| > | ||
| <EuiFlyoutHeader hasBorder> | ||
| <EuiTitle size="m"> | ||
| <h2 id={importEndpointArtifactListFlyoutTitleId}>{labels.pageImportButtonTitle}</h2> | ||
| </EuiTitle> | ||
| </EuiFlyoutHeader> | ||
|
|
||
| <EuiFlyoutBody> | ||
| <EuiText color="subdued" size="s"> | ||
| <p>{labels.importFlyoutDetails}</p> | ||
| </EuiText> | ||
|
|
||
| <EuiSpacer size="m" /> | ||
|
|
||
| <EuiFilePicker | ||
| onChange={handleOnFileChange} | ||
| disabled={isLoading} | ||
| data-test-subj={getTestId('filePicker')} | ||
| /> | ||
| </EuiFlyoutBody> | ||
|
|
||
| <EuiFlyoutFooter> | ||
| <EuiFlexGroup justifyContent="spaceBetween"> | ||
| <EuiButtonEmpty onClick={onCancel} data-test-subj={getTestId('cancelButton')}> | ||
| {labels.flyoutCancelButtonLabel} | ||
| </EuiButtonEmpty> | ||
|
|
||
| <EuiButton | ||
| onClick={handleOnSubmit} | ||
| disabled={file === null} | ||
| isLoading={isLoading} | ||
| data-test-subj={getTestId('importButton')} | ||
| > | ||
| {labels.importFlyoutImportSubmitButtonLabel} | ||
| </EuiButton> | ||
| </EuiFlexGroup> | ||
| </EuiFlyoutFooter> | ||
| </EuiFlyout> | ||
| ); | ||
| }; | ||
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.
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.
Is there a chance we can show more specific error from API?
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.
toasts.addError()actually shows themessageproperty of the passederror, so this should be okay. and also generic, as @ashokaditya noted in his review