[Fix]: Resolve glitch in graph due to variable and time stamp change (#4406)

* refactor: resolve glitch in variable and time stamp change

* refactor: removed unwanted code

* refactor: updated code
This commit is contained in:
Rajat Dabade 2024-01-22 16:36:03 +05:30 committed by GitHub
parent 255b3dd3b0
commit d563778479
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 52 additions and 44 deletions

View File

@ -18,13 +18,14 @@ import { getDashboardVariables } from 'lib/dashbaordVariables/getDashboardVariab
import { getUPlotChartOptions } from 'lib/uPlotLib/getUplotChartOptions'; import { getUPlotChartOptions } from 'lib/uPlotLib/getUplotChartOptions';
import { getUPlotChartData } from 'lib/uPlotLib/utils/getUplotChartData'; import { getUPlotChartData } from 'lib/uPlotLib/utils/getUplotChartData';
import { useDashboard } from 'providers/Dashboard/Dashboard'; import { useDashboard } from 'providers/Dashboard/Dashboard';
import { useCallback, useEffect, useRef, useState } from 'react'; import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useSelector } from 'react-redux'; import { useSelector } from 'react-redux';
import { AppState } from 'store/reducers'; import { AppState } from 'store/reducers';
import { GlobalReducer } from 'types/reducer/globalTime'; import { GlobalReducer } from 'types/reducer/globalTime';
import uPlot from 'uplot'; import uPlot from 'uplot';
import { getTimeRange } from 'utils/getTimeRange'; import { getTimeRange } from 'utils/getTimeRange';
import { getGraphVisibilityStateOnDataChange } from '../utils';
import { PANEL_TYPES_VS_FULL_VIEW_TABLE } from './contants'; import { PANEL_TYPES_VS_FULL_VIEW_TABLE } from './contants';
import GraphManager from './GraphManager'; import GraphManager from './GraphManager';
// import GraphManager from './GraphManager'; // import GraphManager from './GraphManager';
@ -36,13 +37,14 @@ function FullView({
fullViewOptions = true, fullViewOptions = true,
onClickHandler, onClickHandler,
name, name,
originalName,
yAxisUnit, yAxisUnit,
options,
onDragSelect, onDragSelect,
isDependedDataLoaded = false, isDependedDataLoaded = false,
graphsVisibilityStates,
onToggleModelHandler, onToggleModelHandler,
parentChartRef, parentChartRef,
setGraphsVisibilityStates, parentGraphVisibilityState,
}: FullViewProps): JSX.Element { }: FullViewProps): JSX.Element {
const { selectedTime: globalSelectedTime } = useSelector< const { selectedTime: globalSelectedTime } = useSelector<
AppState, AppState,
@ -55,6 +57,20 @@ function FullView({
const { selectedDashboard, isDashboardLocked } = useDashboard(); const { selectedDashboard, isDashboardLocked } = useDashboard();
const { graphVisibilityStates: localStoredVisibilityStates } = useMemo(
() =>
getGraphVisibilityStateOnDataChange({
options,
isExpandedName: false,
name: originalName,
}),
[options, originalName],
);
const [graphsVisibilityStates, setGraphsVisibilityStates] = useState(
localStoredVisibilityStates,
);
const getSelectedTime = useCallback( const getSelectedTime = useCallback(
() => () =>
timeItems.find((e) => e.enum === (widget?.timePreferance || 'GLOBAL_TIME')), timeItems.find((e) => e.enum === (widget?.timePreferance || 'GLOBAL_TIME')),
@ -144,9 +160,9 @@ function FullView({
useEffect(() => { useEffect(() => {
graphsVisibilityStates?.forEach((e, i) => { graphsVisibilityStates?.forEach((e, i) => {
fullViewChartRef?.current?.toggleGraph(i, e); fullViewChartRef?.current?.toggleGraph(i, e);
parentChartRef?.current?.toggleGraph(i, e);
}); });
}, [graphsVisibilityStates, parentChartRef]); parentGraphVisibilityState(graphsVisibilityStates);
}, [graphsVisibilityStates, parentGraphVisibilityState]);
if (response.isFetching) { if (response.isFetching) {
return <Spinner height="100%" size="large" tip="Loading..." />; return <Spinner height="100%" size="large" tip="Loading..." />;
@ -206,7 +222,7 @@ function FullView({
{canModifyChart && chartOptions && !isDashboardLocked && ( {canModifyChart && chartOptions && !isDashboardLocked && (
<GraphManager <GraphManager
data={chartData} data={chartData}
name={name} name={originalName}
options={chartOptions} options={chartOptions}
yAxisUnit={yAxisUnit} yAxisUnit={yAxisUnit}
onToggleModelHandler={onToggleModelHandler} onToggleModelHandler={onToggleModelHandler}

View File

@ -50,13 +50,14 @@ export interface FullViewProps {
fullViewOptions?: boolean; fullViewOptions?: boolean;
onClickHandler?: OnClickPluginOpts['onClick']; onClickHandler?: OnClickPluginOpts['onClick'];
name: string; name: string;
originalName: string;
options: uPlot.Options;
yAxisUnit?: string; yAxisUnit?: string;
onDragSelect: (start: number, end: number) => void; onDragSelect: (start: number, end: number) => void;
isDependedDataLoaded?: boolean; isDependedDataLoaded?: boolean;
graphsVisibilityStates?: boolean[];
onToggleModelHandler?: GraphManagerProps['onToggleModelHandler']; onToggleModelHandler?: GraphManagerProps['onToggleModelHandler'];
setGraphsVisibilityStates: Dispatch<SetStateAction<boolean[]>>;
parentChartRef: GraphManagerProps['lineChartRef']; parentChartRef: GraphManagerProps['lineChartRef'];
parentGraphVisibilityState: Dispatch<SetStateAction<boolean[]>>;
} }
export interface GraphManagerProps extends UplotProps { export interface GraphManagerProps extends UplotProps {
@ -64,8 +65,8 @@ export interface GraphManagerProps extends UplotProps {
yAxisUnit?: string; yAxisUnit?: string;
onToggleModelHandler?: () => void; onToggleModelHandler?: () => void;
options: uPlot.Options; options: uPlot.Options;
setGraphsVisibilityStates: FullViewProps['setGraphsVisibilityStates']; graphsVisibilityStates?: boolean[];
graphsVisibilityStates: FullViewProps['graphsVisibilityStates']; setGraphsVisibilityStates: Dispatch<SetStateAction<boolean[]>>;
lineChartRef?: MutableRefObject<ToggleGraphProps | undefined>; lineChartRef?: MutableRefObject<ToggleGraphProps | undefined>;
parentChartRef?: MutableRefObject<ToggleGraphProps | undefined>; parentChartRef?: MutableRefObject<ToggleGraphProps | undefined>;
} }

View File

@ -17,7 +17,6 @@ import {
SetStateAction, SetStateAction,
useCallback, useCallback,
useEffect, useEffect,
useMemo,
useRef, useRef,
useState, useState,
} from 'react'; } from 'react';
@ -39,14 +38,15 @@ function WidgetGraphComponent({
queryResponse, queryResponse,
errorMessage, errorMessage,
name, name,
onClickHandler,
threshold, threshold,
headerMenuList, headerMenuList,
isWarning, isWarning,
data, data,
options, options,
graphVisibiltyState,
onClickHandler,
onDragSelect, onDragSelect,
graphVisibility, setGraphVisibility,
}: WidgetGraphComponentProps): JSX.Element { }: WidgetGraphComponentProps): JSX.Element {
const [deleteModal, setDeleteModal] = useState(false); const [deleteModal, setDeleteModal] = useState(false);
const [hovered, setHovered] = useState(false); const [hovered, setHovered] = useState(false);
@ -60,33 +60,28 @@ function WidgetGraphComponent({
const lineChartRef = useRef<ToggleGraphProps>(); const lineChartRef = useRef<ToggleGraphProps>();
const graphRef = useRef<HTMLDivElement>(null); const graphRef = useRef<HTMLDivElement>(null);
const { graphVisibilityStates: localStoredVisibilityStates } = useMemo( useEffect(() => {
() => if (queryResponse.isSuccess) {
getGraphVisibilityStateOnDataChange({ const {
graphVisibilityStates: localStoredVisibilityState,
} = getGraphVisibilityStateOnDataChange({
options, options,
isExpandedName: true, isExpandedName: false,
name, name,
}), });
[options, name], setGraphVisibility(localStoredVisibilityState);
); }
// eslint-disable-next-line react-hooks/exhaustive-deps
const [graphsVisibilityStates, setGraphsVisibilityStates] = useState< }, [queryResponse.isSuccess]);
boolean[]
>(localStoredVisibilityStates);
useEffect(() => { useEffect(() => {
setGraphsVisibilityStates(localStoredVisibilityStates);
if (!lineChartRef.current) return; if (!lineChartRef.current) return;
localStoredVisibilityStates.forEach((state, index) => { graphVisibiltyState.forEach((state, index) => {
lineChartRef.current?.toggleGraph(index, state); lineChartRef.current?.toggleGraph(index, state);
}); });
setGraphsVisibilityStates(localStoredVisibilityStates); // eslint-disable-next-line react-hooks/exhaustive-deps
}, [localStoredVisibilityStates]); }, []);
graphVisibility?.forEach((state, index) => {
lineChartRef.current?.toggleGraph(index, state);
});
const { setLayouts, selectedDashboard, setSelectedDashboard } = useDashboard(); const { setLayouts, selectedDashboard, setSelectedDashboard } = useDashboard();
@ -279,13 +274,14 @@ function WidgetGraphComponent({
> >
<FullView <FullView
name={`${name}expanded`} name={`${name}expanded`}
originalName={name}
widget={widget} widget={widget}
yAxisUnit={widget.yAxisUnit} yAxisUnit={widget.yAxisUnit}
onToggleModelHandler={onToggleModelHandler} onToggleModelHandler={onToggleModelHandler}
parentChartRef={lineChartRef} parentChartRef={lineChartRef}
parentGraphVisibilityState={setGraphVisibility}
onDragSelect={onDragSelect} onDragSelect={onDragSelect}
setGraphsVisibilityStates={setGraphsVisibilityStates} options={options}
graphsVisibilityStates={graphsVisibilityStates}
/> />
</Modal> </Modal>

View File

@ -167,13 +167,6 @@ function GridCardGraph({
Array(queryResponse.data?.payload?.data.result.length || 0).fill(true), Array(queryResponse.data?.payload?.data.result.length || 0).fill(true),
); );
useEffect(() => {
setGraphVisibility([
true,
...Array(queryResponse.data?.payload?.data.result.length).fill(true),
]);
}, [queryResponse.data?.payload?.data.result.length]);
const options = useMemo( const options = useMemo(
() => () =>
getUPlotChartOptions({ getUPlotChartOptions({
@ -227,7 +220,8 @@ function GridCardGraph({
threshold={threshold} threshold={threshold}
headerMenuList={menuList} headerMenuList={menuList}
onClickHandler={onClickHandler} onClickHandler={onClickHandler}
graphVisibility={graphVisibility} graphVisibiltyState={graphVisibility}
setGraphVisibility={setGraphVisibility}
/> />
)} )}
</div> </div>

View File

@ -1,7 +1,7 @@
import { ToggleGraphProps } from 'components/Graph/types'; import { ToggleGraphProps } from 'components/Graph/types';
import { UplotProps } from 'components/Uplot/Uplot'; import { UplotProps } from 'components/Uplot/Uplot';
import { OnClickPluginOpts } from 'lib/uPlotLib/plugins/onClickPlugin'; import { OnClickPluginOpts } from 'lib/uPlotLib/plugins/onClickPlugin';
import { MutableRefObject, ReactNode } from 'react'; import { Dispatch, MutableRefObject, ReactNode, SetStateAction } from 'react';
import { UseQueryResult } from 'react-query'; import { UseQueryResult } from 'react-query';
import { ErrorResponse, SuccessResponse } from 'types/api'; import { ErrorResponse, SuccessResponse } from 'types/api';
import { Dashboard, Widgets } from 'types/api/dashboard/getAll'; import { Dashboard, Widgets } from 'types/api/dashboard/getAll';
@ -28,7 +28,8 @@ export interface WidgetGraphComponentProps extends UplotProps {
threshold?: ReactNode; threshold?: ReactNode;
headerMenuList: MenuItemKeys[]; headerMenuList: MenuItemKeys[];
isWarning: boolean; isWarning: boolean;
graphVisibility?: boolean[]; graphVisibiltyState: boolean[];
setGraphVisibility: Dispatch<SetStateAction<boolean[]>>;
} }
export interface GridCardGraphProps { export interface GridCardGraphProps {