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

[Fix] [Synonyms UI] Fix infinite loading when permissions missing #211530

Merged
merged 3 commits into from
Feb 21, 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
@@ -0,0 +1,51 @@
/*
* 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 { EuiEmptyPrompt } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';

const ERROR_MESSAGES = {
generic: {
title: (
<FormattedMessage id="xpack.search.synonyms.errorTitle" defaultMessage="An error occurred" />
),
body: (
<FormattedMessage
id="xpack.search.synonyms.errorDescription"
defaultMessage="An error occured while fetching synonyms. Check Kibana logs for more information."
/>
),
},
missingPermissions: {
title: (
<FormattedMessage
id="xpack.search.synonyms.missingPermissionsTitle"
defaultMessage="Missing permissions"
/>
),
body: (
<FormattedMessage
id="xpack.search.synonyms.missingPermissionsDescription"
defaultMessage="You do not have the necessary permissions to manage synonyms. Contact your system administrator."
/>
),
},
};

export const ErrorPrompt: React.FC<{ errorType: 'missingPermissions' | 'generic' }> = ({
errorType,
}) => {
return (
<EuiEmptyPrompt
iconType="logoEnterpriseSearch"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're migrating away from this logo, so we may want to use logoElasticsearch instead.

title={<h2>{ERROR_MESSAGES[errorType].title}</h2>}
body={<p>{ERROR_MESSAGES[errorType].body}</p>}
/>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { render, screen } from '@testing-library/react';
import React from 'react';
import { SearchSynonymsOverview } from './overview';
import { I18nProvider } from '@kbn/i18n-react';
import { useFetchSynonymsSets } from '../../hooks/use_fetch_synonyms_sets';

jest.mock('../../hooks/use_fetch_synonyms_sets', () => ({
useFetchSynonymsSets: jest.fn(() => ({
data: undefined,
isLoading: false,
isError: true,
error: { body: { statusCode: 500 } },
})),
}));

describe('Search Synonyms Overview', () => {
const queryClient = new QueryClient();
const Wrapper = ({ children }: { children?: React.ReactNode }) => (
<I18nProvider>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</I18nProvider>
);
it('should show error prompt when we get a generic error', () => {
render(
<Wrapper>
<SearchSynonymsOverview />
</Wrapper>
);

expect(screen.getByText('An error occurred')).toBeInTheDocument();
});

it('should show error prompt when we get a missing permissions error', () => {
(useFetchSynonymsSets as jest.Mock).mockReturnValue({
data: undefined,
isLoading: false,
isError: true,
error: { body: { statusCode: 403 } },
});

render(
<Wrapper>
<SearchSynonymsOverview />
</Wrapper>
);

expect(screen.getByText('Missing permissions')).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ import { SynonymSets } from '../synonym_sets/synonym_sets';
import { useFetchSynonymsSets } from '../../hooks/use_fetch_synonyms_sets';
import { EmptyPrompt } from '../empty_prompt/empty_prompt';
import { CreateSynonymsSetModal } from '../synonym_sets/create_new_set_modal';
import { ErrorPrompt } from '../error_prompt/error_prompt';
import { isPermissionError } from '../../utils/synonyms_utils';

export const SearchSynonymsOverview = () => {
const {
services: { console: consolePlugin, history, searchNavigation },
} = useKibana();
const { data: synonymsData, isInitialLoading } = useFetchSynonymsSets();
const { data: synonymsData, isInitialLoading, isError, error } = useFetchSynonymsSets();
const [isCreateModalVisible, setIsCreateModalVisible] = useState(false);

const embeddableConsole = useMemo(
Expand All @@ -44,50 +46,52 @@ export const SearchSynonymsOverview = () => {
solutionNav={searchNavigation?.useClassicNavigation(history)}
color="primary"
>
<KibanaPageTemplate.Header
pageTitle="Synonyms"
restrictWidth
color="primary"
rightSideItems={[
<EuiFlexGroup alignItems="center">
<EuiFlexItem grow={false}>
<EuiLink
data-test-subj="searchSynonymsSearchSynonymsOverviewApiDocumentationLink"
external
target="_blank"
href={docLinks.synonymsApi}
>
<FormattedMessage
id="xpack.searchSynonyms.synonymsSetDetail.documentationLink"
defaultMessage="API Documentation"
/>
</EuiLink>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButton
data-test-subj="searchSynonymsSearchSynonymsOverviewCreateButton"
fill
iconType="plusInCircle"
onClick={() => {
setIsCreateModalVisible(true);
}}
>
<FormattedMessage
id="xpack.searchSynonyms.synonymsSetDetail.createButton"
defaultMessage="Create"
/>
</EuiButton>
</EuiFlexItem>
</EuiFlexGroup>,
]}
>
<EuiText>
<FormattedMessage
id="xpack.searchSynonyms.synonymsSetDetail.description"
defaultMessage="Create and manage synonym sets and synonym rules."
/>
</EuiText>
</KibanaPageTemplate.Header>
{synonymsData && !isInitialLoading && !isError && (
<KibanaPageTemplate.Header
pageTitle="Synonyms"
restrictWidth
color="primary"
rightSideItems={[
<EuiFlexGroup alignItems="center">
<EuiFlexItem grow={false}>
<EuiLink
data-test-subj="searchSynonymsSearchSynonymsOverviewApiDocumentationLink"
external
target="_blank"
href={docLinks.synonymsApi}
>
<FormattedMessage
id="xpack.searchSynonyms.synonymsSetDetail.documentationLink"
defaultMessage="API Documentation"
/>
</EuiLink>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiButton
data-test-subj="searchSynonymsSearchSynonymsOverviewCreateButton"
fill
iconType="plusInCircle"
onClick={() => {
setIsCreateModalVisible(true);
}}
>
<FormattedMessage
id="xpack.searchSynonyms.synonymsSetDetail.createButton"
defaultMessage="Create"
/>
</EuiButton>
</EuiFlexItem>
</EuiFlexGroup>,
]}
>
<EuiText>
<FormattedMessage
id="xpack.searchSynonyms.synonymsSetDetail.description"
defaultMessage="Create and manage synonym sets and synonym rules."
/>
</EuiText>
</KibanaPageTemplate.Header>
)}
<KibanaPageTemplate.Section restrictWidth>
{isCreateModalVisible && (
<CreateSynonymsSetModal
Expand All @@ -97,6 +101,9 @@ export const SearchSynonymsOverview = () => {
/>
)}
{isInitialLoading && <EuiLoadingSpinner />}
{isError && (
<ErrorPrompt errorType={isPermissionError(error) ? 'missingPermissions' : 'generic'} />
)}

{!isInitialLoading && synonymsData && synonymsData._meta.totalItemCount > 0 && (
<SynonymSets />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { useQuery } from '@tanstack/react-query';
import type { SynonymsGetSynonymsSetsSynonymsSetItem } from '@elastic/elasticsearch/lib/api/types';
import { KibanaServerError } from '@kbn/kibana-utils-plugin/common';
import { SYNONYMS_SETS_QUERY_KEY } from '../../common/constants';
import { DEFAULT_PAGE_VALUE, Page, Paginate } from '../../common/pagination';
import { APIRoutes } from '../../common/api_routes';
Expand All @@ -16,7 +17,7 @@ export const useFetchSynonymsSets = (page: Page = DEFAULT_PAGE_VALUE) => {
const {
services: { http },
} = useKibana();
return useQuery({
return useQuery<Paginate<SynonymsGetSynonymsSetsSynonymsSetItem>, { body: KibanaServerError }>({
queryKey: [SYNONYMS_SETS_QUERY_KEY, page.from, page.size],
queryFn: async () => {
return await http.get<Paginate<SynonymsGetSynonymsSetsSynonymsSetItem>>(
Expand All @@ -26,5 +27,7 @@ export const useFetchSynonymsSets = (page: Page = DEFAULT_PAGE_VALUE) => {
}
);
},
refetchOnWindowFocus: false,
retry: false,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { EuiComboBoxOptionOption } from '@elastic/eui';
import { KibanaServerError } from '@kbn/kibana-utils-plugin/common';

export const isExplicitSynonym = (synonym: string) => {
return synonym.trim().includes('=>');
Expand Down Expand Up @@ -41,3 +42,7 @@ export const synonymsOptionToString = ({
`${fromTerms.map((s) => s.label).join(',')}${
isExplicit ? ' => ' + toTerms.map((s) => s.label).join(',') : ''
}`;

export const isPermissionError = (error: { body: KibanaServerError }) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this function being used anywhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used in overview.tsx to change the error type for the error prompt component.

return error.body.statusCode === 403;
};