import './WidgetFullView.styles.scss'; import { SyncOutlined } from '@ant-design/icons'; import { Button } from 'antd'; import cx from 'classnames'; import { ToggleGraphProps } from 'components/Graph/types'; import Spinner from 'components/Spinner'; import TimePreference from 'components/TimePreferenceDropDown'; import { PANEL_TYPES } from 'constants/queryBuilder'; import GridPanelSwitch from 'container/GridPanelSwitch'; import { timeItems, timePreferance, } from 'container/NewWidget/RightContainer/timeItems'; import { useGetQueryRange } from 'hooks/queryBuilder/useGetQueryRange'; import { useStepInterval } from 'hooks/queryBuilder/useStepInterval'; import { useChartMutable } from 'hooks/useChartMutable'; import { useIsDarkMode } from 'hooks/useDarkMode'; import { getDashboardVariables } from 'lib/dashbaordVariables/getDashboardVariables'; import { getUPlotChartOptions } from 'lib/uPlotLib/getUplotChartOptions'; import { getUPlotChartData } from 'lib/uPlotLib/utils/getUplotChartData'; import { useDashboard } from 'providers/Dashboard/Dashboard'; import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { useSelector } from 'react-redux'; import { AppState } from 'store/reducers'; import { GlobalReducer } from 'types/reducer/globalTime'; import uPlot from 'uplot'; import { getTimeRange } from 'utils/getTimeRange'; import { getGraphVisibilityStateOnDataChange } from '../utils'; import { PANEL_TYPES_VS_FULL_VIEW_TABLE } from './contants'; import GraphManager from './GraphManager'; // import GraphManager from './GraphManager'; import { GraphContainer, TimeContainer } from './styles'; import { FullViewProps } from './types'; function FullView({ widget, fullViewOptions = true, onClickHandler, name, originalName, yAxisUnit, options, onDragSelect, isDependedDataLoaded = false, onToggleModelHandler, parentChartRef, parentGraphVisibilityState, }: FullViewProps): JSX.Element { const { selectedTime: globalSelectedTime } = useSelector< AppState, GlobalReducer >((state) => state.globalTime); const fullViewRef = useRef(null); const [chartOptions, setChartOptions] = useState(); const { selectedDashboard, isDashboardLocked } = useDashboard(); const { graphVisibilityStates: localStoredVisibilityStates } = useMemo( () => getGraphVisibilityStateOnDataChange({ options, isExpandedName: false, name: originalName, }), [options, originalName], ); const [graphsVisibilityStates, setGraphsVisibilityStates] = useState( localStoredVisibilityStates, ); const getSelectedTime = useCallback( () => timeItems.find((e) => e.enum === (widget?.timePreferance || 'GLOBAL_TIME')), [widget], ); const fullViewChartRef = useRef(); const [selectedTime, setSelectedTime] = useState({ name: getSelectedTime()?.name || '', enum: widget?.timePreferance || 'GLOBAL_TIME', }); const updatedQuery = useStepInterval(widget?.query); const response = useGetQueryRange( { selectedTime: selectedTime.enum, graphType: widget.panelTypes, query: updatedQuery, globalSelectedInterval: globalSelectedTime, variables: getDashboardVariables(selectedDashboard?.data.variables), }, { queryKey: `FullViewGetMetricsQueryRange-${selectedTime.enum}-${globalSelectedTime}-${widget.id}`, enabled: !isDependedDataLoaded && widget.panelTypes !== PANEL_TYPES.LIST, // Internally both the list view panel has it's own query range api call, so we don't need to call it again }, ); const canModifyChart = useChartMutable({ panelType: widget.panelTypes, panelTypeAndGraphManagerVisibility: PANEL_TYPES_VS_FULL_VIEW_TABLE, }); const chartData = getUPlotChartData(response?.data?.payload, widget.fillSpans); const isDarkMode = useIsDarkMode(); const [minTimeScale, setMinTimeScale] = useState(); const [maxTimeScale, setMaxTimeScale] = useState(); const { minTime, maxTime, selectedTime: globalSelectedInterval } = useSelector< AppState, GlobalReducer >((state) => state.globalTime); useEffect((): void => { const { startTime, endTime } = getTimeRange(response); setMinTimeScale(startTime); setMaxTimeScale(endTime); }, [maxTime, minTime, globalSelectedInterval, response]); useEffect(() => { if (!response.isFetching && fullViewRef.current) { const width = fullViewRef.current?.clientWidth ? fullViewRef.current.clientWidth - 45 : 700; const height = fullViewRef.current?.clientWidth ? fullViewRef.current.clientHeight : 300; const newChartOptions = getUPlotChartOptions({ yAxisUnit: yAxisUnit || '', apiResponse: response.data?.payload, dimensions: { height, width, }, isDarkMode, onDragSelect, graphsVisibilityStates, setGraphsVisibilityStates, thresholds: widget.thresholds, minTimeScale, maxTimeScale, softMax: widget.softMax === undefined ? null : widget.softMax, softMin: widget.softMin === undefined ? null : widget.softMin, }); setChartOptions(newChartOptions); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [response.isFetching, graphsVisibilityStates, fullViewRef.current]); useEffect(() => { graphsVisibilityStates?.forEach((e, i) => { fullViewChartRef?.current?.toggleGraph(i, e); }); parentGraphVisibilityState(graphsVisibilityStates); }, [graphsVisibilityStates, parentGraphVisibilityState]); const isListView = widget.panelTypes === PANEL_TYPES.LIST; if (response.isFetching) { return ; } return (
{fullViewOptions && (
{chartOptions && ( )}
{canModifyChart && chartOptions && !isDashboardLocked && ( )}
); } FullView.defaultProps = { fullViewOptions: undefined, onClickHandler: undefined, yAxisUnit: undefined, onDragSelect: undefined, isDependedDataLoaded: undefined, }; FullView.displayName = 'FullView'; export default FullView;