Skip to content
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

View request flyout #127156

Merged
merged 20 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 { ViewApiRequestFlyout } from './view_api_request_flyout';
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 { act } from 'react-dom/test-utils';
import { mountWithI18nProvider } from '@kbn/test-jest-helpers';
import { findTestSubject, takeMountedSnapshot } from '@elastic/eui/lib/test';
import { compressToEncodedURIComponent } from 'lz-string';

import { ViewApiRequestFlyout } from './view_api_request_flyout';
import type { UrlService } from 'src/plugins/share/common/url_service';

const payload = {
title: 'Test title',
description: 'Test description',
request: 'Hello world',
closeFlyout: jest.fn(),
};

const urlService = {
locators: {
get: jest.fn().mockReturnValue({
useUrl: jest.fn().mockImplementation((value) => {
return `devToolsUrl_${value?.loadFrom}`;
}),
}),
},
} as any as UrlService;

describe('ViewApiRequestFlyout', () => {
test('is rendered', () => {
const component = mountWithI18nProvider(<ViewApiRequestFlyout {...payload} />);
expect(takeMountedSnapshot(component)).toMatchSnapshot();
});

describe('props', () => {
test('on closeFlyout', async () => {
const component = mountWithI18nProvider(<ViewApiRequestFlyout {...payload} />);

await act(async () => {
findTestSubject(component, 'apiRequestFlyoutClose').simulate('click');
});

expect(payload.closeFlyout).toBeCalled();
});

test('doesnt have openInConsole when some optional props are not supplied', async () => {
const component = mountWithI18nProvider(
<ViewApiRequestFlyout {...payload} canShowDevtools />
);

const openInConsole = findTestSubject(component, 'apiRequestFlyoutOpenInConsoleButton');
expect(openInConsole.length).toEqual(0);
});

test('has openInConsole when all optional props are supplied', async () => {
const encodedRequest = compressToEncodedURIComponent(payload.request);
const component = mountWithI18nProvider(
<ViewApiRequestFlyout
{...payload}
canShowDevtools
navigateToUrl={jest.fn()}
urlService={urlService}
/>
);

const openInConsole = findTestSubject(component, 'apiRequestFlyoutOpenInConsoleButton');
expect(openInConsole.length).toEqual(1);
expect(openInConsole.props().href).toEqual(`devToolsUrl_data:text/plain,${encodedRequest}`);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* 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.
*/
// We want to allow both right-clicking to open in a new tab and clicking through
// the "Open in Console" link. We could use `RedirectAppLinks` at the top level
// but that inserts a div which messes up the layout of the flyout.
/* eslint-disable @elastic/eui/href-or-on-click */
sabarasaba marked this conversation as resolved.
Show resolved Hide resolved

import React, { useCallback } from 'react';
import { FormattedMessage } from '@kbn/i18n-react';
import { compressToEncodedURIComponent } from 'lz-string';

import {
EuiFlyout,
EuiFlyoutHeader,
EuiTitle,
EuiFlyoutBody,
EuiFlyoutFooter,
EuiButtonEmpty,
EuiText,
EuiSpacer,
EuiCodeBlock,
EuiFlexGroup,
EuiFlexItem,
EuiCopy,
} from '@elastic/eui';
import { ApplicationStart } from 'src/core/public';
import type { UrlService } from 'src/plugins/share/common/url_service';

interface ViewApiRequestFlyoutProps {
title: string;
description: string;
request: string;
closeFlyout: () => void;
navigateToUrl?: ApplicationStart['navigateToUrl'];
urlService?: UrlService;
canShowDevtools?: boolean;
}

export const ViewApiRequestFlyout: React.FunctionComponent<ViewApiRequestFlyoutProps> = ({
title,
description,
request,
closeFlyout,
navigateToUrl,
urlService,
canShowDevtools,
}) => {
const getUrlParams = undefined;
const devToolsDataUri = compressToEncodedURIComponent(request);

// Generate a console preview link if we have a valid locator
const consolePreviewLink = urlService?.locators.get('CONSOLE_APP_LOCATOR')?.useUrl(
{
loadFrom: `data:text/plain,${devToolsDataUri}`,
},
getUrlParams,
[request]
);

const consolePreviewClick = useCallback(
() => consolePreviewLink && navigateToUrl && navigateToUrl(consolePreviewLink),
[consolePreviewLink, navigateToUrl]
);

// Check if both the Dev Tools UI and the Console UI are enabled.
const shouldShowDevToolsLink = canShowDevtools && consolePreviewLink !== undefined;

return (
<EuiFlyout maxWidth={480} onClose={closeFlyout} data-test-subj="apiRequestFlyout">
sabarasaba marked this conversation as resolved.
Show resolved Hide resolved
<EuiFlyoutHeader>
<EuiTitle>
<h2 data-test-subj="apiRequestFlyoutTitle">{title}</h2>
</EuiTitle>
</EuiFlyoutHeader>

<EuiFlyoutBody>
<EuiText>
<p data-test-subj="apiRequestFlyoutDescription">{description}</p>
</EuiText>
<EuiSpacer />

<EuiFlexGroup
sabarasaba marked this conversation as resolved.
Show resolved Hide resolved
direction="column"
gutterSize="s"
wrap={false}
responsive={false}
className="insRequestCodeViewer"
sabarasaba marked this conversation as resolved.
Show resolved Hide resolved
>
<EuiFlexItem grow={false}>
<EuiSpacer size="s" />
<div className="eui-textRight">
<EuiCopy textToCopy={request}>
{(copy) => (
<EuiButtonEmpty
size="xs"
flush="right"
iconType="copyClipboard"
onClick={copy}
data-test-subj="apiRequestFlyoutCopyClipboardButton"
>
<FormattedMessage
id="esUi.viewApiRequest.copyToClipboardButton"
defaultMessage="Copy to clipboard"
/>
</EuiButtonEmpty>
)}
</EuiCopy>
{shouldShowDevToolsLink && (
<EuiButtonEmpty
size="xs"
flush="right"
iconType="wrench"
href={consolePreviewLink}
onClick={consolePreviewClick}
data-test-subj="apiRequestFlyoutOpenInConsoleButton"
>
<FormattedMessage
id="esUi.viewApiRequest.openInConsoleButton"
defaultMessage="Open in Console"
/>
</EuiButtonEmpty>
)}
</div>
</EuiFlexItem>
<EuiFlexItem grow={true}>
<EuiCodeBlock language="json" data-test-subj="apiRequestFlyoutBody">
{request}
</EuiCodeBlock>
</EuiFlexItem>
</EuiFlexGroup>
</EuiFlyoutBody>

<EuiFlyoutFooter>
<EuiButtonEmpty
iconType="cross"
onClick={closeFlyout}
flush="left"
data-test-subj="apiRequestFlyoutClose"
>
<FormattedMessage id="esUi.viewApiRequest.closeButtonLabel" defaultMessage="Close" />
</EuiButtonEmpty>
</EuiFlyoutFooter>
</EuiFlyout>
);
};
1 change: 1 addition & 0 deletions src/plugins/es_ui_shared/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type { EuiCodeEditorProps } from './components/code_editor';
export { EuiCodeEditor } from './components/code_editor';
export type { Frequency } from './components/cron_editor';
export { CronEditor } from './components/cron_editor';
export { ViewApiRequestFlyout } from './components/view_api_request_flyout';

export type {
SendRequestConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export type PipelineFormTestSubjects =
| 'onFailureEditor'
| 'testPipelineButton'
| 'showRequestLink'
| 'requestFlyout'
| 'requestFlyout.title'
| 'apiRequestFlyout'
| 'apiRequestFlyout.apiRequestFlyoutTitle'
| 'testPipelineFlyout'
| 'testPipelineFlyout.title'
| 'documentationLink';
Loading