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

[8.x] [Lens][Expressions] Fixes react Error When Rendering Recoverable Error (#196285) #196714

Merged
merged 1 commit into from
Oct 17, 2024
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 @@ -20,24 +20,27 @@ import { getLongMessage } from '../../../user_messages_utils';
interface Props {
errors: Array<string | UserMessage>;
title: string;
onRender?: () => void;
}

export function WorkspaceErrors(props: Props) {
export function WorkspaceErrors({ errors, title, onRender }: Props) {
const [activePage, setActivePage] = useState(0);

const activeError = props.errors.length ? props.errors[activePage] : '';
const activeError = errors.length ? errors[activePage] : '';

React.useEffect(() => onRender?.(), [onRender]);

return (
<EuiEmptyPrompt
actions={
props.errors.length > 1 ? (
errors.length > 1 ? (
<EuiFlexGroup
justifyContent="spaceAround"
data-test-subj="lnsWorkspaceErrorsPaginationControl"
>
<EuiFlexItem grow={false}>
<EuiPagination
pageCount={props.errors.length}
pageCount={errors.length}
activePage={activePage}
onPageClick={setActivePage}
/>
Expand All @@ -64,7 +67,7 @@ export function WorkspaceErrors(props: Props) {
)}
</div>
}
title={<h2>{props.title}</h2>}
title={<h2>{title}</h2>}
iconColor="danger"
iconType="warning"
data-test-subj="lnsWorkspaceErrors"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,13 +676,13 @@ export const InnerWorkspacePanel = React.memo(function InnerWorkspacePanel({

function useReportingState(errors: UserMessage[]): {
isRenderComplete: boolean;
hasDynamicError: boolean;
hasRequestError: boolean;
setHasRequestError: (state: boolean) => void;
setIsRenderComplete: (state: boolean) => void;
setDynamicError: (state: boolean) => void;
nodeRef: React.RefObject<HTMLDivElement>;
} {
const [isRenderComplete, setIsRenderComplete] = useState(Boolean(errors?.length));
const [hasDynamicError, setDynamicError] = useState(false);
const [hasRequestError, setHasRequestError] = useState(false);
const nodeRef = useRef<HTMLDivElement>(null);

useEffect(() => {
Expand All @@ -691,9 +691,13 @@ function useReportingState(errors: UserMessage[]): {
}
}, [isRenderComplete, errors]);

return { isRenderComplete, setIsRenderComplete, hasDynamicError, setDynamicError, nodeRef };
return { isRenderComplete, setIsRenderComplete, hasRequestError, setHasRequestError, nodeRef };
}

const dataLoadingErrorTitle = i18n.translate('xpack.lens.editorFrame.dataFailure', {
defaultMessage: `An error occurred when loading data`,
});

export const VisualizationWrapper = ({
expression,
lensInspector,
Expand Down Expand Up @@ -730,13 +734,14 @@ export const VisualizationWrapper = ({

const searchContext = useLensSelector(selectExecutionContextSearch);
// Used for reporting
const { isRenderComplete, hasDynamicError, setIsRenderComplete, setDynamicError, nodeRef } =
const { isRenderComplete, hasRequestError, setIsRenderComplete, setHasRequestError, nodeRef } =
useReportingState(errors);

const onRenderHandler = useCallback(() => {
setHasRequestError(false);
setIsRenderComplete(true);
onRender$();
}, [setIsRenderComplete, onRender$]);
}, [onRender$, setHasRequestError, setIsRenderComplete]);

const searchSessionId = useLensSelector(selectSearchSessionId);

Expand All @@ -759,17 +764,13 @@ export const VisualizationWrapper = ({
);
}

const dataLoadingErrorTitle = i18n.translate('xpack.lens.editorFrame.dataFailure', {
defaultMessage: `An error occurred when loading data`,
});

return (
<div
className="lnsExpressionRenderer"
data-shared-items-container
data-render-complete={isRenderComplete}
data-shared-item=""
data-render-error={hasDynamicError ? dataLoadingErrorTitle : undefined}
data-render-error={hasRequestError ? dataLoadingErrorTitle : undefined}
ref={nodeRef}
>
<ExpressionRendererComponent
Expand All @@ -795,11 +796,13 @@ export const VisualizationWrapper = ({
? [errorMessage]
: [];

if (!hasDynamicError) {
setDynamicError(true);
}

return <WorkspaceErrors errors={visibleErrorMessages} title={dataLoadingErrorTitle} />;
return (
<WorkspaceErrors
errors={visibleErrorMessages}
title={dataLoadingErrorTitle}
onRender={() => setHasRequestError(true)}
/>
);
}}
/>
</div>
Expand Down