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

[9.0] [Search] [Playground] Hide create index button when plugin not available (#209165) #209313

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
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