Skip to content

Commit

Permalink
[Search] [Playground] Hide create index button when plugin not availa…
Browse files Browse the repository at this point in the history
…ble (elastic#209165)

## Summary

Hide create index button when elastisearch feature is disabled
  • Loading branch information
yansavitski authored Feb 3, 2025
1 parent 73d46c7 commit 78606e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ jest.mock('../../hooks/use_kibana', () => ({
application: {
navigateToUrl: jest.fn(),
},
share: {
url: {
locators: {
get: jest.fn().mockReturnValue(undefined),
},
chrome: {
navLinks: {
get: jest.fn().mockReturnValue(undefined),
},
},
},
Expand All @@ -38,28 +36,25 @@ const Wrapper: FC<PropsWithChildren<unknown>> = ({ children }) => {
};

describe('CreateIndexButton', () => {
it('renders correctly when there is no locator', async () => {
it('renders correctly when there is no link to indices', async () => {
const { queryByTestId } = render(<CreateIndexButton />, { wrapper: Wrapper });

expect(queryByTestId('createIndexButton')).not.toBeInTheDocument();
});

it('renders correctly when there is a locator', async () => {
it('renders correctly when navlink exists', async () => {
const navigateToUrl = jest.fn();

(useKibana as unknown as jest.Mock).mockImplementation(() => ({
services: {
application: {
navigateToUrl,
},
share: {
url: {
locators: {
get: jest.fn().mockReturnValue({
getUrl: jest.fn().mockReturnValue('mock-url'),
getRedirectUrl: jest.fn().mockReturnValue('mock-shown-url'),
}),
},
chrome: {
navLinks: {
get: jest.fn().mockReturnValue({
url: 'mock-url',
}),
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,43 @@
* 2.0.
*/

import React, { useCallback } from 'react';
import { EuiButton, EuiCallOut } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import React, { useCallback, useMemo } from 'react';
import { SEARCH_INDICES, SEARCH_INDICES_CREATE_INDEX } from '@kbn/deeplinks-search/constants';
import { i18n } from '@kbn/i18n';
import { useKibana } from '../../hooks/use_kibana';

export const CreateIndexButton: React.FC = () => {
const {
services: { application, share },
services: { application, chrome },
} = useKibana();

const createIndexLocator = useMemo(
() => share.url.locators.get('SEARCH_CREATE_INDEX'),
[share.url.locators]
);
const createIndexUrl = chrome.navLinks.get(
`${SEARCH_INDICES}:${SEARCH_INDICES_CREATE_INDEX}`
)?.url;

const handleCreateIndexClick = useCallback(
async (event: React.MouseEvent<HTMLAnchorElement>) => {
(event: React.MouseEvent<HTMLAnchorElement>) => {
event.preventDefault();

if (!createIndexLocator) {
if (!createIndexUrl) {
return;
}

const url = await createIndexLocator.getUrl({});
application?.navigateToUrl(url);
application?.navigateToUrl(createIndexUrl);
},
[application, createIndexLocator]
[application, createIndexUrl]
);

return createIndexLocator ? (
return createIndexUrl ? (
// eslint-disable-next-line @elastic/eui/href-or-on-click
<EuiButton
color="primary"
iconType="plusInCircle"
fill
data-test-subj="createIndexButton"
href={createIndexLocator.getRedirectUrl({})}
href={createIndexUrl}
onClick={handleCreateIndexClick}
>
<FormattedMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@kbn/alerts-ui-shared",
"@kbn/ui-actions-plugin",
"@kbn/file-upload-common",
"@kbn/deeplinks-search",
],
"exclude": [
"target/**/*",
Expand Down

0 comments on commit 78606e0

Please sign in to comment.