[Lens] Fix reference line width stale update (#184414)

## Summary

Fixes #182775

Prevents stale update on line width update.
I suspect the root problem there was due to this commit
0657a08651 applied for too many
re-renderings within the annotation panel. I've removed the fix there
and used a `useMemo` for the `currentConfig` prop in the parent
component to stabilize the object reference on change.

I've profiled the renderings with the React profiler without noticing
any particular issue.
This commit is contained in:
Marco Liberati 2024-06-05 10:07:13 +02:00 committed by GitHub
parent 0a0853bef9
commit 8902e06ae6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 15 additions and 11 deletions

View file

@ -8,7 +8,7 @@
import './index.scss';
import { isFieldLensCompatible } from '@kbn/visualization-ui-components';
import React, { useCallback, useEffect } from 'react';
import React, { useCallback, useEffect, useMemo } from 'react';
import { i18n } from '@kbn/i18n';
import { EuiFormRow, EuiSwitch, EuiSwitchEvent, EuiButtonGroup, EuiSpacer } from '@elastic/eui';
import {
@ -87,6 +87,16 @@ const AnnotationEditorControls = ({
[currentAnnotation, onAnnotationChange]
);
const currentLineConfig = useMemo(() => {
if (isRange) {
return;
}
return {
lineStyle: currentAnnotation.lineStyle,
lineWidth: currentAnnotation.lineWidth,
};
}, [currentAnnotation, isRange]);
return (
<>
<DimensionEditorSection
@ -270,10 +280,7 @@ const AnnotationEditorControls = ({
<LineStyleSettings
idPrefix={idPrefix}
setConfig={update}
currentConfig={{
lineStyle: currentAnnotation.lineStyle,
lineWidth: currentAnnotation.lineWidth,
}}
currentConfig={currentLineConfig}
/>
</>
)}

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import React, { useState, memo, useCallback } from 'react';
import React, { useState, memo } from 'react';
import { i18n } from '@kbn/i18n';
import {
EuiButtonGroup,
@ -44,12 +44,9 @@ export const LineStyleSettings = ({
<EuiFlexItem>
<LineThicknessSlider
value={currentConfig?.lineWidth || 1}
// Without this memoization, EuiFieldNumber rerenders too often
// which somehow causes the annotation query to fall out of sync
onChange={useCallback((value) => {
onChange={(value) => {
setConfig({ lineWidth: value });
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])}
}}
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>