Skip to content

Commit

Permalink
[fix] Prevent infinite useEffects loop in range-plot (#2892)
Browse files Browse the repository at this point in the history
Signed-off-by: Ihor Dykhta <[email protected]>
  • Loading branch information
igorDykhta authored Jan 3, 2025
1 parent 0b67c54 commit 4c9ffe8
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions src/components/src/common/range-plot.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project

import React, {useCallback, useMemo, useState, useEffect, CSSProperties} from 'react';
import React, {useCallback, useEffect, useMemo, useRef, useState, CSSProperties} from 'react';
import styled, {withTheme} from 'styled-components';
import RangeBrushFactory, {OnBrush, RangeBrushProps} from './range-brush';
import HistogramPlotFactory from './histogram-plot';
Expand Down Expand Up @@ -181,36 +181,39 @@ export default function RangePlotFactory(
...otherProps
}: WithPlotLoadingProps) => {
const [isLoading, setIsLoading] = useState(false);
// eslint-disable-next-line complexity
const isChangingRef = useRef(false);

useEffect(() => {
if (isChangingRef.current) {
if (hasHistogram(plotType, bins)) {
// Bins are loaded
isChangingRef.current = false;
}
} else {
if (!plotType || (isHistogramPlot(plotType) && !bins)) {
// load histogram
setIsLoading(true);
setFilterPlot({plotType: {type: PLOT_TYPES.histogram}});
isChangingRef.current = true;
}
}
}, [bins, plotType, setFilterPlot]);

useEffect(() => {
if (!plotType && !isLoading) {
// if plotType is undefined. this shouldn't happen
setIsLoading(true);
setFilterPlot({
plotType: {
type: PLOT_TYPES.histogram
}
});
} else if (isHistogramPlot(plotType) && !bins && !isLoading) {
setIsLoading(true);
// load histogram
setFilterPlot({
plotType: {
type: PLOT_TYPES.histogram
}
});
} else if (isLineChart(plotType) && !lineChart && !isLoading) {
// load line chart
setIsLoading(true);
setFilterPlot({
plotType: {
type: PLOT_TYPES.lineChart
}
});
} else if (isLoading && (hasHistogram(plotType, bins) || hasLineChart(plotType, lineChart))) {
setIsLoading(false);
if (isChangingRef.current) {
if (hasLineChart(plotType, lineChart)) {
// Line chart is loaded
isChangingRef.current = false;
}
} else {
if (isLineChart(plotType) && !lineChart) {
// load line chart
setIsLoading(true);
setFilterPlot({plotType: {type: PLOT_TYPES.lineChart}});
isChangingRef.current = true;
}
}
}, [plotType, bins, lineChart, setFilterPlot, isLoading, setIsLoading]);
}, [lineChart, plotType, setFilterPlot]);

const rangePlotStyle = useMemo(
() => ({
Expand Down

0 comments on commit 4c9ffe8

Please sign in to comment.