Skip to content

Commit b1f738a

Browse files
committed
fix too many rernders
1 parent 8570992 commit b1f738a

File tree

3 files changed

+80
-49
lines changed

3 files changed

+80
-49
lines changed

x-pack/plugins/observability/public/components/shared/exploratory_view/exploratory_view.tsx

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 2.0.
66
*/
77
import { i18n } from '@kbn/i18n';
8-
import React, { useEffect, useRef, useState, useCallback } from 'react';
8+
import React, { useEffect, useRef, useState } from 'react';
99
import { EuiPanel, EuiTitle } from '@elastic/eui';
1010
import styled from 'styled-components';
1111
import { isEmpty } from 'lodash';
@@ -14,11 +14,12 @@ import { ObservabilityPublicPluginsStart } from '../../../plugin';
1414
import { ExploratoryViewHeader } from './header/header';
1515
import { useSeriesStorage } from './hooks/use_series_storage';
1616
import { useLensAttributes } from './hooks/use_lens_attributes';
17-
import { EmptyView } from './components/empty_view';
1817
import { TypedLensByValueInput } from '../../../../../lens/public';
1918
import { useAppIndexPatternContext } from './hooks/use_app_index_pattern';
2019
import { SeriesBuilder } from './series_builder/series_builder';
2120
import { SeriesUrl } from './types';
21+
import { LensEmbeddable } from './lens_embeddable';
22+
import { EmptyView } from './components/empty_view';
2223

