Skip to content

Commit

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

# Backport

This will backport the following commits from `main` to `9.0`:
- [[Search] [Playground] Hide create index button when plugin not
available (#209165)](#209165)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Yan
Savitski","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-02-03T14:15:42Z","message":"[Search]
[Playground] Hide create index button when plugin not available
(#209165)\n\n## Summary\n\nHide create index button when elastisearch
feature is
disabled","sha":"78606e0fcff3594108830a9bbbdf0497d7dc4d4b","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:Search","backport:version","v9.1.0"],"title":"[Search]
[Playground] Hide create index button when plugin not
available","number":209165,"url":"https://github.com/elastic/kibana/pull/209165","mergeCommit":{"message":"[Search]
[Playground] Hide create index button when plugin not available
(#209165)\n\n## Summary\n\nHide create index button when elastisearch
feature is
disabled","sha":"78606e0fcff3594108830a9bbbdf0497d7dc4d4b"}},"sourceBranch":"main","suggestedTargetBranches":["9.0"],"targetPullRequestStates":[{"branch":"9.0","label":"v9.0.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/209165","number":209165,"mergeCommit":{"message":"[Search]
[Playground] Hide create index button when plugin not available
(#209165)\n\n## Summary\n\nHide create index button when elastisearch
feature is
disabled","sha":"78606e0fcff3594108830a9bbbdf0497d7dc4d4b"}}]}]
BACKPORT-->

Co-authored-by: Yan Savitski <[email protected]>
  • Loading branch information
kibanamachine and yansavitski authored Feb 3, 2025
1 parent 100ba11 commit e30388c
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 e30388c

Please sign in to comment.