Skip to content

Commit

Permalink
extract logic
Browse files Browse the repository at this point in the history
  • Loading branch information
markov00 committed Oct 10, 2024
1 parent 1b8d5f9 commit 18f4f3b
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from './get_application_user_messages';
import { cleanup, render, screen } from '@testing-library/react';
import { I18nProvider } from '@kbn/i18n-react';
import { getLongMessage } from '../user_messages_utils';

jest.mock('@kbn/shared-ux-link-redirect-app', () => {
const original = jest.requireActual('@kbn/shared-ux-link-redirect-app');
Expand Down Expand Up @@ -166,12 +167,7 @@ describe('application-level user messages', () => {
};
const firstMessage = getApplicationUserMessages({ ...props, ...propsOverrides }).at(0);
const rtlRender = render(
<I18nProvider>
{firstMessage &&
(typeof firstMessage.longMessage === 'function'
? firstMessage.longMessage()
: firstMessage.longMessage)}
</I18nProvider>
<I18nProvider>{firstMessage && getLongMessage(firstMessage)}</I18nProvider>
);
return rtlRender;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import { LayerSettingsPanel } from './layer_settings';
import { FormBasedLayer, LastValueIndexPatternColumn } from '../..';
import { filterAndSortUserMessages } from '../../app_plugin/get_application_user_messages';
import { EDITOR_INVALID_DIMENSION } from '../../user_messages_ids';
import { getLongMessage } from '../../user_messages_utils';
export type { OperationType, GenericIndexPatternColumn } from './operations';
export { deleteColumn } from './operations';

Expand Down Expand Up @@ -995,10 +996,7 @@ function getLayerErrorMessages(
defaultMessage="Layer {position} error: {wrappedMessage}"
values={{
position: index + 1,
wrappedMessage:
typeof error.longMessage === 'function'
? error.longMessage()
: error.longMessage,
wrappedMessage: getLongMessage(error),
}}
/>
),
Expand Down
17 changes: 4 additions & 13 deletions x-pack/plugins/lens/public/datasources/form_based/utils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { FramePublicAPI, IndexPattern } from '../../types';
import { TermsIndexPatternColumn } from './operations';
import { FormBasedLayer } from './types';
import { createMockedIndexPatternWithAdditionalFields } from './mocks';
import { getLongMessage } from '../../user_messages_utils';

describe('indexpattern_datasource utils', () => {
describe('getPrecisionErrorWarningMessages', () => {
Expand Down Expand Up @@ -125,11 +126,7 @@ describe('indexpattern_datasource utils', () => {

expect({ ...rest, longMessage: '' }).toMatchSnapshot();

render(
<I18nProvider>
{typeof longMessage === 'function' ? longMessage() : longMessage}
</I18nProvider>
);
render(<I18nProvider>{getLongMessage(warningMessages[0])}</I18nProvider>);

expect(screen.getByTestId('lnsPrecisionWarningEnableAccuracy')).toBeInTheDocument();
await userEvent.click(screen.getByTestId('lnsPrecisionWarningEnableAccuracy'));
Expand All @@ -155,9 +152,7 @@ describe('indexpattern_datasource utils', () => {
expect({ ...rest, longMessage: '' }).toMatchSnapshot();

const { container } = render(
<I18nProvider>
{typeof longMessage === 'function' ? longMessage() : longMessage}
</I18nProvider>
<I18nProvider>{getLongMessage(warningMessages[0])}</I18nProvider>
);
expect(container).toHaveTextContent(
'might be an approximation. For more precise results, try increasing the number of Top Values or using Filters instead.'
Expand Down Expand Up @@ -199,11 +194,7 @@ describe('indexpattern_datasource utils', () => {

expect({ ...rest, longMessage: '' }).toMatchSnapshot();

render(
<I18nProvider>
{typeof longMessage === 'function' ? longMessage() : longMessage}
</I18nProvider>
);
render(<I18nProvider>{getLongMessage(warningMessages[0])}</I18nProvider>);
await userEvent.click(screen.getByText('Rank by rarity'));
const stateSetter = setState.mock.calls[0][0];
const newState = stateSetter(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
import { getSharedActions } from './layer_actions/layer_actions';
import { FlyoutContainer } from '../../../shared_components/flyout_container';
import { FakeDimensionButton } from './buttons/fake_dimension_button';
import { getLongMessage } from '../../../user_messages_utils';

export function LayerPanel(props: LayerPanelProps) {
const [openDimension, setOpenDimension] = useState<{
Expand Down Expand Up @@ -573,10 +574,7 @@ export function LayerPanel(props: LayerPanelProps) {
? {
severity: firstMessage.severity,
content:
firstMessage.shortMessage ||
(typeof firstMessage.longMessage === 'function'
? firstMessage.longMessage()
: firstMessage.longMessage),
firstMessage.shortMessage || getLongMessage(firstMessage),
}
: undefined
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
import type { LensInspector } from '../../lens_inspector_service';
import { ErrorBoundary, showMemoizedErrorNotification } from '../../lens_ui_errors';
import { IndexPatternServiceAPI } from '../../data_views_service/service';
import { getLongMessage } from '../../user_messages_utils';

export interface EditorFrameProps {
datasourceMap: DatasourceMap;
Expand Down Expand Up @@ -128,11 +129,7 @@ export function EditorFrame(props: EditorFrameProps) {
bannerMessages={
bannerMessages.length ? (
<ErrorBoundary onError={onError}>
<BannerWrapper
nodes={bannerMessages.map(({ longMessage }) =>
typeof longMessage === 'function' ? longMessage() : longMessage
)}
/>
<BannerWrapper nodes={bannerMessages.map(getLongMessage)} />
</ErrorBoundary>
) : undefined
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
EuiText,
} from '@elastic/eui';
import type { UserMessage } from '../../../types';
import { getLongMessage } from '../../../user_messages_utils';

interface Props {
errors: Array<string | UserMessage>;
Expand Down Expand Up @@ -56,12 +57,7 @@ export function WorkspaceErrors(props: Props) {
{activeError.longMessage ? (
<>
<EuiSpacer />
<EuiText size="s">
{' '}
{typeof activeError.longMessage === 'function'
? activeError.longMessage()
: activeError.longMessage}
</EuiText>
<EuiText size="s"> {getLongMessage(activeError)}</EuiText>
</>
) : null}
</div>
Expand Down
7 changes: 2 additions & 5 deletions x-pack/plugins/lens/public/embeddable/embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ import { EmbeddableFeatureBadge } from './embeddable_info_badges';
import { getDatasourceLayers } from '../state_management/utils';
import type { EditLensConfigurationProps } from '../app_plugin/shared/edit_on_the_fly/get_edit_lens_configuration';
import { TextBasedPersistedState } from '../datasources/text_based/types';
import { getLongMessage } from '../user_messages_utils';

export type LensSavedObjectAttributes = Omit<Document, 'savedObjectId' | 'type'>;

Expand Down Expand Up @@ -263,11 +264,7 @@ function VisualizationErrorPanel({ errors, canEdit }: { errors: UserMessage[]; c
<>
{firstError ? (
<>
<p>
{typeof firstError.longMessage === 'function'
? firstError.longMessage()
: firstError.longMessage}
</p>
<p>{getLongMessage(firstError)}</p>
{errors.length > 1 && !canFixInLens ? (
<p>
<FormattedMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import React, { Fragment } from 'react';
import { useState } from 'react';
import type { UserMessage } from '../types';
import './embeddable_info_badges.scss';
import { getLongMessage } from '../user_messages_utils';

export const EmbeddableFeatureBadge = ({ messages }: { messages: UserMessage[] }) => {
const { euiTheme } = useEuiTheme();
Expand Down Expand Up @@ -98,10 +99,8 @@ export const EmbeddableFeatureBadge = ({ messages }: { messages: UserMessage[] }
<h3>{shortMessage}</h3>
</EuiTitle>
<ul className="lnsEmbeddablePanelFeatureList">
{messageGroup.map(({ longMessage }, i) => (
<Fragment key={`${uniqueId}-${i}`}>
{typeof longMessage === 'function' ? longMessage() : longMessage}
</Fragment>
{messageGroup.map((message, i) => (
<Fragment key={`${uniqueId}-${i}`}>{getLongMessage(message)}</Fragment>
))}
</ul>
</aside>
Expand Down
12 changes: 12 additions & 0 deletions x-pack/plugins/lens/public/user_messages_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* 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 { UserMessage } from './types';

export function getLongMessage(msg: UserMessage) {
return typeof msg.longMessage === 'function' ? msg.longMessage() : msg.longMessage;
}

0 comments on commit 18f4f3b

Please sign in to comment.