2324
export const combineTimeRanges = (
2425
allSeries: Record<string, SeriesUrl>,
@@ -52,14 +53,13 @@ export function ExploratoryView({
5253
saveAttributes?: (attr: TypedLensByValueInput['attributes'] | null) => void;
5354
}) {
5455
const {
55-
services: { lens, notifications },
56+
services: { lens },
5657
} = useKibana<ObservabilityPublicPluginsStart>();
5758

5859
const seriesBuilderRef = useRef<HTMLDivElement>(null);
5960
const wrapperRef = useRef<HTMLDivElement>(null);
6061

6162
const [height, setHeight] = useState<string>('100vh');
62-
const [seriesId, setSeriesId] = useState<string>('');
6363

6464
const [lastUpdated, setLastUpdated] = useState<number | undefined>();
6565

@@ -69,13 +69,7 @@ export function ExploratoryView({
6969

7070
const { loadIndexPattern, loading } = useAppIndexPatternContext();
7171

72-
const LensComponent = lens?.EmbeddableComponent;
73-
74-
const { firstSeriesId, firstSeries: series, setSeries, allSeries } = useSeriesStorage();
75-
76-
useEffect(() => {
77-
setSeriesId(firstSeriesId);
78-
}, [allSeries, firstSeriesId]);
72+
const { firstSeries, firstSeriesId, allSeries } = useSeriesStorage();
7973

8074
const lensAttributesT = useLensAttributes();
8175

@@ -108,49 +102,16 @@ export function ExploratoryView({
108102
setHeightOffset();
109103
});
110104

111-
const timeRange = combineTimeRanges(allSeries, series);
112-
113-
const onLensLoad = useCallback(() => {
114-
setLastUpdated(Date.now());
115-
}, []);
116-
117-
const onBrushEnd = useCallback(
118-
({ range }: { range: number[] }) => {
119-
if (series?.reportType !== 'data-distribution') {
120-
setSeries(seriesId, {
121-
...series,
122-
time: {
123-
from: new Date(range[0]).toISOString(),
124-
to: new Date(range[1]).toISOString(),
125-
},
126-
});
127-
} else {
128-
notifications?.toasts.add(
129-
i18n.translate('xpack.observability.exploratoryView.noBrusing', {
130-
defaultMessage: 'Zoom by brush selection is only available on time series charts.',
131-
})
132-
);
133-
}
134-
},
135-
[notifications?.toasts, series, seriesId, setSeries]
136-
);
137-
138105
return (
139106
<Wrapper>
140107
{lens ? (
141108
<>
142-
<ExploratoryViewHeader lensAttributes={lensAttributes} seriesId={seriesId} />
109+
<ExploratoryViewHeader lensAttributes={lensAttributes} seriesId={firstSeriesId} />
143110
<LensWrapper ref={wrapperRef} height={height}>
144-
{lensAttributes && timeRange.to && timeRange.from ? (
145-
<LensComponent
146-
id="exploratoryView"
147-
timeRange={timeRange}
148-
attributes={lensAttributes}
149-
onLoad={onLensLoad}
150-
onBrushEnd={onBrushEnd}
151-
/>
111+
{lensAttributes ? (
112+
<LensEmbeddable setLastUpdated={setLastUpdated} lensAttributes={lensAttributes} />
152113
) : (
153-
<EmptyView series={series} loading={loading} height={height} />
114+
<EmptyView series={firstSeries} loading={loading} height={height} />
154115
)}
155116
</LensWrapper>
156117
<SeriesBuilder

x-pack/plugins/observability/public/components/shared/exploratory_view/hooks/use_series_storage.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ export function UrlStorageContextProvider({
5959
convertAllShortSeries(storage.get(allSeriesKey) ?? {})
6060
);
6161
const [firstSeriesId, setFirstSeriesId] = useState('');
62+
const [firstSeries, setFirstSeries] = useState<SeriesUrl>();
6263

6364
useEffect(() => {
6465
const allSeriesIds = Object.keys(allShortSeries);
6566
const allSeriesN: AllSeries = convertAllShortSeries(allShortSeries ?? {});
6667

6768
setAllSeries(allSeriesN);
6869
setFirstSeriesId(allSeriesIds?.[0]);
70+
setFirstSeries(allSeriesN?.[0]);
6971
(storage as IKbnUrlStateStorage).set(allSeriesKey, allShortSeries);
7072
}, [allShortSeries, storage]);
7173

@@ -100,7 +102,7 @@ export function UrlStorageContextProvider({
100102
firstSeriesId,
101103
allSeries,
102104
allSeriesIds,
103-
firstSeries: allSeries?.[firstSeriesId],
105+
firstSeries: firstSeries!,
104106
};
105107
return <UrlStorageContext.Provider value={value}>{children}</UrlStorageContext.Provider>;
106108
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { i18n } from '@kbn/i18n';
9+
import React, { Dispatch, SetStateAction, useCallback } from 'react';
10+
import { combineTimeRanges } from './exploratory_view';
11+
import { TypedLensByValueInput } from '../../../../../lens/public';
12+
import { useSeriesStorage } from './hooks/use_series_storage';
13+
import { ObservabilityPublicPluginsStart } from '../../../plugin';
14+
import { useKibana } from '../../../../../../../src/plugins/kibana_react/public';
15+
16+
interface Props {
17+
lensAttributes: TypedLensByValueInput['attributes'];
18+
setLastUpdated: Dispatch<SetStateAction<number | undefined>>;
19+
}
20+
21+
export function LensEmbeddable(props: Props) {
22+
const { lensAttributes, setLastUpdated } = props;
23+
24+
const {
25+
services: { lens, notifications },
26+
} = useKibana<ObservabilityPublicPluginsStart>();
27+
28+
const LensComponent = lens?.EmbeddableComponent;
29+
30+
const { firstSeriesId, firstSeries: series, setSeries, allSeries } = useSeriesStorage();
31+
32+
const timeRange = combineTimeRanges(allSeries, series);
33+
34+
const onLensLoad = useCallback(() => {
35+
setLastUpdated(Date.now());
36+
}, [setLastUpdated]);
37+
38+
const onBrushEnd = useCallback(
39+
({ range }: { range: number[] }) => {
40+
if (series?.reportType !== 'data-distribution') {
41+
setSeries(firstSeriesId, {
42+
...series,
43+
time: {
44+
from: new Date(range[0]).toISOString(),
45+
to: new Date(range[1]).toISOString(),
46+
},
47+
});
48+
} else {
49+
notifications?.toasts.add(
50+
i18n.translate('xpack.observability.exploratoryView.noBrusing', {
51+
defaultMessage: 'Zoom by brush selection is only available on time series charts.',
52+
})
53+
);
54+
}
55+
},
56+
[notifications?.toasts, series, firstSeriesId, setSeries]
57+
);
58+
59+
return (
60+
<LensComponent
61+
id="exploratoryView"
62+
timeRange={timeRange}
63+
attributes={lensAttributes}
64+
onLoad={onLensLoad}
65+
onBrushEnd={onBrushEnd}
66+
/>
67+
);
68+
}

0 commit comments

Comments
 (0)