diff --git a/x-pack/plugins/lens/public/app_plugin/get_application_user_messages.test.tsx b/x-pack/plugins/lens/public/app_plugin/get_application_user_messages.test.tsx index 4345ae6795a36..9233ed332be6b 100644 --- a/x-pack/plugins/lens/public/app_plugin/get_application_user_messages.test.tsx +++ b/x-pack/plugins/lens/public/app_plugin/get_application_user_messages.test.tsx @@ -164,14 +164,13 @@ describe('application-level user messages', () => { visualization: {} as Visualization, visualizationState: { activeId: 'foo', state: {} }, }; + const firstMessage = getApplicationUserMessages({ ...props, ...propsOverrides }).at(0); const rtlRender = render( - { - getApplicationUserMessages({ - ...props, - ...propsOverrides, - })[0].longMessage as React.ReactNode - } + {firstMessage && + (typeof firstMessage.longMessage === 'function' + ? firstMessage.longMessage() + : firstMessage.longMessage)} ); return rtlRender; diff --git a/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts b/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts index 784ee32f28b4f..b399f8eaa7b54 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts +++ b/x-pack/plugins/lens/public/datasources/form_based/form_based.test.ts @@ -3154,9 +3154,7 @@ describe('IndexPattern Data Source', () => { values={ Object { "position": 1, - "wrappedMessage": - error 1 - , + "wrappedMessage": "error 1", } } />, @@ -3177,9 +3175,7 @@ describe('IndexPattern Data Source', () => { values={ Object { "position": 1, - "wrappedMessage": - error 2 - , + "wrappedMessage": "error 2", } } />, diff --git a/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx b/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx index 1bdd12a5cb49c..4d1d405d9ed4f 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/form_based.tsx @@ -995,7 +995,10 @@ function getLayerErrorMessages( defaultMessage="Layer {position} error: {wrappedMessage}" values={{ position: index + 1, - wrappedMessage: <>{error.longMessage}, + wrappedMessage: + typeof error.longMessage === 'function' + ? error.longMessage() + : error.longMessage, }} /> ), diff --git a/x-pack/plugins/lens/public/datasources/form_based/utils.test.tsx b/x-pack/plugins/lens/public/datasources/form_based/utils.test.tsx index b1f57635590ec..1badb72fcdace 100644 --- a/x-pack/plugins/lens/public/datasources/form_based/utils.test.tsx +++ b/x-pack/plugins/lens/public/datasources/form_based/utils.test.tsx @@ -121,10 +121,15 @@ describe('indexpattern_datasource utils', () => { ); expect(warningMessages).toHaveLength(1); + const { longMessage, ...rest } = warningMessages[0]; - expect({ ...warningMessages[0], longMessage: '' }).toMatchSnapshot(); + expect({ ...rest, longMessage: '' }).toMatchSnapshot(); - render({warningMessages[0].longMessage as React.ReactNode}); + render( + + {typeof longMessage === 'function' ? longMessage() : longMessage} + + ); expect(screen.getByTestId('lnsPrecisionWarningEnableAccuracy')).toBeInTheDocument(); await userEvent.click(screen.getByTestId('lnsPrecisionWarningEnableAccuracy')); @@ -145,11 +150,14 @@ describe('indexpattern_datasource utils', () => { ); expect(warningMessages).toHaveLength(1); + const { longMessage, ...rest } = warningMessages[0]; - expect({ ...warningMessages[0], longMessage: '' }).toMatchSnapshot(); + expect({ ...rest, longMessage: '' }).toMatchSnapshot(); const { container } = render( - {warningMessages[0].longMessage as React.ReactNode} + + {typeof longMessage === 'function' ? longMessage() : longMessage} + ); expect(container).toHaveTextContent( 'might be an approximation. For more precise results, try increasing the number of Top Values or using Filters instead.' @@ -178,7 +186,7 @@ describe('indexpattern_datasource utils', () => { } as unknown as GenericIndexPatternColumn, }; const setState = jest.fn(); - const warnings = getPrecisionErrorWarningMessages( + const warningMessages = getPrecisionErrorWarningMessages( datatableUtilitites, state, framePublicAPI, @@ -186,10 +194,16 @@ describe('indexpattern_datasource utils', () => { setState ); - expect(warnings).toHaveLength(1); - expect({ ...warnings[0], longMessage: '' }).toMatchSnapshot(); + expect(warningMessages).toHaveLength(1); + const { longMessage, ...rest } = warningMessages[0]; - render({warnings[0].longMessage as React.ReactNode}); + expect({ ...rest, longMessage: '' }).toMatchSnapshot(); + + render( + + {typeof longMessage === 'function' ? longMessage() : longMessage} + + ); await userEvent.click(screen.getByText('Rank by rarity')); const stateSetter = setState.mock.calls[0][0]; const newState = stateSetter(state); diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/layer_panel.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/layer_panel.tsx index 342d275b45e39..136fc3fac603c 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/layer_panel.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/config_panel/layer_panel.tsx @@ -518,6 +518,7 @@ export function LayerPanel(props: LayerPanelProps) { props?.getUserMessages?.('dimensionButton', { dimensionId: columnId, }) ?? []; + const firstMessage = messages.at(0); return ( {layerDatasource ? ( <> diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx index 5020d1837470f..d44b5f081a274 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/editor_frame.tsx @@ -129,7 +129,9 @@ export function EditorFrame(props: EditorFrameProps) { bannerMessages.length ? ( longMessage as React.ReactNode)} + nodes={bannerMessages.map(({ longMessage }) => + typeof longMessage === 'function' ? longMessage() : longMessage + )} /> ) : undefined diff --git a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_errors.tsx b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_errors.tsx index 949ae6648c183..4011e469f1df5 100644 --- a/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_errors.tsx +++ b/x-pack/plugins/lens/public/editor_frame_service/editor_frame/workspace_panel/workspace_errors.tsx @@ -56,7 +56,12 @@ export function WorkspaceErrors(props: Props) { {activeError.longMessage ? ( <> - {activeError.longMessage as React.ReactNode} + + {' '} + {typeof activeError.longMessage === 'function' + ? activeError.longMessage() + : activeError.longMessage} + ) : null} diff --git a/x-pack/plugins/lens/public/embeddable/embeddable.tsx b/x-pack/plugins/lens/public/embeddable/embeddable.tsx index 6c758abb81cff..da0faf7314a41 100644 --- a/x-pack/plugins/lens/public/embeddable/embeddable.tsx +++ b/x-pack/plugins/lens/public/embeddable/embeddable.tsx @@ -248,7 +248,7 @@ export interface ViewUnderlyingDataArgs { } function VisualizationErrorPanel({ errors, canEdit }: { errors: UserMessage[]; canEdit: boolean }) { - const showMore = errors.length > 1; + const firstError = errors.at(0); const canFixInLens = canEdit && errors.some(({ fixableInEditor }) => fixableInEditor); return (
@@ -258,10 +258,14 @@ function VisualizationErrorPanel({ errors, canEdit }: { errors: UserMessage[]; c data-test-subj="embeddable-lens-failure" body={ <> - {errors.length ? ( + {firstError ? ( <> -

{errors[0].longMessage as React.ReactNode}

- {showMore && !canFixInLens ? ( +

+ {typeof firstError.longMessage === 'function' + ? firstError.longMessage() + : firstError.longMessage} +

+ {errors.length > 1 && !canFixInLens ? (

    {messageGroup.map(({ longMessage }, i) => ( - {longMessage as React.ReactNode} + + {typeof longMessage === 'function' ? longMessage() : longMessage} + ))}
diff --git a/x-pack/plugins/lens/public/types.ts b/x-pack/plugins/lens/public/types.ts index f4063747e9b77..d22016f75620a 100644 --- a/x-pack/plugins/lens/public/types.ts +++ b/x-pack/plugins/lens/public/types.ts @@ -302,7 +302,7 @@ export interface UserMessage { severity: 'error' | 'warning' | 'info'; hidePopoverIcon?: boolean; shortMessage: string; - longMessage: string | React.ReactNode | ((closePopover: () => void) => React.ReactNode); + longMessage: string | React.ReactNode | ((closePopover?: () => void) => React.ReactNode); fixableInEditor: boolean; displayLocations: UserMessageDisplayLocation[]; }